Comparing version 0.1.3 to 0.1.4
@@ -109,2 +109,22 @@ // Copyright 2014. A Medium Corporation | ||
/** | ||
* Set the stream to ignore undefined values when they are written. This helps avoid accidentally | ||
* signaling the end of a stream. | ||
* | ||
* @return {Mapper} | ||
*/ | ||
Mapper.prototype.ignoreUndefined = function () { | ||
this._ignoringUndefined = true | ||
return this | ||
} | ||
/** | ||
* Determing whether the stream is ignoring undefined values. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
Mapper.prototype.isIgnoringUndefined = function () { | ||
return !! this._ignoringUndefined | ||
} | ||
/** | ||
* Wrapper for the core readable stream's push method. Allows pushing multiple values in | ||
@@ -115,2 +135,3 @@ * a single call for streams in multi mode. | ||
* @param {*} data Data to push. Should be an array when the stream is in multi mode. | ||
* @return {Boolean} | ||
*/ | ||
@@ -120,8 +141,18 @@ Mapper.prototype.push = function (data) { | ||
// If we're in multi-mode but get undefined while we're ignoring undefined values, bail. | ||
// In this case we don't give Node a chance to tell us whether or not the stream is in a | ||
// state to accept more pushes. Since we didn't add any data to the buffer, it's as safe | ||
// to push more data as it was to push this one. If it's not safe to push more data then | ||
// it wasn't safe to push this data, so you're probably ignoring this value anyway so | ||
// who cares what we return. ¯\_(ツ)_/¯ | ||
if (this._shouldSkipPush(data)) { | ||
return true | ||
} | ||
// The null condition here is because internally Node writes a null value to indicate | ||
// the end of the stream. | ||
if (! this.isMulti() || data === null) { | ||
push.call(this, data) | ||
return push.call(this, data) | ||
} else { | ||
data.forEach(push.bind(this)) | ||
return data.every(push.bind(this)) | ||
} | ||
@@ -131,2 +162,12 @@ } | ||
/** | ||
* Determine whether we should skip the push step for this chunk of data. | ||
* | ||
* @param {*} data | ||
* @return {Boolean} | ||
*/ | ||
Mapper.prototype._shouldSkipPush = function (data) { | ||
return data === undefined && this.isIgnoringUndefined() | ||
} | ||
/** | ||
* Create a Mapper stream. | ||
@@ -133,0 +174,0 @@ * @param {Function} mapper |
{ | ||
"name": "sculpt", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Generate Node 0.10-friendly transform streams to manipulate other streams.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -98,3 +98,22 @@ # Sculpt | ||
Map streams can be set to ignore values that are `undefined`. Ordinarily Node.js treats `null`-ish | ||
values (including `undefined`) as signaling the end of a stream. In some cases it's useful to be | ||
able to avoid pushing data for some inputs without having a separate stream to filter the data — for | ||
example, cases where deciding whether you want to push data requires expensive computation. In | ||
those cases, you can set the stream to ignore `undefined` values. | ||
```javascript | ||
var stream = sculpt.map(function (chunk) { | ||
if (chunk === 'hello') return | ||
return chunk | ||
}).ignoreUndefined() | ||
stream.pipe(process.stdout) | ||
stream.write('hello') | ||
strea.write('world') | ||
// world | ||
``` | ||
### Filter | ||
@@ -101,0 +120,0 @@ |
@@ -120,2 +120,24 @@ // Copyright 2014. A Medium Corporation | ||
}) | ||
it('Should be able to ignore undefined values', function (done) { | ||
var stream = map(function (num) { | ||
return num % 2 ? num : undefined | ||
}).ignoreUndefined() | ||
var collector = collect() | ||
stream.on('error', done) | ||
collector.on('error', done) | ||
stream.pipe(collector) | ||
stream.write(1) | ||
stream.write(2) | ||
stream.write(3) | ||
stream.write(4) | ||
stream.end() | ||
collector.on('end', function () { | ||
assert.deepEqual([1, 3], collector.getObjects()) | ||
done() | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
929
353
2
45653
33