pax_global_header00006660000000000000000000000064125402126250014511gustar00rootroot0000000000000052 comment=fd290780ed8f8a9e9452c947e7f8cd9f8fefba72 through2-filter-2.0.0/000077500000000000000000000000001254021262500145355ustar00rootroot00000000000000through2-filter-2.0.0/.gitignore000066400000000000000000000000301254021262500165160ustar00rootroot00000000000000node_modules .tern-port through2-filter-2.0.0/LICENSE000066400000000000000000000021061254021262500155410ustar00rootroot00000000000000(The MIT License) Copyright (c) Bryce B. Baril Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. through2-filter-2.0.0/README.md000066400000000000000000000045111254021262500160150ustar00rootroot00000000000000through2-filter =============== [![NPM](https://nodei.co/npm/through2-filter.png)](https://nodei.co/npm/through2-filter/) This is a super thin wrapper around [through2](http://npm.im/through2) that works like `Array.prototype.filter` but for streams. For when through2 is just too verbose :wink: Note you will **NOT** be able to alter the content of the chunks. This is intended for filtering only. If you want to modify the stream content, use either `through2` or `through2-map`. ```js var filter = require("through2-filter") var skip = filter(function (chunk) { // skip buffers longer than 100 return chunk.length < 100 }) // vs. with through2: var skip = through2(function (chunk, encoding, callback) { // skip buffers longer than 100 if (chunk.length < 100) this.push(chunk) return callback() }) // Then use your filter: source.pipe(skip).pipe(sink) // Additionally accepts `wantStrings` argument to conver buffers into strings var alphanum = new RegExp("^[A-Za-z0-1]+$") var scrub = filter({wantStrings: true}, function (str) { return alphanum.exec(str) }) // Works like `Array.prototype.filter` meaning you can specify a function that // takes up to two* arguments: fn(element, index) var skip10 = filter(function (element, index) { return index > 10 }) ``` *Differences from `Array.prototype.filter`: * No third `array` callback argument. That would require realizing the entire stream, which is generally counter-productive to stream operations. * `Array.prototype.filter` doesn't modify the source Array, which is somewhat nonsensical when applied to streams. API --- `require("through2-filter")([options], fn)` --- Create a `through2-filter` instance that will call `fn(chunk)`. If `fn(chunk)` returns "true" the chunk will be passed downstream. Otherwise it will be dropped. `require("through2-filter").ctor([options], fn)` --- Create a `through2-filter` Type that can be instantiated via `new Type()` or `Type()` to create reusable spies. `require("through2-filter").obj([options], fn)` --- Create a `through2-filter` that defaults to `objectMode = true`. `require("through2-filter").objCtor([options], fn)` --- Create a `through2-filter` Type that defaults to `objectMode = true`. Options ------- * wantStrings: Automatically call chunk.toString() for the super lazy. * all other through2 options LICENSE ======= MIT through2-filter-2.0.0/index.js000066400000000000000000000020121254021262500161750ustar00rootroot00000000000000"use strict"; module.exports = make module.exports.ctor = ctor module.exports.objCtor = objCtor module.exports.obj = obj var through2 = require("through2") var xtend = require("xtend") function ctor(options, fn) { if (typeof options == "function") { fn = options options = {} } var Filter = through2.ctor(options, function (chunk, encoding, callback) { if (this.options.wantStrings) chunk = chunk.toString() if (fn.call(this, chunk, this._index++)) this.push(chunk) return callback() }) Filter.prototype._index = 0 return Filter } function objCtor(options, fn) { if (typeof options === "function") { fn = options options = {} } options = xtend({objectMode: true, highWaterMark: 16}, options) return ctor(options, fn) } function make(options, fn) { return ctor(options, fn)() } function obj(options, fn) { if (typeof options === "function") { fn = options options = {} } options = xtend({objectMode: true, highWaterMark: 16}, options) return make(options, fn) } through2-filter-2.0.0/package.json000066400000000000000000000016701254021262500170270ustar00rootroot00000000000000{ "name": "through2-filter", "version": "2.0.0", "description": "A through2 to create an Array.prototype.filter analog for streams.", "files": [ "index.js" ], "directories": { "test": "test" }, "scripts": { "test": "node test/" }, "repository": { "type": "git", "url": "git@github.com:brycebaril/through2-filter.git" }, "keywords": [ "streams", "through", "through2", "filter" ], "author": "Bryce B. Baril", "license": "MIT", "jshintConfig": { "asi": true, "globalstrict": true, "validthis": true, "eqnull": true, "node": true, "loopfunc": true, "newcap": false, "eqeqeq": false }, "bugs": { "url": "https://github.com/brycebaril/through2-filter/issues" }, "devDependencies": { "tape": "^4.0.0", "stream-spigot": "^3.0.5", "concat-stream": "^1.4.7" }, "dependencies": { "through2": "~2.0.0", "xtend": "~4.0.0" } } through2-filter-2.0.0/test/000077500000000000000000000000001254021262500155145ustar00rootroot00000000000000through2-filter-2.0.0/test/index.js000066400000000000000000000106671254021262500171730ustar00rootroot00000000000000"use strict"; var test = require("tape").test var filter = require("../") var spigot = require("stream-spigot") var concat = require("concat-stream") test("ctor", function (t) { t.plan(2) var Filter = filter.ctor(function (record) { return !record.skip }) function combine(records) { t.equals(records.length, 3, "Correct number of remaining records") t.notOk(records.filter(function (r) { return r.skip }).length, "No remaining skip records") } spigot({objectMode: true}, [ {foo: "bar"}, {foo: "baz", skip: true}, {foo: "bif", skip: true}, {foo: "blah"}, {foo: "buzz"}, ]) .pipe(new Filter({objectMode: true})) .pipe(concat(combine)) }) test("objCtor", function (t) { t.plan(2) var Filter = filter.objCtor(function (record) { return !record.skip }) function combine(records) { t.equals(records.length, 3, "Correct number of remaining records") t.notOk(records.filter(function (r) { return r.skip }).length, "No remaining skip records") } spigot({objectMode: true}, [ {foo: "bar"}, {foo: "baz", skip: true}, {foo: "bif", skip: true}, {foo: "blah"}, {foo: "buzz"}, ]) .pipe(new Filter()) .pipe(concat(combine)) }) test("ctor options", function (t) { t.plan(7) var Filter = filter.ctor({objectMode: true, foo: "bar"}, function (record) { t.equals(this.options.foo, "bar", "Can see options") return !record.skip }) function combine(records) { t.equals(records.length, 3, "Correct number of remaining records") t.notOk(records.filter(function (r) { return r.skip }).length, "No remaining skip records") } spigot({objectMode: true}, [ {foo: "bar"}, {foo: "baz", skip: true}, {foo: "bif", skip: true}, {foo: "blah"}, {foo: "buzz"}, ]) .pipe(new Filter()) .pipe(concat(combine)) }) test("ctor buffer wantStrings", function (t) { t.plan(1) var re = new RegExp("skip") var Filter = filter.ctor({wantStrings: true}, function (chunk) { return chunk.length <= 5 }) function combine(result) { t.equals(result.toString(), "abuvwxyz", "result is correct") } spigot([ "a", "b", "cskipk", "lmnopqrstskip", "u", "vwxyz", ]).pipe(new Filter()) .pipe(concat(combine)) }) test("simple", function (t) { t.plan(2) var f = filter({objectMode: true}, function (record) { return !record.skip }) function combine(records) { t.equals(records.length, 3, "Correct number of remaining records") t.notOk(records.filter(function (r) { return r.skip }).length, "No remaining skip records") } spigot({objectMode: true}, [ {foo: "bar"}, {foo: "baz", skip: true}, {foo: "bif", skip: true}, {foo: "blah"}, {foo: "buzz"}, ]) .pipe(f) .pipe(concat(combine)) }) test("simple .obj", function (t) { t.plan(2) var f = filter.obj(function (record) { return !record.skip }) function combine(records) { t.equals(records.length, 3, "Correct number of remaining records") t.notOk(records.filter(function (r) { return r.skip }).length, "No remaining skip records") } spigot({objectMode: true}, [ {foo: "bar"}, {foo: "baz", skip: true}, {foo: "bif", skip: true}, {foo: "blah"}, {foo: "buzz"}, ]) .pipe(f) .pipe(concat(combine)) }) test("simple buffer", function (t) { t.plan(1) var f = filter({objectMode: true}, function (chunk) { return chunk.length <= 5 }) function combine(result) { t.equals(result.toString(), "abuvwxyz", "result is correct") } spigot([ "a", "b", "cdefghijk", "lmnopqrst", "u", "vwxyz", ]).pipe(f) .pipe(concat(combine)) }) test("simple buffer wantStrings", function (t) { t.plan(1) var re = new RegExp("skip") var f = filter({wantStrings: true}, function (chunk) { return chunk.length <= 5 }) function combine(result) { t.equals(result.toString(), "abuvwxyz", "result is correct") } spigot([ "a", "b", "cskipk", "lmnopqrstskip", "u", "vwxyz", ]).pipe(f) .pipe(concat(combine)) }) test("simple index", function (t) { t.plan(1) var f = filter({objectMode: true}, function (record, index) { return index < 2 }) function combine(records) { t.deepEquals(records, [{foo: "bar"},{foo: "baz"}], "Expected content") } spigot({objectMode: true}, [ {foo: "bar"}, {foo: "baz"}, {foo: "bif"}, {foo: "blah"}, {foo: "buzz"}, ]) .pipe(f) .pipe(concat(combine)) })