barnard59-base 2.0.1 → 2.1.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/filter.js +4 -4
- package/package.json +1 -1
- package/test/filter.test.js +27 -0
package/CHANGELOG.md
CHANGED
package/filter.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { obj } from 'through2'
|
|
2
2
|
|
|
3
3
|
function filter(func) {
|
|
4
|
-
return obj(
|
|
4
|
+
return obj((chunk, encoding, callback) => {
|
|
5
5
|
Promise.resolve().then(() => {
|
|
6
|
-
return func(chunk, encoding)
|
|
6
|
+
return func.call(this, chunk, encoding)
|
|
7
7
|
}).then(result => {
|
|
8
8
|
if (result) {
|
|
9
|
-
|
|
9
|
+
return callback(null, chunk)
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
callback()
|
|
12
|
+
return callback()
|
|
13
13
|
}).catch(callback)
|
|
14
14
|
})
|
|
15
15
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { deepStrictEqual } from 'assert'
|
|
2
|
+
import { array } from 'get-stream'
|
|
3
|
+
import { Readable } from 'readable-stream'
|
|
4
|
+
import filter from '../filter.js'
|
|
5
|
+
|
|
6
|
+
describe('filter', () => {
|
|
7
|
+
it('should pass pipeline context to callback function', async () => {
|
|
8
|
+
const input = new Readable({
|
|
9
|
+
objectMode: true,
|
|
10
|
+
read: () => {
|
|
11
|
+
input.push('a')
|
|
12
|
+
input.push('b')
|
|
13
|
+
input.push(null)
|
|
14
|
+
},
|
|
15
|
+
})
|
|
16
|
+
const context = {
|
|
17
|
+
variable: new Map().set('condition', 'a'),
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const outStream = input.pipe(filter.call(context, function (chunk) {
|
|
21
|
+
return this.variable.get('condition') === chunk
|
|
22
|
+
}))
|
|
23
|
+
const output = await array(outStream)
|
|
24
|
+
|
|
25
|
+
deepStrictEqual(output, ['a'])
|
|
26
|
+
})
|
|
27
|
+
})
|