Comparing version 2.0.1 to 3.0.0
/** | ||
* Collects all values from an (async) iterable and returns them as an array | ||
*/ | ||
export default function all<T>(source: AsyncIterable<T> | Iterable<T>): Promise<T[]>; | ||
declare function all<T>(source: Iterable<T>): T[]; | ||
declare function all<T>(source: AsyncIterable<T>): Promise<T[]>; | ||
export default all; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,7 +0,16 @@ | ||
/** | ||
* Collects all values from an (async) iterable and returns them as an array | ||
*/ | ||
export default async function all(source) { | ||
function isAsyncIterable(thing) { | ||
return thing[Symbol.asyncIterator] != null; | ||
} | ||
function all(source) { | ||
if (isAsyncIterable(source)) { | ||
return (async () => { | ||
const arr = []; | ||
for await (const entry of source) { | ||
arr.push(entry); | ||
} | ||
return arr; | ||
})(); | ||
} | ||
const arr = []; | ||
for await (const entry of source) { | ||
for (const entry of source) { | ||
arr.push(entry); | ||
@@ -11,2 +20,3 @@ } | ||
} | ||
export default all; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "it-all", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "Collects all values from an (async) iterable and returns them as an array", | ||
@@ -23,3 +23,3 @@ "author": "Alex Potsides <alex@achingbrain.net>", | ||
"src", | ||
"dist/src", | ||
"dist", | ||
"!dist/test", | ||
@@ -26,0 +26,0 @@ "!**/*.tsbuildinfo" |
# it-all <!-- 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 @@ > Collects all values from an (async) iterable and returns them as an array | ||
- [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 `ItAll` in the global namespace. | ||
```html | ||
<script src="https://unpkg.com/it-all/dist/index.min.js"></script> | ||
``` | ||
For when you need a one-liner to collect iterable values. | ||
@@ -30,6 +39,8 @@ | ||
// This can also be an iterator, async iterator, generator, etc | ||
const values = [0, 1, 2, 3, 4] | ||
// This can also be an iterator, etc | ||
const values = function * () { | ||
yield * [0, 1, 2, 3, 4] | ||
} | ||
const arr = await all(values) | ||
const arr = all(values) | ||
@@ -39,2 +50,14 @@ console.info(arr) // 0, 1, 2, 3, 4 | ||
Async sources must be awaited: | ||
```javascript | ||
const values = async function * () { | ||
yield * [0, 1, 2, 3, 4] | ||
} | ||
const arr = await all(values()) | ||
console.info(arr) // 0, 1, 2, 3, 4 | ||
``` | ||
## License | ||
@@ -47,4 +70,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. |
@@ -0,1 +1,4 @@ | ||
function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> { | ||
return thing[Symbol.asyncIterator] != null | ||
} | ||
@@ -5,6 +8,20 @@ /** | ||
*/ | ||
export default async function all <T> (source: AsyncIterable<T> | Iterable<T>): Promise<T[]> { | ||
function all <T> (source: Iterable<T>): T[] | ||
function all <T> (source: AsyncIterable<T>): Promise<T[]> | ||
function all <T> (source: AsyncIterable<T> | Iterable<T>): Promise<T[]> | T[] { | ||
if (isAsyncIterable(source)) { | ||
return (async () => { | ||
const arr = [] | ||
for await (const entry of source) { | ||
arr.push(entry) | ||
} | ||
return arr | ||
})() | ||
} | ||
const arr = [] | ||
for await (const entry of source) { | ||
for (const entry of source) { | ||
arr.push(entry) | ||
@@ -15,1 +32,3 @@ } | ||
} | ||
export default all |
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
8660
9
56
69