Comparing version 1.0.2 to 1.1.0
41
index.js
import isAsyncIterable from "is-async-iterable"; | ||
import AsyncIterable from "asynciterable"; | ||
@@ -17,21 +18,31 @@ /** | ||
*/ | ||
export default async function* filter(predicate, data) { | ||
if (typeof predicate !== "function") { | ||
throw new TypeError("predicate argument must be a function."); | ||
} | ||
export default function filter(predicate, data) { | ||
return new AsyncIterable(async (write, end) => { | ||
if (typeof predicate !== "function") { | ||
throw new TypeError("predicate argument must be a function."); | ||
} | ||
if (typeof data === "undefined") { | ||
return filter.bind(null, predicate); | ||
} | ||
if (typeof data === "undefined") { | ||
return filter.bind(null, predicate); | ||
} | ||
if (!isAsyncIterable(data)) { | ||
throw new TypeError("data argument must be an iterable or async-iterable."); | ||
} | ||
if (!isAsyncIterable(data)) { | ||
throw new TypeError( | ||
"data argument must be an iterable or async-iterable." | ||
); | ||
} | ||
let index = 0; | ||
for await (const item of data) { | ||
if (await predicate(item, index++, data)) { | ||
yield item; | ||
const generator = data[Symbol.asyncIterator] || data[Symbol.iterator]; | ||
const iterator = generator.call(data); | ||
let index = 0; | ||
let item = await iterator.next(); | ||
while (!item.done) { | ||
if (await predicate(await item.value, index, data)) { | ||
write(item.value); | ||
} | ||
index++; | ||
item = await iterator.next(); | ||
} | ||
} | ||
end(); | ||
}); | ||
} |
{ | ||
"name": "ai-filter", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Filter over async iterables.", | ||
@@ -13,3 +13,3 @@ "repository": "parro-it/ai-filter", | ||
"scripts": { | ||
"test": "node --harmony_async_iteration -r @std/esm test.js | tap-colorize - && linterjs .", | ||
"test": "node -r @std/esm test.js | tap-colorize - && linterjs .", | ||
"doc": "documentation readme index.js --section=API" | ||
@@ -28,5 +28,5 @@ }, | ||
"@std/esm": "^0.16.0", | ||
"ai-concat": "^1.0.0", | ||
"ai-concat": "^1.3.0", | ||
"ai-from-stream": "^1.0.0", | ||
"ai-lines": "^1.0.0", | ||
"ai-lines": "^1.1.0", | ||
"asynciterable": "^1.0.1", | ||
@@ -39,4 +39,5 @@ "documentation": "^5.3.3", | ||
"dependencies": { | ||
"asynciterable": "^1.0.1", | ||
"is-async-iterable": "^1.0.0" | ||
} | ||
} |
5733
45
2
+ Addedasynciterable@^1.0.1
+ Addedasynciterable@1.1.0(transitive)