stream-transform
Advanced tools
Comparing version 3.3.2 to 3.3.3
103
lib/index.js
@@ -1,2 +0,1 @@ | ||
/* | ||
@@ -9,15 +8,15 @@ Stream Transform | ||
import stream from 'stream'; | ||
import util from 'util'; | ||
import stream from "stream"; | ||
import util from "util"; | ||
const Transformer = function(options = {}, handler){ | ||
const Transformer = function (options = {}, handler) { | ||
this.options = options; | ||
if(options.consume === undefined || options.consume === null){ | ||
if (options.consume === undefined || options.consume === null) { | ||
this.options.consume = false; | ||
} | ||
this.options.objectMode = true; | ||
if(options.parallel === undefined || options.parallel === null){ | ||
if (options.parallel === undefined || options.parallel === null) { | ||
this.options.parallel = 100; | ||
} | ||
if(options.params === undefined || options.params === null){ | ||
if (options.params === undefined || options.params === null) { | ||
options.params = null; | ||
@@ -38,7 +37,7 @@ } | ||
Transformer.prototype._transform = function(chunk, _, cb){ | ||
Transformer.prototype._transform = function (chunk, _, cb) { | ||
this.state.started++; | ||
this.state.running++; | ||
// Accept additionnal chunks to be processed in parallel | ||
if(!this.state.paused && this.state.running < this.options.parallel){ | ||
if (!this.state.paused && this.state.running < this.options.parallel) { | ||
cb(); | ||
@@ -49,6 +48,7 @@ cb = null; // Cancel further callback execution | ||
let l = this.handler.length; | ||
if(this.options.params !== null){ | ||
if (this.options.params !== null) { | ||
l--; | ||
} | ||
if(l === 1){ // sync | ||
if (l === 1) { | ||
// sync | ||
const result = this.handler.call(this, chunk, this.options.params); | ||
@@ -65,8 +65,8 @@ if (result && result.then) { | ||
} | ||
}else if(l === 2){ // async | ||
const callback = (err, ...chunks) => | ||
this.__done(err, chunks, cb); | ||
} else if (l === 2) { | ||
// async | ||
const callback = (err, ...chunks) => this.__done(err, chunks, cb); | ||
this.handler.call(this, chunk, callback, this.options.params); | ||
}else{ | ||
throw Error('Invalid handler arguments'); | ||
} else { | ||
throw Error("Invalid handler arguments"); | ||
} | ||
@@ -78,7 +78,7 @@ return false; | ||
}; | ||
Transformer.prototype._flush = function(cb){ | ||
if(this.state.running === 0){ | ||
Transformer.prototype._flush = function (cb) { | ||
if (this.state.running === 0) { | ||
cb(); | ||
}else{ | ||
this._ending = function(){ | ||
} else { | ||
this._ending = function () { | ||
cb(); | ||
@@ -88,5 +88,5 @@ }; | ||
}; | ||
Transformer.prototype.__done = function(err, chunks, cb){ | ||
Transformer.prototype.__done = function (err, chunks, cb) { | ||
this.state.running--; | ||
if(err){ | ||
if (err) { | ||
return this.destroy(err); | ||
@@ -96,4 +96,4 @@ // return this.emit('error', err); | ||
this.state.finished++; | ||
for(let chunk of chunks){ | ||
if (typeof chunk === 'number'){ | ||
for (let chunk of chunks) { | ||
if (typeof chunk === "number") { | ||
chunk = `${chunk}`; | ||
@@ -103,3 +103,3 @@ } | ||
// See https://nodejs.org/api/stream.html#stream_readable_push | ||
if(chunk !== undefined && chunk !== null && chunk !== ''){ | ||
if (chunk !== undefined && chunk !== null && chunk !== "") { | ||
this.state.paused = !this.push(chunk); | ||
@@ -109,25 +109,25 @@ } | ||
// Chunk has been processed | ||
if(cb){ | ||
if (cb) { | ||
cb(); | ||
} | ||
if(this._ending && this.state.running === 0){ | ||
if (this._ending && this.state.running === 0) { | ||
this._ending(); | ||
} | ||
}; | ||
const transform = function(){ | ||
const transform = function () { | ||
let options = {}; | ||
let callback, handler, records; | ||
for(let i = 0; i< arguments.length; i++){ | ||
for (let i = 0; i < arguments.length; i++) { | ||
const argument = arguments[i]; | ||
let type = typeof argument; | ||
if(argument === null){ | ||
type = 'null'; | ||
}else if(type === 'object' && Array.isArray(argument)){ | ||
type = 'array'; | ||
if (argument === null) { | ||
type = "null"; | ||
} else if (type === "object" && Array.isArray(argument)) { | ||
type = "array"; | ||
} | ||
if(type === 'array'){ | ||
if (type === "array") { | ||
records = argument; | ||
}else if(type === 'object'){ | ||
options = {...argument}; | ||
}else if(type === 'function'){ | ||
} else if (type === "object") { | ||
options = { ...argument }; | ||
} else if (type === "function") { | ||
if (handler && i === arguments.length - 1) { | ||
@@ -138,4 +138,6 @@ callback = argument; | ||
} | ||
}else if(type !== 'null'){ | ||
throw new Error(`Invalid Arguments: got ${JSON.stringify(argument)} at position ${i}`); | ||
} else if (type !== "null") { | ||
throw new Error( | ||
`Invalid Arguments: got ${JSON.stringify(argument)} at position ${i}`, | ||
); | ||
} | ||
@@ -146,5 +148,5 @@ } | ||
if (records) { | ||
const writer = function(){ | ||
for(const record of records){ | ||
if(error) break; | ||
const writer = function () { | ||
for (const record of records) { | ||
if (error) break; | ||
transformer.write(record); | ||
@@ -155,13 +157,14 @@ } | ||
// Support Deno, Rollup doesnt provide a shim for setImmediate | ||
if(typeof setImmediate === 'function'){ | ||
if (typeof setImmediate === "function") { | ||
setImmediate(writer); | ||
}else{ | ||
} else { | ||
setTimeout(writer, 0); | ||
} | ||
} | ||
if(callback || options.consume) { | ||
if (callback || options.consume) { | ||
const result = []; | ||
transformer.on('readable', function(){ | ||
let record; while((record = transformer.read()) !== null){ | ||
if(callback){ | ||
transformer.on("readable", function () { | ||
let record; | ||
while ((record = transformer.read()) !== null) { | ||
if (callback) { | ||
result.push(record); | ||
@@ -171,7 +174,7 @@ } | ||
}); | ||
transformer.on('error', function(err){ | ||
transformer.on("error", function (err) { | ||
error = true; | ||
if (callback) callback(err); | ||
}); | ||
transformer.on('end', function(){ | ||
transformer.on("end", function () { | ||
if (callback && !error) callback(null, result); | ||
@@ -178,0 +181,0 @@ }); |
@@ -1,2 +0,1 @@ | ||
/* | ||
@@ -9,24 +8,26 @@ Stream Transform - sync module | ||
import {Transformer} from './index.js'; | ||
import { Transformer } from "./index.js"; | ||
const transform = function(){ | ||
const transform = function () { | ||
// Import arguments normalization | ||
let handler, records; | ||
let options = {}; | ||
for(const i in arguments){ | ||
for (const i in arguments) { | ||
const argument = arguments[i]; | ||
let type = typeof argument; | ||
if(argument === null){ | ||
type = 'null'; | ||
}else if(type === 'object' && Array.isArray(argument)){ | ||
type = 'array'; | ||
if (argument === null) { | ||
type = "null"; | ||
} else if (type === "object" && Array.isArray(argument)) { | ||
type = "array"; | ||
} | ||
if(type === 'array'){ | ||
if (type === "array") { | ||
records = argument; | ||
}else if(type === 'object'){ | ||
options = {...argument}; | ||
}else if(type === 'function'){ | ||
} else if (type === "object") { | ||
options = { ...argument }; | ||
} else if (type === "function") { | ||
handler = argument; | ||
}else if(type !== 'null'){ | ||
throw new Error(`Invalid Arguments: got ${JSON.stringify(argument)} at position ${i}`); | ||
} else if (type !== "null") { | ||
throw new Error( | ||
`Invalid Arguments: got ${JSON.stringify(argument)} at position ${i}`, | ||
); | ||
} | ||
@@ -36,7 +37,7 @@ } | ||
let expected_handler_length = 1; | ||
if(options.params){ | ||
if (options.params) { | ||
expected_handler_length++; | ||
} | ||
if(handler.length > expected_handler_length){ | ||
throw Error('Invalid Handler: only synchonous handlers are supported'); | ||
if (handler.length > expected_handler_length) { | ||
throw Error("Invalid Handler: only synchonous handlers are supported"); | ||
} | ||
@@ -46,9 +47,9 @@ // Start transformation | ||
const transformer = new Transformer(options, handler); | ||
transformer.push = function(chunk){ | ||
transformer.push = function (chunk) { | ||
chunks.push(chunk); | ||
}; | ||
for(const record of records){ | ||
transformer._transform(record, null, function(){}); | ||
for (const record of records) { | ||
transformer._transform(record, null, function () {}); | ||
} | ||
return chunks; | ||
return chunks; | ||
}; | ||
@@ -55,0 +56,0 @@ |
{ | ||
"version": "3.3.2", | ||
"version": "3.3.3", | ||
"name": "stream-transform", | ||
@@ -15,18 +15,22 @@ "description": "Object transformations implementing the Node.js `stream.Transform` API", | ||
"devDependencies": { | ||
"@rollup/plugin-eslint": "^9.0.4", | ||
"@rollup/plugin-node-resolve": "^15.2.1", | ||
"@types/mocha": "^10.0.1", | ||
"@types/node": "^20.5.6", | ||
"@eslint/js": "^9.15.0", | ||
"@rollup/plugin-node-resolve": "^15.3.0", | ||
"@types/mocha": "^10.0.9", | ||
"@types/node": "^22.9.1", | ||
"coffeescript": "~2.7.0", | ||
"csv-generate": "^4.4.1", | ||
"each": "^2.4.0", | ||
"eslint": "^8.47.0", | ||
"mocha": "~10.2.0", | ||
"pad": "~3.2.0", | ||
"rollup": "^3.28.1", | ||
"csv-generate": "^4.4.2", | ||
"each": "^2.7.2", | ||
"eslint": "^9.15.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-plugin-mocha": "^10.5.0", | ||
"eslint-plugin-prettier": "^5.2.1", | ||
"mocha": "~10.8.2", | ||
"pad": "~3.3.0", | ||
"prettier": "^3.3.3", | ||
"rollup": "^4.27.3", | ||
"rollup-plugin-node-builtins": "^2.1.2", | ||
"rollup-plugin-node-globals": "^1.4.0", | ||
"should": "~13.2.3", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.2.2" | ||
"ts-node": "^10.9.2", | ||
"typescript": "^5.6.3" | ||
}, | ||
@@ -78,5 +82,9 @@ "exports": { | ||
], | ||
"throw-deprecation": true, | ||
"throw-deprecation": false, | ||
"timeout": 40000 | ||
}, | ||
"lint-staged": { | ||
"*.js": "npm run lint:fix", | ||
"*.md": "prettier -w" | ||
}, | ||
"repository": { | ||
@@ -92,9 +100,5 @@ "type": "git", | ||
"postbuild:ts": "find dist/cjs -name '*.d.cts' -exec sh -c \"sed -i \"s/\\.js'/\\.cjs'/g\" {} || sed -i '' \"s/\\.js'/\\.cjs'/g\" {}\" \\;", | ||
"lint": "npm run lint:lib && npm run lint:samples && npm run lint:test", | ||
"postlint": "tsc --noEmit true", | ||
"lint:lib": "eslint --fix lib/*.js", | ||
"lint:samples": "eslint --fix samples/*.js", | ||
"lint:test": "coffeelint --fix test/*.coffee", | ||
"lint:check": "eslint && tsc --noEmit true", | ||
"lint:fix": "eslint --fix && tsc --noEmit true", | ||
"preversion": "npm run build && git add dist", | ||
"pretest": "npm run build", | ||
"test": "mocha 'test/**/*.{coffee,ts}'", | ||
@@ -121,3 +125,3 @@ "test:legacy": "mocha --loader=./test/loaders/legacy/all.js --ignore test/handler.mode.callback.coffee --ignore test/handler.mode.callback.error.coffee 'test/**/*.{coffee,ts}'" | ||
}, | ||
"gitHead": "6aadcace1a9e77ea308159733391e42ad019c5cb" | ||
"gitHead": "cc1235a58de98dd9eab0665c7b1d03213e9633c7" | ||
} |
@@ -1,2 +0,1 @@ | ||
# Stream transformation for Node.js and the web | ||
@@ -14,21 +13,21 @@ | ||
* [Project homepage](https://csv.js.org/transform/) | ||
* [API](https://csv.js.org/transform/api/) | ||
* [Options](https://csv.js.org/transform/options/) | ||
* [Handler](https://csv.js.org/transform/handler/) | ||
* [State properties](https://csv.js.org/transform/state/) | ||
* [Examples](https://csv.js.org/transform/examples/) | ||
- [Project homepage](https://csv.js.org/transform/) | ||
- [API](https://csv.js.org/transform/api/) | ||
- [Options](https://csv.js.org/transform/options/) | ||
- [Handler](https://csv.js.org/transform/handler/) | ||
- [State properties](https://csv.js.org/transform/state/) | ||
- [Examples](https://csv.js.org/transform/examples/) | ||
## Main features | ||
* Extends the native Node.js [transform stream API](http://nodejs.org/api/stream.html#stream_class_stream_transform) | ||
* Simplicity with the optional callback and sync API | ||
* Pipe transformations between readable and writable streams | ||
* Synchronous versus asynchronous user functions | ||
* Sequential and parallel execution | ||
* Accept object, array or JSON as input and output | ||
* Sequential or user-defined concurrent execution | ||
* Skip and multiply records | ||
* Alter or clone input records | ||
* MIT License | ||
- Extends the native Node.js [transform stream API](http://nodejs.org/api/stream.html#stream_class_stream_transform) | ||
- Simplicity with the optional callback and sync API | ||
- Pipe transformations between readable and writable streams | ||
- Synchronous versus asynchronous user functions | ||
- Sequential and parallel execution | ||
- Accept object, array or JSON as input and output | ||
- Sequential or user-defined concurrent execution | ||
- Skip and multiply records | ||
- Alter or clone input records | ||
- MIT License | ||
@@ -46,16 +45,19 @@ ## Usage | ||
```js | ||
import { transform } from 'stream-transform/sync'; | ||
import assert from 'assert'; | ||
import { transform } from "stream-transform/sync"; | ||
import assert from "assert"; | ||
const records = transform([ | ||
[ 'a', 'b', 'c', 'd' ], | ||
[ '1', '2', '3', '4' ] | ||
], function(record){ | ||
record.push(record.shift()); | ||
return record; | ||
}); | ||
const records = transform( | ||
[ | ||
["a", "b", "c", "d"], | ||
["1", "2", "3", "4"], | ||
], | ||
function (record) { | ||
record.push(record.shift()); | ||
return record; | ||
}, | ||
); | ||
assert.deepEqual(records, [ | ||
[ 'b', 'c', 'd', 'a' ], | ||
[ '2', '3', '4', '1' ] | ||
["b", "c", "d", "a"], | ||
["2", "3", "4", "1"], | ||
]); | ||
@@ -76,2 +78,2 @@ ``` | ||
* David Worms: <https://github.com/wdavidw> | ||
- David Worms: <https://github.com/wdavidw> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
77
974591
20
19
28168