├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package-lock.json ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .tern-port 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) Bryce B. Baril 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | through2-map 2 | ============ 3 | 4 | [![NPM](https://nodei.co/npm/through2-map.png)](https://nodei.co/npm/through2-map/) 5 | 6 | This is a super thin wrapper around [through2](http://npm.im/through2) that works like `Array.prototype.map` but for streams. 7 | 8 | For when through2 is just too verbose :wink: 9 | 10 | Note you will **NOT** be able to skip chunks. This is intended for modification only. If you want filter the stream content, use either `through2` or `through2-filter`. This transform also does not have a `flush` function. 11 | 12 | **IMPORTANT:** If you return `null` from your function, the stream will end there. 13 | 14 | ```js 15 | 16 | var map = require("through2-map") 17 | 18 | var truncate = map(function (chunk) { 19 | return chunk.slice(0, 10) 20 | }) 21 | 22 | // vs. with through2: 23 | var truncate = through2(function (chunk, encoding, callback) { 24 | this.push(chunk.slice(0, 10)) 25 | return callback() 26 | }) 27 | 28 | // Then use your map: 29 | source.pipe(truncate).pipe(sink) 30 | 31 | // Additionally accepts `wantStrings` argument to convert buffers into strings 32 | var stripTags = map({wantStrings: true}, function (str) { 33 | // OMG don't actually use this 34 | return str.replace(/<.*?>/g, "") 35 | }) 36 | 37 | // Works like `Array.prototype.map` meaning you can specify a function that 38 | // takes up to two* arguments: fn(chunk, index) 39 | var spaceout = map({wantStrings: true}, function (chunk, index) { 40 | return (index % 2 == 0) ? chunk + "\n\n" : chunk 41 | }) 42 | 43 | // vs. with through2: 44 | var spaceout = through2(function (chunk, encoding, callback) { 45 | if (this.index == undefined) this.index = 0 46 | var buf = (this.index++ % 2 == 0) ? Buffer.concat(chunk, new Buffer("\n\n")) : chunk 47 | this.push(buf) 48 | return callback() 49 | }) 50 | 51 | ``` 52 | 53 | *Differences from `Array.prototype.map`: 54 | * Cannot insert `null` elements into the stream without aborting. 55 | * No third `array` callback argument. That would require realizing the entire stream, which is generally counter-productive to stream operations. 56 | * `Array.prototype.map` doesn't modify the source Array, which is somewhat nonsensical when applied to streams. 57 | 58 | API 59 | --- 60 | 61 | ``` 62 | require("through2-map")([options,] fn) 63 | ``` 64 | 65 | Create a `stream.Transform` instance that will call `fn(chunk, index)` on each stream segment. 66 | 67 | - - - 68 | 69 | ``` 70 | var Tx = require("through2-map").ctor([options,] fn) 71 | ``` 72 | 73 | Create a reusable `stream.Transform` TYPE that can be called via `new Tx` or `Tx()` to create an instance. 74 | 75 | - - - 76 | 77 | ``` 78 | require("through2-map").obj([options,] fn) 79 | ``` 80 | 81 | Create a `through2-map` instance that defaults to `objectMode: true`. 82 | 83 | - - - 84 | 85 | ``` 86 | require("through2-map").objCtor([options,] fn) 87 | ``` 88 | 89 | Just like ctor, but with `objectMode: true` defaulting to true. 90 | 91 | Options 92 | ------- 93 | 94 | * wantStrings: Automatically call chunk.toString() for the super lazy. 95 | * all other through2 options 96 | 97 | LICENSE 98 | ======= 99 | 100 | MIT 101 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = make 4 | module.exports.ctor = ctor 5 | module.exports.objCtor = objCtor 6 | module.exports.obj = obj 7 | 8 | const through2 = require("through2") 9 | 10 | function ctor(options, fn) { 11 | if (typeof options == "function") { 12 | fn = options 13 | options = {} 14 | } 15 | 16 | var Map = through2.ctor(options, function (chunk, encoding, callback) { 17 | if (this.options.wantStrings) chunk = chunk.toString() 18 | try { 19 | this.push(fn.call(this, chunk, this._index++)) 20 | return callback() 21 | } catch (e) { 22 | return callback(e) 23 | } 24 | }) 25 | Map.prototype._index = 0 26 | return Map 27 | } 28 | 29 | function make(options, fn) { 30 | return ctor(options, fn)() 31 | } 32 | 33 | function objCtor(options, fn) { 34 | if (typeof options === "function") { 35 | fn = options 36 | options = {} 37 | } 38 | options = Object.assign({objectMode: true, highWaterMark: 16}, options) 39 | return ctor(options, fn) 40 | } 41 | 42 | function obj(options, fn) { 43 | if (typeof options === "function") { 44 | fn = options 45 | options = {} 46 | } 47 | options = Object.assign({objectMode: true, highWaterMark: 16}, options) 48 | return make(options, fn) 49 | } 50 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "through2-map", 3 | "version": "4.0.0", 4 | "lockfileVersion": 3, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "through2-map", 9 | "version": "4.0.0", 10 | "license": "MIT", 11 | "dependencies": { 12 | "through2": "^4.0.2" 13 | }, 14 | "devDependencies": { 15 | "stream-spigot": "~3.0.5", 16 | "tape": "~4.0.0", 17 | "terminus": "~1.0.12" 18 | }, 19 | "engines": { 20 | "node": ">= 6" 21 | } 22 | }, 23 | "node_modules/balanced-match": { 24 | "version": "1.0.2", 25 | "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", 26 | "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", 27 | "dev": true 28 | }, 29 | "node_modules/brace-expansion": { 30 | "version": "1.1.11", 31 | "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", 32 | "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", 33 | "dev": true, 34 | "dependencies": { 35 | "balanced-match": "^1.0.0", 36 | "concat-map": "0.0.1" 37 | } 38 | }, 39 | "node_modules/concat-map": { 40 | "version": "0.0.1", 41 | "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", 42 | "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", 43 | "dev": true 44 | }, 45 | "node_modules/core-util-is": { 46 | "version": "1.0.3", 47 | "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", 48 | "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", 49 | "dev": true 50 | }, 51 | "node_modules/deep-equal": { 52 | "version": "1.0.1", 53 | "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", 54 | "integrity": "sha512-bHtC0iYvWhyaTzvV3CZgPeZQqCOBGyGsVV7v4eevpdkLHfiSrXUdBG+qAuSz4RI70sszvjQ1QSZ98An1yNwpSw==", 55 | "dev": true 56 | }, 57 | "node_modules/defined": { 58 | "version": "0.0.0", 59 | "resolved": "https://registry.npmjs.org/defined/-/defined-0.0.0.tgz", 60 | "integrity": "sha512-zpqiCT8bODLu3QSmLLic8xJnYWBFjOSu/fBCm189oAiTtPq/PSanNACKZDS7kgSyCJY7P+IcODzlIogBK/9RBg==", 61 | "dev": true 62 | }, 63 | "node_modules/glob": { 64 | "version": "5.0.15", 65 | "resolved": "https://registry.npmjs.org/glob/-/glob-5.0.15.tgz", 66 | "integrity": "sha512-c9IPMazfRITpmAAKi22dK1VKxGDX9ehhqfABDriL/lzO92xcUKEJPQHrVA/2YHSNFB4iFlykVmWvwo48nr3OxA==", 67 | "dev": true, 68 | "dependencies": { 69 | "inflight": "^1.0.4", 70 | "inherits": "2", 71 | "minimatch": "2 || 3", 72 | "once": "^1.3.0", 73 | "path-is-absolute": "^1.0.0" 74 | }, 75 | "engines": { 76 | "node": "*" 77 | } 78 | }, 79 | "node_modules/inflight": { 80 | "version": "1.0.6", 81 | "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", 82 | "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", 83 | "dev": true, 84 | "dependencies": { 85 | "once": "^1.3.0", 86 | "wrappy": "1" 87 | } 88 | }, 89 | "node_modules/inherits": { 90 | "version": "2.0.4", 91 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", 92 | "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" 93 | }, 94 | "node_modules/isarray": { 95 | "version": "1.0.0", 96 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", 97 | "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==", 98 | "dev": true 99 | }, 100 | "node_modules/minimatch": { 101 | "version": "3.1.2", 102 | "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", 103 | "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", 104 | "dev": true, 105 | "dependencies": { 106 | "brace-expansion": "^1.1.7" 107 | }, 108 | "engines": { 109 | "node": "*" 110 | } 111 | }, 112 | "node_modules/object-inspect": { 113 | "version": "1.0.2", 114 | "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.0.2.tgz", 115 | "integrity": "sha512-Bg2UlB5dtn0VQ2BvuisvthOUh9ehgL4FQsnLoXvY2GMJKfwNC6nZO0NFs7llpH6yZJcRUZZAoSb5VSnRSw17ww==", 116 | "dev": true 117 | }, 118 | "node_modules/once": { 119 | "version": "1.4.0", 120 | "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", 121 | "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", 122 | "dev": true, 123 | "dependencies": { 124 | "wrappy": "1" 125 | } 126 | }, 127 | "node_modules/path-is-absolute": { 128 | "version": "1.0.1", 129 | "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", 130 | "integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==", 131 | "dev": true, 132 | "engines": { 133 | "node": ">=0.10.0" 134 | } 135 | }, 136 | "node_modules/process-nextick-args": { 137 | "version": "1.0.7", 138 | "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-1.0.7.tgz", 139 | "integrity": "sha512-yN0WQmuCX63LP/TMvAg31nvT6m4vDqJEiiv2CAZqWOGNWutc9DfDk1NPYYmKUFmaVM2UwDowH4u5AHWYP/jxKw==", 140 | "dev": true 141 | }, 142 | "node_modules/readable-stream": { 143 | "version": "2.2.11", 144 | "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.2.11.tgz", 145 | "integrity": "sha512-h+8+r3MKEhkiVrwdKL8aWs1oc1VvBu33ueshOvS26RsZQ3Amhx/oO3TKe4lApSV9ueY6as8EAh7mtuFjdlhg9Q==", 146 | "dev": true, 147 | "dependencies": { 148 | "core-util-is": "~1.0.0", 149 | "inherits": "~2.0.1", 150 | "isarray": "~1.0.0", 151 | "process-nextick-args": "~1.0.6", 152 | "safe-buffer": "~5.0.1", 153 | "string_decoder": "~1.0.0", 154 | "util-deprecate": "~1.0.1" 155 | } 156 | }, 157 | "node_modules/resumer": { 158 | "version": "0.0.0", 159 | "resolved": "https://registry.npmjs.org/resumer/-/resumer-0.0.0.tgz", 160 | "integrity": "sha512-Fn9X8rX8yYF4m81rZCK/5VmrmsSbqS/i3rDLl6ZZHAXgC2nTAx3dhwG8q8odP/RmdLa2YrybDJaAMg+X1ajY3w==", 161 | "dev": true, 162 | "dependencies": { 163 | "through": "~2.3.4" 164 | } 165 | }, 166 | "node_modules/safe-buffer": { 167 | "version": "5.0.1", 168 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.0.1.tgz", 169 | "integrity": "sha512-cr7dZWLwOeaFBLTIuZeYdkfO7UzGIKhjYENJFAxUOMKWGaWDm2nJM2rzxNRm5Owu0DH3ApwNo6kx5idXZfb/Iw==", 170 | "dev": true 171 | }, 172 | "node_modules/stream-spigot": { 173 | "version": "3.0.6", 174 | "resolved": "https://registry.npmjs.org/stream-spigot/-/stream-spigot-3.0.6.tgz", 175 | "integrity": "sha512-n3ebCcU13ks6fIN01tqt2K91hI2sH+bgooBZY8ZZAeJbsya6U2D1o1N94pjVCRanhwXaI7b50wkRDLG5QwPYfg==", 176 | "dev": true, 177 | "dependencies": { 178 | "readable-stream": "~2.2.6", 179 | "xtend": "~4.0.0" 180 | } 181 | }, 182 | "node_modules/string_decoder": { 183 | "version": "1.0.3", 184 | "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", 185 | "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", 186 | "dev": true, 187 | "dependencies": { 188 | "safe-buffer": "~5.1.0" 189 | } 190 | }, 191 | "node_modules/string_decoder/node_modules/safe-buffer": { 192 | "version": "5.1.2", 193 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", 194 | "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", 195 | "dev": true 196 | }, 197 | "node_modules/tape": { 198 | "version": "4.0.3", 199 | "resolved": "https://registry.npmjs.org/tape/-/tape-4.0.3.tgz", 200 | "integrity": "sha512-Aet+h8l4d3j/Ff9HNNUKPFXD0XAUsKyezumN64iBRPtZF1uh2NxKZYNXag8DXwt8UhdQN1AJ24+Cdqb0QL4zrQ==", 201 | "dev": true, 202 | "dependencies": { 203 | "deep-equal": "~1.0.0", 204 | "defined": "~0.0.0", 205 | "glob": "~5.0.3", 206 | "inherits": "~2.0.1", 207 | "object-inspect": "~1.0.0", 208 | "resumer": "~0.0.0", 209 | "through": "~2.3.4" 210 | }, 211 | "bin": { 212 | "tape": "bin/tape" 213 | } 214 | }, 215 | "node_modules/terminus": { 216 | "version": "1.0.12", 217 | "resolved": "https://registry.npmjs.org/terminus/-/terminus-1.0.12.tgz", 218 | "integrity": "sha512-3DuwQBmnuhBTdrHkbfi5av+S1DvBEFMN6iIkR+kf/bnx68dY6RwOW1GoCzovNrD/VGJObBpPcFbFIPxrynl5bw==", 219 | "dev": true, 220 | "dependencies": { 221 | "readable-stream": "~1.0.33", 222 | "xtend": "~4.0.0" 223 | } 224 | }, 225 | "node_modules/terminus/node_modules/isarray": { 226 | "version": "0.0.1", 227 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", 228 | "integrity": "sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==", 229 | "dev": true 230 | }, 231 | "node_modules/terminus/node_modules/readable-stream": { 232 | "version": "1.0.34", 233 | "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz", 234 | "integrity": "sha512-ok1qVCJuRkNmvebYikljxJA/UEsKwLl2nI1OmaqAu4/UE+h0wKCHok4XkL/gvi39OacXvw59RJUOFUkDib2rHg==", 235 | "dev": true, 236 | "dependencies": { 237 | "core-util-is": "~1.0.0", 238 | "inherits": "~2.0.1", 239 | "isarray": "0.0.1", 240 | "string_decoder": "~0.10.x" 241 | } 242 | }, 243 | "node_modules/terminus/node_modules/string_decoder": { 244 | "version": "0.10.31", 245 | "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", 246 | "integrity": "sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ==", 247 | "dev": true 248 | }, 249 | "node_modules/through": { 250 | "version": "2.3.8", 251 | "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", 252 | "integrity": "sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==", 253 | "dev": true 254 | }, 255 | "node_modules/through2": { 256 | "version": "4.0.2", 257 | "resolved": "https://registry.npmjs.org/through2/-/through2-4.0.2.tgz", 258 | "integrity": "sha512-iOqSav00cVxEEICeD7TjLB1sueEL+81Wpzp2bY17uZjZN0pWZPuo4suZ/61VujxmqSGFfgOcNuTZ85QJwNZQpw==", 259 | "dependencies": { 260 | "readable-stream": "3" 261 | } 262 | }, 263 | "node_modules/through2/node_modules/readable-stream": { 264 | "version": "3.6.2", 265 | "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", 266 | "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", 267 | "dependencies": { 268 | "inherits": "^2.0.3", 269 | "string_decoder": "^1.1.1", 270 | "util-deprecate": "^1.0.1" 271 | }, 272 | "engines": { 273 | "node": ">= 6" 274 | } 275 | }, 276 | "node_modules/through2/node_modules/safe-buffer": { 277 | "version": "5.2.1", 278 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", 279 | "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", 280 | "funding": [ 281 | { 282 | "type": "github", 283 | "url": "https://github.com/sponsors/feross" 284 | }, 285 | { 286 | "type": "patreon", 287 | "url": "https://www.patreon.com/feross" 288 | }, 289 | { 290 | "type": "consulting", 291 | "url": "https://feross.org/support" 292 | } 293 | ] 294 | }, 295 | "node_modules/through2/node_modules/string_decoder": { 296 | "version": "1.3.0", 297 | "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", 298 | "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", 299 | "dependencies": { 300 | "safe-buffer": "~5.2.0" 301 | } 302 | }, 303 | "node_modules/util-deprecate": { 304 | "version": "1.0.2", 305 | "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", 306 | "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" 307 | }, 308 | "node_modules/wrappy": { 309 | "version": "1.0.2", 310 | "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", 311 | "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", 312 | "dev": true 313 | }, 314 | "node_modules/xtend": { 315 | "version": "4.0.2", 316 | "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", 317 | "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", 318 | "dev": true, 319 | "engines": { 320 | "node": ">=0.4" 321 | } 322 | } 323 | } 324 | } 325 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "through2-map", 3 | "version": "4.0.0", 4 | "description": "A through2 to create an Array.prototype.map analog for streams.", 5 | "main": "index.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "node test/" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git@github.com:brycebaril/through2-map.git" 15 | }, 16 | "keywords": [ 17 | "streams", 18 | "through", 19 | "through2", 20 | "map" 21 | ], 22 | "author": "Bryce B. Baril", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/brycebaril/through2-map/issues" 26 | }, 27 | "engines": { 28 | "node": ">= 6" 29 | }, 30 | "jshintConfig": { 31 | "asi": true, 32 | "globalstrict": true, 33 | "validthis": true, 34 | "eqnull": true, 35 | "node": true, 36 | "loopfunc": true, 37 | "newcap": false, 38 | "eqeqeq": false 39 | }, 40 | "devDependencies": { 41 | "tape": "~4.0.0", 42 | "stream-spigot": "~3.0.5", 43 | "terminus": "~1.0.12" 44 | }, 45 | "dependencies": { 46 | "through2": "^4.0.2" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const test = require("tape") 4 | 5 | const map = require("../") 6 | const spigot = require("stream-spigot") 7 | const concat = require("terminus").concat 8 | 9 | test("ctor", function (t) { 10 | t.plan(2) 11 | 12 | const Map = map.ctor(function (record) { 13 | record.foo.toUpperCase() 14 | return record 15 | }) 16 | 17 | function combine(records) { 18 | t.equals(records.length, 5, "Correct number of remaining records") 19 | t.notOk(records.filter(function (r) { /^[A-Z]$/.exec(r.foo) }).length, "Everything uppercased") 20 | } 21 | 22 | spigot({objectMode: true}, [ 23 | {foo: "bar"}, 24 | {foo: "baz"}, 25 | {foo: "bif"}, 26 | {foo: "blah"}, 27 | {foo: "buzz"}, 28 | ]) 29 | .pipe(new Map({objectMode: true})) 30 | .pipe(concat({objectMode: true}, combine)) 31 | }) 32 | 33 | test("ctor options", function (t) { 34 | t.plan(7) 35 | 36 | const Map = map.ctor({objectMode: true, foo: "bar"}, function (record) { 37 | t.equals(this.options.foo, "bar", "can see options") 38 | record.foo.toUpperCase() 39 | return record 40 | }) 41 | 42 | function combine(records) { 43 | t.equals(records.length, 5, "Correct number of remaining records") 44 | t.notOk(records.filter(function (r) { /^[A-Z]$/.exec(r.foo) }).length, "Everything uppercased") 45 | } 46 | 47 | spigot({objectMode: true}, [ 48 | {foo: "bar"}, 49 | {foo: "baz"}, 50 | {foo: "bif"}, 51 | {foo: "blah"}, 52 | {foo: "buzz"}, 53 | ]) 54 | .pipe(new Map({objectMode: true})) 55 | .pipe(concat({objectMode: true}, combine)) 56 | }) 57 | 58 | test("objCtor", function (t) { 59 | t.plan(7) 60 | 61 | const Map = map.objCtor(function (record) { 62 | t.equals(this.options.objectMode, true, "can see options") 63 | record.foo.toUpperCase() 64 | return record 65 | }) 66 | 67 | function combine(records) { 68 | t.equals(records.length, 5, "Correct number of remaining records") 69 | t.notOk(records.filter(function (r) { /^[A-Z]$/.exec(r.foo) }).length, "Everything uppercased") 70 | } 71 | 72 | spigot({objectMode: true}, [ 73 | {foo: "bar"}, 74 | {foo: "baz"}, 75 | {foo: "bif"}, 76 | {foo: "blah"}, 77 | {foo: "buzz"}, 78 | ]) 79 | .pipe(new Map({objectMode: true})) 80 | .pipe(concat({objectMode: true}, combine)) 81 | }) 82 | 83 | test("ctor buffer wantStrings index", function (t) { 84 | t.plan(1) 85 | 86 | const Map = map.ctor({wantStrings: true}, function (chunk, index) { 87 | return (index % 2 === 0) ? chunk.toUpperCase() : chunk 88 | }) 89 | 90 | function combine(result) { 91 | t.equals(result.toString(), "AbCdEf", "result is correct") 92 | } 93 | 94 | spigot([ 95 | "a", 96 | "b", 97 | "c", 98 | "d", 99 | "e", 100 | "f", 101 | ]).pipe(new Map()) 102 | .pipe(concat(combine)) 103 | }) 104 | 105 | test("simple", function (t) { 106 | t.plan(2) 107 | 108 | const m = map({objectMode: true}, function (record) { 109 | record.foo.toUpperCase() 110 | return record 111 | }) 112 | 113 | function combine(records) { 114 | t.equals(records.length, 5, "Correct number of remaining records") 115 | t.notOk(records.filter(function (r) { /^[A-Z]$/.exec(r.foo) }).length, "Everything uppercased") 116 | } 117 | 118 | spigot({objectMode: true}, [ 119 | {foo: "bar"}, 120 | {foo: "baz"}, 121 | {foo: "bif"}, 122 | {foo: "blah"}, 123 | {foo: "buzz"}, 124 | ]) 125 | .pipe(m) 126 | .pipe(concat({objectMode: true}, combine)) 127 | }) 128 | 129 | test("simple .obj", function (t) { 130 | t.plan(2) 131 | 132 | const m = map.obj(function (record) { 133 | record.foo.toUpperCase() 134 | return record 135 | }) 136 | 137 | function combine(records) { 138 | t.equals(records.length, 5, "Correct number of remaining records") 139 | t.notOk(records.filter(function (r) { /^[A-Z]$/.exec(r.foo) }).length, "Everything uppercased") 140 | } 141 | 142 | spigot({objectMode: true}, [ 143 | {foo: "bar"}, 144 | {foo: "baz"}, 145 | {foo: "bif"}, 146 | {foo: "blah"}, 147 | {foo: "buzz"}, 148 | ]) 149 | .pipe(m) 150 | .pipe(concat({objectMode: true}, combine)) 151 | }) 152 | 153 | test("simple buffer", function (t) { 154 | t.plan(1) 155 | 156 | const f = map({objectMode: true}, function (chunk) { 157 | return chunk.slice(0, 5) 158 | }) 159 | 160 | function combine(result) { 161 | t.equals(result.toString(), "abcdefglmnopuvwxyz", "result is correct") 162 | } 163 | 164 | spigot([ 165 | "a", 166 | "b", 167 | "cdefghijk", 168 | "lmnopqrst", 169 | "u", 170 | "vwxyz", 171 | ]).pipe(f) 172 | .pipe(concat(combine)) 173 | }) 174 | 175 | test("end early", function (t) { 176 | t.plan(1) 177 | 178 | let count = 0 179 | const f = map(function (chunk) { 180 | if (++count > 1) 181 | return null 182 | return chunk 183 | }) 184 | 185 | function combine(result) { 186 | t.equals(result.toString(), "a", "result is correct") 187 | } 188 | 189 | spigot([ 190 | "a", 191 | "b", 192 | "cdefghijk", 193 | "lmnopqrst", 194 | "u", 195 | "vwxyz", 196 | ]).pipe(f) 197 | .pipe(concat({objectMode: true}, combine)) 198 | }) 199 | 200 | test("error", function (t) { 201 | t.plan(1) 202 | 203 | const f = map(function (chunk) { 204 | throw new Error("Error in map function") 205 | }) 206 | 207 | function end () { 208 | t.fail("Should not end") 209 | } 210 | 211 | const r = spigot([ 212 | "a", 213 | "b", 214 | "cdefghijk", 215 | "lmnopqrst", 216 | "u", 217 | "vwxyz", 218 | ]).pipe(f) 219 | .on("end", end) 220 | .on("error", function (err) { 221 | t.true(err instanceof Error, "Caught error") 222 | }) 223 | }) 224 | --------------------------------------------------------------------------------