@vbarbarosh/node-helpers 3.26.0 → 3.28.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.26.0",
5
+ "version": "3.28.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
@@ -26,6 +26,7 @@
26
26
  "axios": "^1.3.2",
27
27
  "bluebird": "^3.7.2",
28
28
  "form-data": "^4.0.0",
29
+ "htmlparser2": "^9.1.0",
29
30
  "json-stable-stringify": "^1.0.2",
30
31
  "sanitize-filename": "^1.6.3",
31
32
  "split": "^1.0.1",
@@ -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,26 @@
1
+ const const_type = require('./const_type');
2
+
3
+ /**
4
+ * @similar https://github.com/locutusjs/locutus/tree/master/src/php/var
5
+ */
6
+ function gettype(value)
7
+ {
8
+ if (value === null) {
9
+ return const_type.null;
10
+ }
11
+ if (Array.isArray(value)) {
12
+ return const_type.array;
13
+ }
14
+ if (Number.isNaN(value)) {
15
+ return const_type.nan;
16
+ }
17
+ if (value === Number.NEGATIVE_INFINITY) {
18
+ return const_type.neg_inf;
19
+ }
20
+ if (value === Number.POSITIVE_INFINITY) {
21
+ return const_type.pos_inf;
22
+ }
23
+ return typeof value;
24
+ }
25
+
26
+ 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
+ });
@@ -0,0 +1,67 @@
1
+ const stream = require('stream');
2
+ const htmlparser2 = require('htmlparser2');
3
+
4
+ /**
5
+ * Parse XML stream and produce objects.
6
+ */
7
+ function stream_parse_xml(selector, mapper = stream_parse_xml.makeobj)
8
+ {
9
+ let out;
10
+ let elem = {name: '#doc', parent: null, attrs: {}, text: [], children: [], raw: []};
11
+
12
+ const path = [];
13
+ const selector_str = selector.join('>');
14
+ let path_str = '';
15
+
16
+ const options = {xmlMode: true};
17
+ const events = {
18
+ onopentag: function (name, attrs) {
19
+ elem = {name, parent: elem, attrs, text: [], children: [], raw: []};
20
+ if (path.length > 1) {
21
+ elem.parent.raw.push(elem);
22
+ elem.parent.children.push(elem);
23
+ }
24
+ path.push(elem.name);
25
+ path_str = path.join('>');
26
+ },
27
+ onclosetag: function (name) {
28
+ if (path_str === selector_str) {
29
+ out.push(mapper(elem));
30
+ }
31
+ elem = elem.parent;
32
+ path.pop();
33
+ path_str = path.join('>');
34
+ },
35
+ ontext: function (text) {
36
+ elem.raw.push(text);
37
+ elem.text.push(text);
38
+ },
39
+ onend: function () {
40
+ if (selector_str === '') {
41
+ out.push(mapper(elem));
42
+ }
43
+ },
44
+ };
45
+
46
+ const parser = new htmlparser2.Parser(events, options);
47
+ return out = new stream.Transform({
48
+ objectMode: true,
49
+ transform: function (buf, encoding, next) {
50
+ parser.write(buf.toString());
51
+ next();
52
+ },
53
+ });
54
+ }
55
+
56
+ stream_parse_xml.makeobj = function (node) {
57
+ if (node.children.length === 0) {
58
+ return node.text.join('').trim();
59
+ }
60
+ const out = {};
61
+ node.children.forEach(function (child) {
62
+ out[child.name] = stream_parse_xml.makeobj(child);
63
+ });
64
+ return out;
65
+ }
66
+
67
+ module.exports = stream_parse_xml;
@@ -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
  {