object-stream-tools
Advanced tools
Comparing version 2.1.0 to 2.2.0
100
index.js
@@ -25,2 +25,57 @@ 'use strict' | ||
function map(func) { | ||
let i = 0 | ||
return thru((data, cb) => { | ||
cb(null, func(data, i++)) | ||
}) | ||
} | ||
function filter(func) { | ||
let i = 0 | ||
return thru((data, cb) => { | ||
if (func(data, i++)) { | ||
cb(null, data) | ||
} else { | ||
cb() | ||
} | ||
}) | ||
} | ||
function some(func) { | ||
let i = 0 | ||
return thru(function(curr, cb) { | ||
if (func(curr, i++)) { | ||
cb(null, true) | ||
this.emit('end') | ||
} else { | ||
cb() | ||
} | ||
}, function () { | ||
this.emit('data', false) | ||
this.emit('end') | ||
}) | ||
} | ||
function required() { | ||
throw new Error('Initial value required') | ||
} | ||
function reduce(func, acc = required()) { | ||
let i = 0 | ||
return thru((curr, cb) => { | ||
acc = func(acc, curr, i++) | ||
cb() | ||
}, function () { | ||
this.emit('data', acc) | ||
this.emit('end') | ||
}) | ||
} | ||
function newReadable() { | ||
const rs = new stream.Readable({objectMode: true}) | ||
rs._read = () => { | ||
} | ||
return rs | ||
} | ||
function arrayToStream(data) { | ||
@@ -47,18 +102,2 @@ const newStream = newReadable() | ||
function newReadable() { | ||
const rs = new stream.Readable({objectMode: true}) | ||
rs._read = () => { | ||
} | ||
return rs | ||
} | ||
function map(func) { | ||
return new stream.Transform({ | ||
objectMode: true, | ||
transform: (data, enc, cb) => { | ||
cb(null, func(data)) | ||
} | ||
}) | ||
} | ||
function promiseToStream(promise) { | ||
@@ -85,30 +124,2 @@ const newStream = newReadable() | ||
function filter(func) { | ||
return new stream.Transform({ | ||
objectMode: true, | ||
transform: (data, enc, cb) => { | ||
if (func(data)) { | ||
cb(null, data) | ||
} else { | ||
cb() | ||
} | ||
} | ||
}) | ||
} | ||
function required() { | ||
throw new Error('Initial value required') | ||
} | ||
function reduce(func, acc = required()) { | ||
let i = 0 | ||
return thru((curr, cb) => { | ||
acc = func(acc, curr, i++) | ||
cb() | ||
}, function () { | ||
this.emit('data', acc) | ||
this.emit('end') | ||
}) | ||
} | ||
module.exports = { | ||
@@ -124,4 +135,5 @@ thru, | ||
reduce, | ||
some, | ||
promiseToStream, | ||
streamToPromise | ||
} |
{ | ||
"name": "object-stream-tools", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Useful tools for manipulating object streams. Will be especially helpful to developers used to map - filter - reduce approach of nodejs arrays.", | ||
@@ -39,2 +39,5 @@ "main": "index.js", | ||
}, | ||
"engines": { | ||
"node" : ">=6.0.0" | ||
}, | ||
"homepage": "https://github.com/kichooo/object-stream-tools#readme", | ||
@@ -41,0 +44,0 @@ "devDependencies": { |
@@ -7,2 +7,3 @@ 'use strict' | ||
const jsonStream = require('JSONStream') | ||
const stream = require('stream') | ||
@@ -59,2 +60,11 @@ const data = require('./data.json') | ||
tap.test('Test map uses correct iterator', t => | ||
dataStream() | ||
.pipe(ost.map((obj, i) => obj.foo + i)) | ||
.pipe(ost.streamToArray()) | ||
.on('data', objs => t.same(objs, ['bar0', 'foo1', 'rand2'])) | ||
.on('error', t.fail) | ||
.on('end', t.end) | ||
) | ||
tap.test('Test filter', t => | ||
@@ -69,2 +79,12 @@ dataStream() | ||
tap.test('Test filter uses correct iterator', t => | ||
dataStream() | ||
.pipe(ost.filter((e, i) => e.value + i > 6)) | ||
.pipe(ost.streamToArray()) | ||
.on('data', objs => | ||
t.same(objs, data.filter((e, i) => e.value + i > 6))) | ||
.on('error', t.fail) | ||
.on('end', t.end) | ||
) | ||
tap.test('Test filter on numerical values', t => | ||
@@ -173,2 +193,32 @@ dataStream() | ||
tap.test('Test stream to promise on broken streams', t => { | ||
const readable = ost.newReadable() | ||
ost.streamToPromise(readable) | ||
.then(data => t.fail('should resolve as failed promise')) | ||
.catch(err => { | ||
t.same(err, 'Jabberwacky') | ||
t.end() | ||
}) | ||
readable.emit('error', 'Jabberwacky') | ||
}) | ||
tap.test('Test some if any value matches', t => | ||
ost.streamToPromise( | ||
dataStream().pipe(ost.some(el => el.value === 42)) | ||
) | ||
.then(boolArr => t.same(...boolArr, true)) | ||
.catch(t.fail) | ||
) | ||
tap.test('Test some when no value matches', t => | ||
ost.streamToPromise( | ||
dataStream().pipe(ost.some(el => el.value === 'XXL')) | ||
) | ||
.then(boolArr => t.same(...boolArr, false)) | ||
.catch(t.fail) | ||
) | ||
function dataStream() { | ||
@@ -175,0 +225,0 @@ return fs.createReadStream('./test/data.json') |
16098
337