@vbarbarosh/node-helpers 3.16.0 → 3.17.1
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/stream_strpbrk.js +70 -0
- package/src/stream_strpbrk.test.js +28 -0
package/package.json
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const stream = require('stream');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Split input stream by consecutive delimiters in `chars`.
|
|
5
|
+
*/
|
|
6
|
+
function stream_strpbrk(chars = '\r\n')
|
|
7
|
+
{
|
|
8
|
+
const pending = [];
|
|
9
|
+
return new stream.Transform({
|
|
10
|
+
objectMode: true,
|
|
11
|
+
transform: function (str, encoding, next) {
|
|
12
|
+
str = Buffer.isBuffer(str) ? str.toString() : str;
|
|
13
|
+
|
|
14
|
+
for (let off = 0; off < str.length; ) {
|
|
15
|
+
// skip consecutive delimiters
|
|
16
|
+
while (off < str.length) {
|
|
17
|
+
let stop = true;
|
|
18
|
+
for (let i = 0, end = chars.length; i < end; ++i) {
|
|
19
|
+
if (str[off] === chars[i]) {
|
|
20
|
+
off++;
|
|
21
|
+
stop = false;
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (stop) {
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// find next delimiter
|
|
31
|
+
const delims = [];
|
|
32
|
+
for (let i = 0, end = chars.length; i < end; ++i) {
|
|
33
|
+
const j = str.indexOf(chars[i], off);
|
|
34
|
+
if (j !== -1) {
|
|
35
|
+
delims.push(j);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// no delimiters were found
|
|
40
|
+
if (!delims.length) {
|
|
41
|
+
const tmp = str.slice(off);
|
|
42
|
+
if (tmp.length) {
|
|
43
|
+
pending.push(tmp);
|
|
44
|
+
}
|
|
45
|
+
next();
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
delims.sort(fcmp);
|
|
50
|
+
pending.push(str.slice(off, delims[0]));
|
|
51
|
+
this.push(''.concat(...pending.splice(0)));
|
|
52
|
+
off = delims[0];
|
|
53
|
+
}
|
|
54
|
+
next();
|
|
55
|
+
},
|
|
56
|
+
flush: function (next) {
|
|
57
|
+
if (pending.length) {
|
|
58
|
+
this.push(''.concat(...pending.splice(0)));
|
|
59
|
+
}
|
|
60
|
+
next();
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function fcmp(a, b)
|
|
66
|
+
{
|
|
67
|
+
return a - b;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = stream_strpbrk;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const stream = require('stream');
|
|
3
|
+
const stream_strpbrk = require('./stream_strpbrk');
|
|
4
|
+
|
|
5
|
+
const items = [
|
|
6
|
+
['empty', '|', [], []],
|
|
7
|
+
['| -> empty', '|', ['|'], []],
|
|
8
|
+
['|| -> empty', '|', ['||'], []],
|
|
9
|
+
['||| -> empty', '|', ['|', '|', '|'], []],
|
|
10
|
+
['a → a', '|', ['a'], ['a']],
|
|
11
|
+
['|a → a', '|', ['|a'], ['a']],
|
|
12
|
+
['a| → a', '|', ['a|'], ['a']],
|
|
13
|
+
['a|b → a,b', '|', ['a|b'], ['a', 'b']],
|
|
14
|
+
['a|||b → a,b', '|', ['a|||b'], ['a', 'b']],
|
|
15
|
+
['abc → abc', '|', ['a', 'b', 'c'], ['abc']],
|
|
16
|
+
['a|b;c', '|;', ['a|b;c'], ['a', 'b', 'c']],
|
|
17
|
+
['Buffer(a) → a', '|', ['a'].map(Buffer.from), ['a']],
|
|
18
|
+
['Buffer(a|b) → a,b', '|', ['a|b'].map(Buffer.from), ['a', 'b']],
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
describe('stream_strpbrk', function () {
|
|
22
|
+
items.forEach(function ([title, delimiters, input, expected]) {
|
|
23
|
+
it(title, async function () {
|
|
24
|
+
const actual = await stream.Readable.from(input).pipe(stream_strpbrk(delimiters)).toArray();
|
|
25
|
+
assert.deepStrictEqual(actual, expected);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
});
|