barnard59-base 2.3.0 → 2.4.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/CHANGELOG.md +6 -0
- package/batch.js +26 -0
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/manifest.ttl +8 -1
- package/package.json +2 -2
- package/test/batch.test.js +59 -0
- /package/{nul.d.ts → dev-nul.d.ts} +0 -0
- /package/{nul.js → dev-nul.js} +0 -0
package/CHANGELOG.md
CHANGED
package/batch.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Transform } from 'readable-stream'
|
|
2
|
+
|
|
3
|
+
function batch(size = 0) {
|
|
4
|
+
let array = []
|
|
5
|
+
const maxSize = Number(size)
|
|
6
|
+
return new Transform({
|
|
7
|
+
readableObjectMode: true,
|
|
8
|
+
writableObjectMode: true,
|
|
9
|
+
readableHighWaterMark: maxSize,
|
|
10
|
+
writableHighWaterMark: maxSize,
|
|
11
|
+
transform(item, _encoding, callback) {
|
|
12
|
+
array.push(item)
|
|
13
|
+
if (array.length === maxSize) {
|
|
14
|
+
this.push(array)
|
|
15
|
+
array = []
|
|
16
|
+
}
|
|
17
|
+
callback()
|
|
18
|
+
},
|
|
19
|
+
flush(callback) {
|
|
20
|
+
if (array.length > 0) this.push(array)
|
|
21
|
+
callback()
|
|
22
|
+
},
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default batch
|
package/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export { default as forEach } from "./forEach.js";
|
|
|
5
5
|
export { default as glob } from "./glob.js";
|
|
6
6
|
export { default as limit } from "./limit.js";
|
|
7
7
|
export { default as map } from "./map.js";
|
|
8
|
-
export { default as nul } from "./nul.js";
|
|
8
|
+
export { default as nul } from "./dev-nul.js";
|
|
9
9
|
export { default as offset } from "./offset.js";
|
|
10
10
|
export * from "./process.js";
|
|
11
11
|
export { default as toString } from "./toString.js";
|
package/index.js
CHANGED
|
@@ -7,7 +7,7 @@ export { default as glob } from './glob.js'
|
|
|
7
7
|
export { parse as jsonParse, stringify as jsonStringify } from './json.js'
|
|
8
8
|
export { default as limit } from './limit.js'
|
|
9
9
|
export { default as map } from './map.js'
|
|
10
|
-
export { default as nul } from './nul.js'
|
|
10
|
+
export { default as nul } from './dev-nul.js'
|
|
11
11
|
export { default as offset } from './offset.js'
|
|
12
12
|
export * from './process.js'
|
|
13
13
|
export { default as toString } from './toString.js'
|
package/manifest.ttl
CHANGED
|
@@ -31,6 +31,13 @@
|
|
|
31
31
|
code:link <node:barnard59-base/filter.js#default>
|
|
32
32
|
].
|
|
33
33
|
|
|
34
|
+
<batch> a p:Operation, p:WritableObjectMode, p:ReadableObjectMode;
|
|
35
|
+
rdfs:label "Batch";
|
|
36
|
+
rdfs:comment "Groups incoming items into arrays of the given size.";
|
|
37
|
+
code:implementedBy [ a code:EcmaScriptModule;
|
|
38
|
+
code:link <node:barnard59-base/batch.js#default>
|
|
39
|
+
].
|
|
40
|
+
|
|
34
41
|
<flatten> a p:Operation, p:WritableObjectMode, p:ReadableObjectMode;
|
|
35
42
|
rdfs:label "Flatten";
|
|
36
43
|
rdfs:comment "Separates incoming arrays into their elements and emits each element as a single chunk.";
|
|
@@ -84,7 +91,7 @@
|
|
|
84
91
|
rdfs:label "/dev/null";
|
|
85
92
|
rdfs:comment "Dummy output stream, just like /dev/null.";
|
|
86
93
|
code:implementedBy [ a code:EcmaScriptModule;
|
|
87
|
-
code:link <node:barnard59-base/nul.js#default>
|
|
94
|
+
code:link <node:barnard59-base/dev-nul.js#default>
|
|
88
95
|
].
|
|
89
96
|
|
|
90
97
|
<offset> a p:Operation, p:WritableObjectMode, p:ReadableObjectMode;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "barnard59-base",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "Linked Data pipelines",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"@types/readable-stream": "^4.0.10",
|
|
40
40
|
"@types/readable-to-readable": "^0.1.0",
|
|
41
41
|
"@types/through2": "^2.0.41",
|
|
42
|
-
"barnard59-core": "^5.3.
|
|
42
|
+
"barnard59-core": "^5.3.1",
|
|
43
43
|
"chai": "^4.3.10",
|
|
44
44
|
"get-stream": "^6.0.1",
|
|
45
45
|
"into-stream": "^7.0.0",
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { deepStrictEqual, strictEqual } from 'node:assert'
|
|
2
|
+
import { array } from 'get-stream'
|
|
3
|
+
import { isReadableStream as isReadable, isWritableStream as isWritable } from 'is-stream'
|
|
4
|
+
import { Readable } from 'readable-stream'
|
|
5
|
+
import batch from '../batch.js'
|
|
6
|
+
|
|
7
|
+
describe('batch', () => {
|
|
8
|
+
it('should be a function', () => {
|
|
9
|
+
strictEqual(typeof batch, 'function')
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('should return a duplex stream', async () => {
|
|
13
|
+
const result = batch()
|
|
14
|
+
|
|
15
|
+
strictEqual(isWritable(result), true)
|
|
16
|
+
strictEqual(isReadable(result), true)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('should do nothing if there are no input chunks', async () => {
|
|
20
|
+
const input = Readable({
|
|
21
|
+
objectMode: true,
|
|
22
|
+
read: () => {
|
|
23
|
+
input.push(null)
|
|
24
|
+
},
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const result = await array(input.pipe(batch()))
|
|
28
|
+
|
|
29
|
+
deepStrictEqual(result, [])
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('should split input in batches', async () => {
|
|
33
|
+
const expected = [['a', 'b'], ['c', 'd'], ['e']]
|
|
34
|
+
const input = Readable({
|
|
35
|
+
objectMode: true,
|
|
36
|
+
read: () => {
|
|
37
|
+
['a', 'b', 'c', 'd', 'e', null].forEach(item => input.push(item))
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const result = await array(input.pipe(batch(2)))
|
|
42
|
+
|
|
43
|
+
deepStrictEqual(result, expected)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('should emit a single batch', async () => {
|
|
47
|
+
const expected = [['a', 'b', 'c', 'd', 'e']]
|
|
48
|
+
const input = Readable({
|
|
49
|
+
objectMode: true,
|
|
50
|
+
read: () => {
|
|
51
|
+
['a', 'b', 'c', 'd', 'e', null].forEach(item => input.push(item))
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
const result = await array(input.pipe(batch(0)))
|
|
56
|
+
|
|
57
|
+
deepStrictEqual(result, expected)
|
|
58
|
+
})
|
|
59
|
+
})
|
|
File without changes
|
/package/{nul.js → dev-nul.js}
RENAMED
|
File without changes
|