object-stream-tools
Advanced tools
Comparing version 2.2.0 to 2.2.1
12
index.js
@@ -45,3 +45,3 @@ 'use strict' | ||
let i = 0 | ||
return thru(function(curr, cb) { | ||
return thru(function (curr, cb) { | ||
if (func(curr, i++)) { | ||
@@ -59,9 +59,9 @@ cb(null, true) | ||
function required() { | ||
throw new Error('Initial value required') | ||
} | ||
function reduce(func, acc = required()) { | ||
function reduce(func, acc) { | ||
let i = 0 | ||
return thru((curr, cb) => { | ||
if (acc === undefined) { | ||
acc = curr | ||
return cb() | ||
} | ||
acc = func(acc, curr, i++) | ||
@@ -68,0 +68,0 @@ cb() |
{ | ||
"name": "object-stream-tools", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Useful tools for manipulating object streams. Will be especially helpful to developers used to map - filter - reduce approach of nodejs arrays.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node_modules/tap/bin/run.js test/test.js" | ||
"test": "tap test/test.js", | ||
"coverage": "npm test -- --coverage-report=html" | ||
}, | ||
@@ -40,3 +41,3 @@ "repository": { | ||
"engines": { | ||
"node" : ">=6.0.0" | ||
"node" : ">=5.0.0" | ||
}, | ||
@@ -43,0 +44,0 @@ "homepage": "https://github.com/kichooo/object-stream-tools#readme", |
@@ -106,2 +106,6 @@ # object-stream-tools | ||
Please note that if you do not pass initial value reduce function will start in (prev, curr, i) mode. | ||
[Objects/Array/Reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) | ||
#### promise to stream | ||
@@ -123,3 +127,2 @@ | ||
```js | ||
@@ -126,0 +129,0 @@ ost.streamToPromise(fs.createReadStream('../test/data.json') |
@@ -5,3 +5,3 @@ 'use strict' | ||
const tap = require('tap') | ||
const ost = require('../index.js') | ||
const ost = require('../index') | ||
const jsonStream = require('JSONStream') | ||
@@ -110,2 +110,15 @@ const stream = require('stream') | ||
tap.test('Test reduce with no initial value', t => | ||
dataStream() | ||
.pipe(ost.map(obj => obj.value)) | ||
.pipe(ost.reduce((acc, curr, i) => { | ||
return acc + curr + i | ||
})) | ||
.on('data', reducedValue => | ||
t.same(reducedValue, 42 + 1 + 7 + 1)) | ||
.on('error', err => t.fail(err.stack)) | ||
.pipe(jsonStream.stringify()) | ||
.on('end', t.end) | ||
) | ||
// This test is a bit more complicated. We will only let the 1st object through when second has already been called. | ||
@@ -221,3 +234,2 @@ tap.test('Test thruParallel', t => { | ||
function dataStream() { | ||
@@ -224,0 +236,0 @@ return fs.createReadStream('./test/data.json') |
Sorry, the diff of this file is not supported yet
16765
350
137