@vbarbarosh/node-helpers 3.16.0 → 3.17.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.16.0",
5
+ "version": "3.17.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
@@ -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
+ decodeStrings: true,
12
+ transform: function (str, encoding, next) {
13
+ for (let off = 0; off < str.length; ) {
14
+
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,26 @@
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
+ ];
18
+
19
+ describe('stream_strpbrk', function () {
20
+ items.forEach(function ([title, delimiters, input, expected]) {
21
+ it(title, async function () {
22
+ const actual = await stream.Readable.from(input).pipe(stream_strpbrk(delimiters)).toArray();
23
+ assert.deepStrictEqual(actual, expected);
24
+ });
25
+ });
26
+ });