@vbarbarosh/node-helpers 3.25.1 → 3.27.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 +1 -1
- package/src/array_permutations.js +27 -0
- package/src/array_permutations.test.js +10 -0
- package/src/const_type.js +17 -0
- package/src/gettype.js +28 -0
- package/src/gettype.test.js +27 -0
- package/src/object_walk_preorder.js +23 -0
- package/src/object_walk_preorder.test.js +17 -0
- package/src/stream_progress.js +2 -0
- package/src/stream_strpbrk.js +14 -1
- package/src/stream_strpbrk.test.js +1 -0
package/package.json
CHANGED
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const const_type = {
|
|
2
|
+
null: 'null',
|
|
3
|
+
undefined: 'undefined',
|
|
4
|
+
boolean: 'boolean',
|
|
5
|
+
number: 'number',
|
|
6
|
+
bigint: 'bigint',
|
|
7
|
+
nan: 'nan',
|
|
8
|
+
neg_inf: '-inf',
|
|
9
|
+
pos_inf: '+inf',
|
|
10
|
+
string: 'string',
|
|
11
|
+
symbol: 'symbol',
|
|
12
|
+
function: 'function',
|
|
13
|
+
object: 'object',
|
|
14
|
+
array: 'array',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
module.exports = const_type;
|
package/src/gettype.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const const_type = require('./const_type');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param value
|
|
6
|
+
* @return {"undefined"|"object"|"boolean"|"number"|"string"|"function"|"symbol"|"bigint"|string}
|
|
7
|
+
*/
|
|
8
|
+
function gettype(value)
|
|
9
|
+
{
|
|
10
|
+
if (value === null) {
|
|
11
|
+
return const_type.null;
|
|
12
|
+
}
|
|
13
|
+
if (Array.isArray(value)) {
|
|
14
|
+
return const_type.array;
|
|
15
|
+
}
|
|
16
|
+
if (Number.isNaN(value)) {
|
|
17
|
+
return const_type.nan;
|
|
18
|
+
}
|
|
19
|
+
if (value === Number.NEGATIVE_INFINITY) {
|
|
20
|
+
return const_type.neg_inf;
|
|
21
|
+
}
|
|
22
|
+
if (value === Number.POSITIVE_INFINITY) {
|
|
23
|
+
return const_type.pos_inf;
|
|
24
|
+
}
|
|
25
|
+
return typeof value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
module.exports = gettype;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const const_type = require('./const_type');
|
|
3
|
+
const gettype = require('./gettype');
|
|
4
|
+
|
|
5
|
+
const items = [
|
|
6
|
+
['null', null, const_type.null],
|
|
7
|
+
['undefined', undefined, const_type.undefined],
|
|
8
|
+
['boolean', true, const_type.boolean],
|
|
9
|
+
['number', 1, const_type.number],
|
|
10
|
+
['bigint', 1n, const_type.bigint],
|
|
11
|
+
['nan', NaN, const_type.nan],
|
|
12
|
+
['-inf', Number.NEGATIVE_INFINITY, const_type.neg_inf],
|
|
13
|
+
['+inf', Number.POSITIVE_INFINITY, const_type.pos_inf],
|
|
14
|
+
['string', 's', const_type.string],
|
|
15
|
+
['symbol', Symbol(), const_type.symbol],
|
|
16
|
+
['function', () => 1, const_type.function],
|
|
17
|
+
['object', {}, const_type.object],
|
|
18
|
+
['array', [], const_type.array],
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
describe('gettype', function () {
|
|
22
|
+
items.forEach(function ([title, input, expected]) {
|
|
23
|
+
it(title, function () {
|
|
24
|
+
assert.deepStrictEqual(gettype(input), expected);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
function object_walk_preorder(value, fn, path = [])
|
|
2
|
+
{
|
|
3
|
+
if (path.length) {
|
|
4
|
+
fn(value, path);
|
|
5
|
+
}
|
|
6
|
+
if (value === null) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
if (Array.isArray(value)) {
|
|
10
|
+
for (let i = 0, end = value.length; i < end; ++i) {
|
|
11
|
+
object_walk_preorder(value[i], fn, path.concat('*'));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
else if (typeof value === 'object') {
|
|
15
|
+
const keys = Object.keys(value);
|
|
16
|
+
for (let i = 0, end = keys.length; i < end; ++i) {
|
|
17
|
+
const key = keys[i];
|
|
18
|
+
object_walk_preorder(value[key], fn, path.concat(key));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = object_walk_preorder;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const object_walk_preorder = require('./object_walk_preorder');
|
|
3
|
+
|
|
4
|
+
describe('object_walk_preorder', function () {
|
|
5
|
+
it('basic 1', function () {
|
|
6
|
+
const actual = [];
|
|
7
|
+
const expected = ['aaa', 'bbb', 'ccc'];
|
|
8
|
+
object_walk_preorder({aaa: 1, bbb: 2, ccc: 3}, (v,p) => actual.push(p.join('/')));
|
|
9
|
+
assert.deepStrictEqual(actual, expected);
|
|
10
|
+
});
|
|
11
|
+
it('basic 2', function () {
|
|
12
|
+
const actual = [];
|
|
13
|
+
const expected = ['aaa', 'aaa/foo', 'aaa/foo/bar', 'aaa/foo/bar/baz', 'bbb', 'ccc'];
|
|
14
|
+
object_walk_preorder({aaa: {foo: {bar: {baz: 1}}}, bbb: 2, ccc: 3}, (v,p) => actual.push(p.join('/')));
|
|
15
|
+
assert.deepStrictEqual(actual, expected);
|
|
16
|
+
});
|
|
17
|
+
});
|
package/src/stream_progress.js
CHANGED
|
@@ -4,6 +4,8 @@ const stream = require('stream');
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Monitor the progress of data through a pipe. Similar to UNIX `pv` command.
|
|
7
|
+
*
|
|
8
|
+
* @similar https://www.npmjs.com/package/progress-stream
|
|
7
9
|
*/
|
|
8
10
|
function stream_progress({total, user_friendly_status = s => console.log(s)})
|
|
9
11
|
{
|
package/src/stream_strpbrk.js
CHANGED
|
@@ -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
|
-
|
|
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
|
];
|