@vbarbarosh/node-helpers 3.25.0 → 3.26.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": "3.25.0",
5
+ "version": "3.26.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
@@ -0,0 +1,27 @@
1
+ // https://stackoverflow.com/a/37580979/23502239
2
+ function array_permutations(array)
3
+ {
4
+ var length = array.length,
5
+ out = [array.slice()],
6
+ c = new Array(length).fill(0),
7
+ i = 1, k, p;
8
+
9
+ while (i < length) {
10
+ if (c[i] < i) {
11
+ k = i % 2 && c[i];
12
+ p = array[i];
13
+ array[i] = array[k];
14
+ array[k] = p;
15
+ ++c[i];
16
+ i = 1;
17
+ out.push(array.slice());
18
+ }
19
+ else {
20
+ c[i] = 0;
21
+ ++i;
22
+ }
23
+ }
24
+ return out;
25
+ }
26
+
27
+ module.exports = array_permutations;
@@ -0,0 +1,10 @@
1
+ const array_permutations = require('./array_permutations');
2
+ const assert = require('assert');
3
+
4
+ describe('array_permutations', function () {
5
+ it('should handle basic input', function () {
6
+ assert.deepStrictEqual(array_permutations([]), [[]]);
7
+ assert.deepStrictEqual(array_permutations([1,2]), [[1,2],[2,1]]);
8
+ assert.deepStrictEqual(array_permutations([1,2,3]), [[1,2,3],[2,1,3],[3,1,2],[1,3,2],[2,3,1],[3,2,1]]);
9
+ });
10
+ });
@@ -29,7 +29,7 @@ function shell_spawn(args, options)
29
29
  }
30
30
  function promise_exit(code, signal) {
31
31
  out.off('error', promise_error);
32
- resolve({code, signal});
32
+ code ? reject(new Error(`Process terminated with code ${code}`)) : resolve(signal);
33
33
  }
34
34
  });
35
35
  };
@@ -11,7 +11,20 @@ function stream_strpbrk(chars = '\r\n')
11
11
  transform: function (str, encoding, next) {
12
12
  str = Buffer.isBuffer(str) ? str.toString() : str;
13
13
 
14
- for (let off = 0; off < str.length; ) {
14
+ let off = 0;
15
+ // leading delimiters
16
+ while (off < str.length) {
17
+ if (!chars.includes(str[off])) {
18
+ break;
19
+ }
20
+ off++;
21
+ }
22
+
23
+ if (off && pending.length) {
24
+ this.push(''.concat(...pending.splice(0)));
25
+ }
26
+
27
+ while (off < str.length) {
15
28
  // skip consecutive delimiters
16
29
  while (off < str.length) {
17
30
  let stop = true;
@@ -14,6 +14,7 @@ const items = [
14
14
  ['a|||b → a,b', '|', ['a|||b'], ['a', 'b']],
15
15
  ['abc → abc', '|', ['a', 'b', 'c'], ['abc']],
16
16
  ['a|b;c', '|;', ['a|b;c'], ['a', 'b', 'c']],
17
+ ['a|b;c', '|;', ['a|b', ';c'], ['a', 'b', 'c']],
17
18
  ['Buffer(a) → a', '|', ['a'].map(Buffer.from), ['a']],
18
19
  ['Buffer(a|b) → a,b', '|', ['a|b'].map(Buffer.from), ['a', 'b']],
19
20
  ];