Comparing version
@@ -5,3 +5,6 @@ /** | ||
*/ | ||
export default function map<I, O>(source: AsyncIterable<I> | Iterable<I>, func: (val: I) => O | Promise<O>): AsyncGenerator<O, void, undefined>; | ||
declare function map<I, O>(source: Iterable<I>, func: (val: I) => Promise<O>): AsyncGenerator<O, void, undefined>; | ||
declare function map<I, O>(source: Iterable<I>, func: (val: I) => O): Generator<O, void, undefined>; | ||
declare function map<I, O>(source: AsyncIterable<I>, func: (val: I) => O | Promise<O>): AsyncGenerator<O, void, undefined>; | ||
export default map; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,10 +0,37 @@ | ||
/** | ||
* Takes an (async) iterable and returns one with each item mapped by the passed | ||
* function | ||
*/ | ||
export default async function* map(source, func) { | ||
for await (const val of source) { | ||
yield func(val); | ||
import peek from 'it-peekable'; | ||
function isAsyncIterable(thing) { | ||
return thing[Symbol.asyncIterator] != null; | ||
} | ||
function map(source, func) { | ||
if (isAsyncIterable(source)) { | ||
return (async function* () { | ||
for await (const val of source) { | ||
yield func(val); | ||
} | ||
})(); | ||
} | ||
// if mapping function returns a promise we have to return an async generator | ||
const peekable = peek(source); | ||
const { value, done } = peekable.next(); | ||
if (done === true) { | ||
return (function* () { }()); | ||
} | ||
const res = func(value); | ||
// @ts-expect-error .then is not present on O | ||
if (typeof res.then === 'function') { | ||
return (async function* () { | ||
yield await res; | ||
for await (const val of peekable) { | ||
yield func(val); | ||
} | ||
})(); | ||
} | ||
const fn = func; | ||
return (function* () { | ||
for (const val of source) { | ||
yield fn(val); | ||
} | ||
})(); | ||
} | ||
export default map; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "it-map", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "Maps the values yielded by an async iterator", | ||
@@ -23,3 +23,3 @@ "author": "Alex Potsides <alex@achingbrain.net>", | ||
"src", | ||
"dist/src", | ||
"dist", | ||
"!dist/test", | ||
@@ -140,3 +140,6 @@ "!**/*.tsbuildinfo" | ||
"aegir": "^38.1.7" | ||
}, | ||
"dependencies": { | ||
"it-peekable": "^3.0.0" | ||
} | ||
} |
# it-map <!-- omit in toc --> | ||
[](https://codecov.io/gh/achingbrain/it) | ||
[](https://github.com/achingbrain/it/actions/workflows/js-test-and-release.yml) | ||
[](https://github.com/achingbrain/it/actions/workflows/js-test-and-release.yml?query=branch%3Amaster) | ||
@@ -11,5 +11,6 @@ > Maps the values yielded by an async iterator | ||
- [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 `ItMap` in the global namespace. | ||
```html | ||
<script src="https://unpkg.com/it-map/dist/index.min.js"></script> | ||
``` | ||
## Usage | ||
@@ -28,10 +37,24 @@ | ||
// This can also be an iterator, async iterator, generator, etc | ||
// This can also be an iterator, generator, etc | ||
const values = [0, 1, 2, 3, 4] | ||
const result = await map(values, (val) => val++) | ||
const result = map(values, (val) => val++) | ||
console.info(result) // 15 | ||
console.info(result) // [1, 2, 3, 4, 5] | ||
``` | ||
Async sources and transforms must be awaited: | ||
```javascript | ||
import map from 'it-map' | ||
const values = async function * () { | ||
yield * [0, 1, 2, 3, 4] | ||
} | ||
const result = await map(values(), async (val) => val++) | ||
console.info(result) // [1, 2, 3, 4, 5] | ||
``` | ||
## License | ||
@@ -44,4 +67,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,7 @@ | ||
import peek from 'it-peekable' | ||
function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> { | ||
return thing[Symbol.asyncIterator] != null | ||
} | ||
/** | ||
@@ -5,6 +11,44 @@ * Takes an (async) iterable and returns one with each item mapped by the passed | ||
*/ | ||
export default async function * map <I, O> (source: AsyncIterable<I> | Iterable<I>, func: (val: I) => O | Promise<O>): AsyncGenerator<O, void, undefined> { | ||
for await (const val of source) { | ||
yield func(val) | ||
function map <I, O> (source: Iterable<I>, func: (val: I) => Promise<O>): AsyncGenerator<O, void, undefined> | ||
function map <I, O> (source: Iterable<I>, func: (val: I) => O): Generator<O, void, undefined> | ||
function map <I, O> (source: AsyncIterable<I>, func: (val: I) => O | Promise<O>): AsyncGenerator<O, void, undefined> | ||
function map <I, O> (source: AsyncIterable<I> | Iterable<I>, func: (val: I) => O | Promise<O>): AsyncGenerator<O, void, undefined> | Generator<O, void, undefined> { | ||
if (isAsyncIterable(source)) { | ||
return (async function * () { | ||
for await (const val of source) { | ||
yield func(val) | ||
} | ||
})() | ||
} | ||
// if mapping function returns a promise we have to return an async generator | ||
const peekable = peek(source) | ||
const { value, done } = peekable.next() | ||
if (done === true) { | ||
return (function * () {}()) | ||
} | ||
const res = func(value) | ||
// @ts-expect-error .then is not present on O | ||
if (typeof res.then === 'function') { | ||
return (async function * () { | ||
yield await res | ||
for await (const val of peekable) { | ||
yield func(val) | ||
} | ||
})() | ||
} | ||
const fn = func as (val: I) => O | ||
return (function * () { | ||
for (const val of source) { | ||
yield fn(val) | ||
} | ||
})() | ||
} | ||
export default map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
11628
85.04%9
12.5%94
308.7%67
52.27%1
Infinity%+ Added
+ Added