Comparing version 2.0.1 to 3.0.0
@@ -7,3 +7,5 @@ /** | ||
*/ | ||
export default function merge<T>(...sources: Array<AsyncIterable<T> | Iterable<T>>): AsyncGenerator<T, void, undefined>; | ||
declare function merge<T>(...sources: Array<Iterable<T>>): Generator<T, void, undefined>; | ||
declare function merge<T>(...sources: Array<AsyncIterable<T> | Iterable<T>>): AsyncGenerator<T, void, undefined>; | ||
export default merge; | ||
//# sourceMappingURL=index.d.ts.map |
import { pushable } from 'it-pushable'; | ||
/** | ||
* Treat one or more iterables as a single iterable. | ||
* | ||
* Nb. sources are iterated over in parallel so the | ||
* order of emitted items is not guaranteed. | ||
*/ | ||
export default async function* merge(...sources) { | ||
const output = pushable({ | ||
objectMode: true | ||
}); | ||
void Promise.resolve().then(async () => { | ||
try { | ||
await Promise.all(sources.map(async (source) => { | ||
for await (const item of source) { | ||
output.push(item); | ||
} | ||
})); | ||
output.end(); | ||
function isAsyncIterable(thing) { | ||
return thing[Symbol.asyncIterator] != null; | ||
} | ||
function merge(...sources) { | ||
const syncSources = []; | ||
for (const source of sources) { | ||
if (!isAsyncIterable(source)) { | ||
syncSources.push(source); | ||
} | ||
catch (err) { | ||
output.end(err); | ||
} | ||
}); | ||
yield* output; | ||
} | ||
if (syncSources.length === sources.length) { | ||
// all sources are synchronous | ||
return (function* () { | ||
for (const source of syncSources) { | ||
yield* source; | ||
} | ||
})(); | ||
} | ||
return (async function* () { | ||
const output = pushable({ | ||
objectMode: true | ||
}); | ||
void Promise.resolve().then(async () => { | ||
try { | ||
await Promise.all(sources.map(async (source) => { | ||
for await (const item of source) { | ||
output.push(item); | ||
} | ||
})); | ||
output.end(); | ||
} | ||
catch (err) { | ||
output.end(err); | ||
} | ||
}); | ||
yield* output; | ||
})(); | ||
} | ||
export default merge; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "it-merge", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "Treat one or more iterables as a single iterable", | ||
@@ -23,3 +23,3 @@ "author": "Alex Potsides <alex@achingbrain.net>", | ||
"src", | ||
"dist/src", | ||
"dist", | ||
"!dist/test", | ||
@@ -143,4 +143,4 @@ "!**/*.tsbuildinfo" | ||
"aegir": "^38.1.7", | ||
"it-all": "^2.0.0" | ||
"it-all": "^3.0.0" | ||
} | ||
} |
# it-merge <!-- omit in toc --> | ||
[![codecov](https://img.shields.io/codecov/c/github/achingbrain/it.svg?style=flat-square)](https://codecov.io/gh/achingbrain/it) | ||
[![CI](https://img.shields.io/github/workflow/status/achingbrain/it/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/achingbrain/it/actions/workflows/js-test-and-release.yml) | ||
[![CI](https://img.shields.io/github/actions/workflow/status/achingbrain/it/js-test-and-release.yml?branch=master\&style=flat-square)](https://github.com/achingbrain/it/actions/workflows/js-test-and-release.yml?query=branch%3Amaster) | ||
@@ -11,5 +11,6 @@ > Treat one or more iterables as a single iterable | ||
- [Install](#install) | ||
- [Browser `<script>` tag](#browser-script-tag) | ||
- [Usage](#usage) | ||
- [License](#license) | ||
- [Contribute](#contribute) | ||
- [Contribution](#contribution) | ||
@@ -22,2 +23,10 @@ ## Install | ||
### Browser `<script>` tag | ||
Loading this module through a script tag will make it's exports available as `ItMerge` in the global namespace. | ||
```html | ||
<script src="https://unpkg.com/it-merge/dist/index.min.js"></script> | ||
``` | ||
Nb. sources are iterated over in parallel so the order of emitted items is not guaranteed. | ||
@@ -31,8 +40,27 @@ | ||
// This can also be an iterator, async iterator, generator, etc | ||
// This can also be an iterator, generator, etc | ||
const values1 = [0, 1, 2, 3, 4] | ||
const values2 = [5, 6, 7, 8, 9] | ||
const arr = await all(merge(values1, values2)) | ||
const arr = all(merge(values1, values2)) | ||
console.info(arr) // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 | ||
``` | ||
Async sources must be awaited: | ||
```javascript | ||
import merge from 'it-merge' | ||
import all from 'it-all' | ||
// This can also be an iterator, async iterator, generator, etc | ||
const values1 = async function * () { | ||
yield * [0, 1, 2, 3, 4] | ||
} | ||
const values2 = async function * () { | ||
yield * [5, 6, 7, 8, 9] | ||
} | ||
const arr = await all(merge(values1(), values2())) | ||
console.info(arr) // 0, 1, 5, 6, 2, 3, 4, 7, 8, 9 <- nb. order is not guaranteed | ||
@@ -48,4 +76,4 @@ ``` | ||
## Contribute | ||
## Contribution | ||
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. |
import { pushable } from 'it-pushable' | ||
function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> { | ||
return thing[Symbol.asyncIterator] != null | ||
} | ||
/** | ||
@@ -9,24 +13,47 @@ * Treat one or more iterables as a single iterable. | ||
*/ | ||
export default async function * merge <T> (...sources: Array<AsyncIterable<T> | Iterable<T>>): AsyncGenerator<T, void, undefined> { | ||
const output = pushable<T>({ | ||
objectMode: true | ||
}) | ||
function merge <T> (...sources: Array<Iterable<T>>): Generator<T, void, undefined> | ||
function merge <T> (...sources: Array<AsyncIterable<T> | Iterable<T>>): AsyncGenerator<T, void, undefined> | ||
function merge <T> (...sources: Array<AsyncIterable<T> | Iterable<T>>): AsyncGenerator<T, void, undefined> | Generator<T, void, undefined> { | ||
const syncSources: Array<Iterable<T>> = [] | ||
void Promise.resolve().then(async () => { | ||
try { | ||
await Promise.all( | ||
sources.map(async (source) => { | ||
for await (const item of source) { | ||
output.push(item) | ||
} | ||
}) | ||
) | ||
output.end() | ||
} catch (err: any) { | ||
output.end(err) | ||
for (const source of sources) { | ||
if (!isAsyncIterable(source)) { | ||
syncSources.push(source) | ||
} | ||
}) | ||
} | ||
yield * output | ||
if (syncSources.length === sources.length) { | ||
// all sources are synchronous | ||
return (function * () { | ||
for (const source of syncSources) { | ||
yield * source | ||
} | ||
})() | ||
} | ||
return (async function * () { | ||
const output = pushable<T>({ | ||
objectMode: true | ||
}) | ||
void Promise.resolve().then(async () => { | ||
try { | ||
await Promise.all( | ||
sources.map(async (source) => { | ||
for await (const item of source) { | ||
output.push(item) | ||
} | ||
}) | ||
) | ||
output.end() | ||
} catch (err: any) { | ||
output.end(err) | ||
} | ||
}) | ||
yield * output | ||
})() | ||
} | ||
export default merge |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13730
9
112
76