You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. module.exports = make
  3. module.exports.ctor = ctor
  4. module.exports.objCtor = objCtor
  5. module.exports.obj = obj
  6. var through2 = require("through2")
  7. var xtend = require("xtend")
  8. function ctor(options, fn) {
  9. if (typeof options == "function") {
  10. fn = options
  11. options = {}
  12. }
  13. var Filter = through2.ctor(options, function (chunk, encoding, callback) {
  14. if (this.options.wantStrings) chunk = chunk.toString()
  15. try {
  16. if (fn.call(this, chunk, this._index++)) this.push(chunk)
  17. return callback()
  18. } catch (e) {
  19. return callback(e)
  20. }
  21. })
  22. Filter.prototype._index = 0
  23. return Filter
  24. }
  25. function objCtor(options, fn) {
  26. if (typeof options === "function") {
  27. fn = options
  28. options = {}
  29. }
  30. options = xtend({objectMode: true, highWaterMark: 16}, options)
  31. return ctor(options, fn)
  32. }
  33. function make(options, fn) {
  34. return ctor(options, fn)()
  35. }
  36. function obj(options, fn) {
  37. if (typeof options === "function") {
  38. fn = options
  39. options = {}
  40. }
  41. options = xtend({objectMode: true, highWaterMark: 16}, options)
  42. return make(options, fn)
  43. }