@vbarbarosh/node-helpers 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Vladimir Barbarosh <vladimir.barbarosh@gmail.com>",
3
3
  "license": "MIT",
4
4
  "name": "@vbarbarosh/node-helpers",
5
- "version": "2.2.0",
5
+ "version": "2.3.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
@@ -31,6 +31,10 @@
31
31
  },
32
32
  "devDependencies": {
33
33
  "mocha": "^10.2.0",
34
- "throttle": "^1.0.3"
34
+ "throttle": "^1.0.3",
35
+ "redis-v1": "npm:redis@1",
36
+ "redis-v2": "npm:redis@2",
37
+ "redis-v3": "npm:redis@3",
38
+ "redis-v4": "npm:redis@4"
35
39
  }
36
40
  }
@@ -15,7 +15,7 @@ describe('fs_mkdirp', function () {
15
15
  it('should create the same directory structure in parallel', async function () {
16
16
  await fs_tempdir(async function (d) {
17
17
  const tmp = fs_path_resolve(d, 'a/b/c/d/e/f/g/h');
18
- await Promise.all(Array(10).fill(tmp).map(async function (v, i) {
18
+ await Promise.all(Array(10).fill(tmp).map(async function () {
19
19
  await fs_mkdirp(tmp);
20
20
  assert(await fs_fi(tmp).then(v => v.isDirectory()));
21
21
  }));
@@ -1,7 +1,16 @@
1
1
  async function redis_lshift(redis, queue, limit = 1)
2
2
  {
3
3
  const last = limit - 1;
4
- const [items] = await redis.multi().lrange(queue, 0, last).ltrim(queue, limit, -1).exec_p();
4
+ if (redis.lrange) {
5
+ const [items] = await new Promise(function (resolve, reject) {
6
+ redis.multi().lrange(queue, 0, last).ltrim(queue, limit, -1).exec(function (error, out) {
7
+ error ? reject(error) : resolve(out);
8
+ });
9
+ });
10
+ return items;
11
+ }
12
+ // redis@4
13
+ const [items] = await redis.multi().LRANGE(queue, 0, last).LTRIM(queue, limit, -1).exec();
5
14
  return items;
6
15
  }
7
16
 
@@ -0,0 +1,57 @@
1
+ const assert = require('assert');
2
+ const redis_lshift = require('./redis_lshift');
3
+
4
+ const REDIS_HOST = '127.0.0.1';
5
+ const REDIS_PORT = 6379;
6
+ const REDIS_AUTH = null;
7
+ const REDIS_QUEUE = 'TESTING';
8
+
9
+ describe('redis_lshift', function () {
10
+ it('should work with redis@1', async function () {
11
+ await test_1_2_3(require('redis-v1'));
12
+ });
13
+ it('should work with redis@2', async function () {
14
+ await test_1_2_3(require('redis-v2'));
15
+ });
16
+ it('should work with redis@3', async function () {
17
+ await test_1_2_3(require('redis-v3'));
18
+ });
19
+ it('should work with redis@4', async function () {
20
+ await test_4(require('redis-v4'));
21
+ });
22
+ });
23
+
24
+ async function test_1_2_3(redis)
25
+ {
26
+ const r = redis.createClient(REDIS_PORT, REDIS_HOST, {password: REDIS_AUTH || undefined});
27
+ try {
28
+ const expected = 'aa,bb,cc,dd,ee'.split(',');
29
+ await new Promise(function (resolve, reject) {
30
+ r.del(REDIS_QUEUE, (e,v) => e ? reject(e) : resolve(v));
31
+ });
32
+ await new Promise(function (resolve, reject) {
33
+ r.rpush(REDIS_QUEUE, ...expected, (e,v) => e ? reject(e) : resolve(v));
34
+ });
35
+ const actual = await redis_lshift(r, REDIS_QUEUE, expected.length);
36
+ await assert.deepStrictEqual(actual, expected);
37
+ }
38
+ finally {
39
+ await r.quit();
40
+ }
41
+ }
42
+
43
+ async function test_4(redis)
44
+ {
45
+ const r = redis.createClient(REDIS_PORT, REDIS_HOST, {password: REDIS_AUTH || undefined});
46
+ await r.connect();
47
+ try {
48
+ const expected = 'aa,bb,cc,dd,ee'.split(',');
49
+ await r.DEL(REDIS_QUEUE);
50
+ await r.RPUSH(REDIS_QUEUE, expected);
51
+ const actual = await redis_lshift(r, REDIS_QUEUE, expected.length);
52
+ await assert.deepStrictEqual(actual, expected);
53
+ }
54
+ finally {
55
+ await r.quit();
56
+ }
57
+ }
@@ -1,8 +1,16 @@
1
1
  async function redis_zshift(redis, queue, limit = 1)
2
2
  {
3
3
  const last = limit - 1;
4
- // noinspection JSUnresolvedFunction
5
- const [items] = await redis.multi().zrange(queue, 0, last).zremrangebyrank(queue, 0, last).exec_p();
4
+ if (redis.zrange) {
5
+ const [items] = await new Promise(function (resolve, reject) {
6
+ redis.multi().zrange(queue, 0, last).zremrangebyrank(queue, 0, last).exec(function (error, out) {
7
+ error ? reject(error) : resolve(out);
8
+ });
9
+ });
10
+ return items;
11
+ }
12
+ // redis@4
13
+ const [items] = await redis.multi().ZRANGE(queue, 0, last).ZREMRANGEBYRANK(queue, 0, last).exec();
6
14
  return items;
7
15
  }
8
16
 
@@ -0,0 +1,58 @@
1
+ const assert = require('assert');
2
+ const redis_zshift = require('./redis_zshift');
3
+
4
+ const REDIS_HOST = '127.0.0.1';
5
+ const REDIS_PORT = 6379;
6
+ const REDIS_AUTH = null;
7
+ const REDIS_QUEUE = 'TESTING';
8
+
9
+ describe('redis_zshift', function () {
10
+ it('should work with redis@1', async function () {
11
+ await test_1_2_3(require('redis-v1'));
12
+ });
13
+ it('should work with redis@2', async function () {
14
+ await test_1_2_3(require('redis-v2'));
15
+ });
16
+ it('should work with redis@3', async function () {
17
+ await test_1_2_3(require('redis-v3'));
18
+ });
19
+ it('should work with redis@4', async function () {
20
+ await test_4(require('redis-v4'));
21
+ });
22
+ });
23
+
24
+ async function test_1_2_3(redis)
25
+ {
26
+ const r = redis.createClient(REDIS_PORT, REDIS_HOST, {password: REDIS_AUTH || undefined});
27
+ try {
28
+ const expected = 'aa,bb,cc,dd,ee'.split(',');
29
+ await new Promise(function (resolve, reject) {
30
+ r.del(REDIS_QUEUE, (e,v) => e ? reject(e) : resolve(v));
31
+ });
32
+ await new Promise(function (resolve, reject) {
33
+ const args = [REDIS_QUEUE, ...expected.map((v,i) => [i,v]).flat()];
34
+ r.zadd(args, (e,v) => e ? reject(e) : resolve(v));
35
+ });
36
+ const actual = await redis_zshift(r, REDIS_QUEUE, expected.length);
37
+ await assert.deepStrictEqual(actual, expected);
38
+ }
39
+ finally {
40
+ await r.quit();
41
+ }
42
+ }
43
+
44
+ async function test_4(redis)
45
+ {
46
+ const r = redis.createClient(REDIS_PORT, REDIS_HOST, {password: REDIS_AUTH || undefined});
47
+ await r.connect();
48
+ try {
49
+ const expected = 'aa,bb,cc,dd,ee'.split(',');
50
+ await r.DEL(REDIS_QUEUE);
51
+ await r.ZADD(REDIS_QUEUE, expected.map((value,i) => ({score: i, value})));
52
+ const actual = await redis_zshift(r, REDIS_QUEUE, 5);
53
+ await assert.deepStrictEqual(actual, expected);
54
+ }
55
+ finally {
56
+ await r.quit();
57
+ }
58
+ }