-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (37 loc) · 1.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var Transform = require('readable-stream/transform')
var Promise = require('native-or-bluebird')
var inherits = require('util').inherits
var assert = require('assert')
module.exports = map
function map(transform, flush) {
assert(typeof transform === 'function')
var stream = new Transform({
objectMode: true
})
stream._transform = fnToTransform(transform)
if (typeof flush === 'function') stream._flush = fnToFlush(flush)
return stream
}
map.create = function (transform, flush) {
assert(typeof transform === 'function')
function Stream(options) {
if (!(this instanceof Stream)) return new Stream(options)
options = options || {}
if (!('objectMode' in options)) options.objectMode = true
Transform.call(this, options)
}
inherits(Stream, Transform)
Stream.prototype._transform = fnToTransform(transform)
if (typeof flush === 'function') Stream.prototype._flush = fnToFlush(flush)
return Stream
}
function fnToTransform(fn) {
return function (doc, NULL, cb) {
Promise.resolve(fn.call(this, doc)).then(cb.bind(null, null), cb)
}
}
function fnToFlush(fn) {
return function (cb) {
Promise.resolve(fn.call(this)).then(cb.bind(null, null), cb)
}
}