@vbarbarosh/node-helpers 3.26.0 → 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/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/package.json
CHANGED
|
@@ -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
|
{
|