into-stream
Advanced tools
Comparing version 3.0.0 to 3.1.0
56
index.js
'use strict'; | ||
const from = require('from2'); | ||
const pIsPromise = require('p-is-promise'); | ||
@@ -9,13 +10,32 @@ module.exports = x => { | ||
return from((size, cb) => { | ||
if (x.length === 0) { | ||
cb(null, null); | ||
let promise; | ||
let iterator; | ||
prepare(x); | ||
function prepare(value) { | ||
x = value; | ||
promise = pIsPromise(x) ? x : null; | ||
// we don't iterate on strings and buffers since slicing them is ~7x faster | ||
const shouldIterate = !promise && x[Symbol.iterator] && typeof x !== 'string' && !Buffer.isBuffer(x); | ||
iterator = shouldIterate ? x[Symbol.iterator]() : null; | ||
} | ||
return from(function reader(size, cb) { | ||
if (promise) { | ||
promise.then(prepare).then(() => reader.call(this, size, cb), cb); | ||
return; | ||
} | ||
if (Array.isArray(x)) { | ||
cb(null, x.shift()); | ||
if (iterator) { | ||
const obj = iterator.next(); | ||
setImmediate(cb, null, obj.done ? null : obj.value); | ||
return; | ||
} | ||
if (x.length === 0) { | ||
setImmediate(cb, null, null); | ||
return; | ||
} | ||
const chunk = x.slice(0, size); | ||
@@ -33,13 +53,25 @@ x = x.slice(size); | ||
return from.obj(function (size, cb) { | ||
if (Array.isArray(x)) { | ||
if (x.length === 0) { | ||
cb(null, null); | ||
return; | ||
} | ||
let promise; | ||
let iterator; | ||
cb(null, x.shift()); | ||
prepare(x); | ||
function prepare(value) { | ||
x = value; | ||
promise = pIsPromise(x) ? x : null; | ||
iterator = !promise && x[Symbol.iterator] ? x[Symbol.iterator]() : null; | ||
} | ||
return from.obj(function reader(size, cb) { | ||
if (promise) { | ||
promise.then(prepare).then(() => reader.call(this, size, cb), cb); | ||
return; | ||
} | ||
if (iterator) { | ||
const obj = iterator.next(); | ||
setImmediate(cb, null, obj.done ? null : obj.value); | ||
return; | ||
} | ||
this.push(x); | ||
@@ -46,0 +78,0 @@ |
{ | ||
"name": "into-stream", | ||
"version": "3.0.0", | ||
"description": "Convert a buffer/string/array/object into a stream", | ||
"version": "3.1.0", | ||
"description": "Convert a buffer/string/array/object/iterable/promise into a stream", | ||
"license": "MIT", | ||
@@ -27,2 +27,5 @@ "repository": "sindresorhus/into-stream", | ||
"array", | ||
"iterable", | ||
"promise", | ||
"promises", | ||
"from", | ||
@@ -40,10 +43,10 @@ "into", | ||
"dependencies": { | ||
"from2": "^2.1.1" | ||
"from2": "^2.1.1", | ||
"p-is-promise": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"buffer-equals": "^1.0.3", | ||
"get-stream": "^2.1.0", | ||
"get-stream": "^3.0.0", | ||
"xo": "*" | ||
} | ||
} |
# into-stream [![Build Status](https://travis-ci.org/sindresorhus/into-stream.svg?branch=master)](https://travis-ci.org/sindresorhus/into-stream) | ||
> Convert a buffer/string/array/object into a stream | ||
> Convert a buffer/string/array/object/iterable/promise into a stream | ||
@@ -29,3 +29,3 @@ Correctly chunks up the input and handles backpressure. | ||
Type: `Buffer` `string` `Array<Buffer|string>`<br> | ||
Type: `Buffer` `string` `Iterable<Buffer|string>` `Promise`<br> | ||
Returns: [Readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) | ||
@@ -37,3 +37,3 @@ | ||
Type: `object`, `array<object>`<br> | ||
Type: `Object`, `Iterable<Object>` `Promise`<br> | ||
Returns: [Readable object stream](https://nodejs.org/api/stream.html#stream_object_mode) | ||
@@ -40,0 +40,0 @@ |
4520
3
62
2
+ Addedp-is-promise@^1.1.0
+ Addedp-is-promise@1.1.0(transitive)