@vbarbarosh/node-helpers 3.27.0 → 3.29.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.
|
|
5
|
+
"version": "3.29.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",
|
package/src/gettype.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
const const_type = require('./const_type');
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* @param value
|
|
6
|
-
* @return {"undefined"|"object"|"boolean"|"number"|"string"|"function"|"symbol"|"bigint"|string}
|
|
4
|
+
* @similar https://github.com/locutusjs/locutus/tree/master/src/php/var
|
|
7
5
|
*/
|
|
8
6
|
function gettype(value)
|
|
9
7
|
{
|
|
@@ -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;
|
|
@@ -3,13 +3,13 @@ const stream = require('stream');
|
|
|
3
3
|
/**
|
|
4
4
|
* Call `fn` on each item.
|
|
5
5
|
*/
|
|
6
|
-
function
|
|
6
|
+
function stream_through(fn)
|
|
7
7
|
{
|
|
8
|
-
return stream.Transform({
|
|
8
|
+
return new stream.Transform({
|
|
9
9
|
objectMode: true,
|
|
10
|
-
transform:
|
|
10
|
+
transform: function (item, encoding, next) {
|
|
11
11
|
try {
|
|
12
|
-
|
|
12
|
+
fn(item);
|
|
13
13
|
this.push(item, encoding);
|
|
14
14
|
next();
|
|
15
15
|
}
|
|
@@ -20,4 +20,4 @@ function stream_each(fn)
|
|
|
20
20
|
});
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
module.exports =
|
|
23
|
+
module.exports = stream_through;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const stream = require('stream');
|
|
2
|
+
|
|
3
|
+
function stream_transform(fn)
|
|
4
|
+
{
|
|
5
|
+
return new stream.Transform({
|
|
6
|
+
objectMode: true,
|
|
7
|
+
transform: function (item, encoding, next) {
|
|
8
|
+
try {
|
|
9
|
+
this.push(fn(item));
|
|
10
|
+
next();
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
next(error);
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = stream_transform;
|