Skip to content

Commit

Permalink
Add support for TypedArray and ArrayBuffer (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Mac authored and sindresorhus committed Oct 12, 2018
1 parent 05b9ac0 commit ec6ef5c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ module.exports = input => {

function prepare(value) {
input = value;
if (
input instanceof ArrayBuffer ||
(ArrayBuffer.isView(input) && !Buffer.isBuffer(input))
) {
input = Buffer.from(input);
}
promise = pIsPromise(input) ? input : null;
// We don't iterate on strings and buffers since slicing them is ~7x faster
const shouldIterate = !promise && input[Symbol.iterator] && typeof input !== 'string' && !Buffer.isBuffer(input);
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ intoStream('unicorn').pipe(process.stdout);

### intoStream(input)

Type: `Buffer` `string` `Iterable<Buffer|string>` `Promise`<br>
Type: `Buffer` `TypedArray` `ArrayBuffer` `string` `Iterable<Buffer|string>` `Promise`<br>
Returns: [Readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable)

Adheres to the requested chunk size, except for `array` where each element will be a chunk.
Expand Down
18 changes: 18 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ test('buffer', async t => {
t.true((await getStream.buffer(m(f))).equals(f));
});

test('ArrayBuffer', async t => {
const f = Buffer.from(fixture);
const view = new Uint8Array(f.length);
for (let i = 0; i < f.length; i++) {
view[i] = f[i];
}
t.true((await getStream.buffer(m(view.buffer))).equals(f));
});

test('ArrayBuffer view', async t => {
const f = Buffer.from(fixture);
const view = new Uint8Array(f.length);
for (let i = 0; i < f.length; i++) {
view[i] = f[i];
}
t.true((await getStream.buffer(m(view))).equals(f));
});

test('array', async t => {
t.is(await getStream(m(fixture.split(''))), fixture);
});
Expand Down

0 comments on commit ec6ef5c

Please # to comment.