Comparing version 2.0.2 to 3.0.0
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.ItFilter = factory()}(typeof self !== 'undefined' ? self : this, function () { | ||
"use strict";var ItFilter=(()=>{var e=Object.defineProperty;var r=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var y=(a,t)=>{for(var f in t)e(a,f,{get:t[f],enumerable:!0})},d=(a,t,f,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of c(t))!l.call(a,i)&&i!==f&&e(a,i,{get:()=>t[i],enumerable:!(n=r(t,i))||n.enumerable});return a};var s=a=>d(e({},"__esModule",{value:!0}),a);var u={};y(u,{default:()=>o});async function*o(a,t){for await(let f of a)await t(f)&&(yield f)}return s(u);})(); | ||
"use strict";var ItFilter=(()=>{var a=Object.defineProperty;var c=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var s=Object.prototype.hasOwnProperty;var p=(t,n)=>{for(var e in n)a(t,e,{get:n[e],enumerable:!0})},m=(t,n,e,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of y(n))!s.call(t,o)&&o!==e&&a(t,o,{get:()=>n[o],enumerable:!(r=c(n,o))||r.enumerable});return t};var b=t=>m(a({},"__esModule",{value:!0}),t);var w={};p(w,{default:()=>S});function d(t){let[n,e]=t[Symbol.asyncIterator]!=null?[t[Symbol.asyncIterator](),Symbol.asyncIterator]:[t[Symbol.iterator](),Symbol.iterator],r=[];return{peek:()=>n.next(),push:o=>{r.push(o)},next:()=>r.length>0?{done:!1,value:r.shift()}:n.next(),[e](){return this}}}var u=d;function h(t){return t[Symbol.asyncIterator]!=null}function x(t,n){if(h(t))return async function*(){for await(let i of t)await n(i)&&(yield i)}();let e=u(t),{value:r,done:o}=e.next();if(o===!0)return function*(){}();let f=n(r);if(typeof f.then=="function")return async function*(){await f&&(yield r);for await(let i of e)await n(i)&&(yield i)}();let l=n;return function*(){for(let i of t)l(i)&&(yield i)}()}var S=x;return b(w);})(); | ||
return ItFilter})); |
/** | ||
* Filters the passed (async) iterable by using the filter function | ||
*/ | ||
export default function filter<T>(source: AsyncIterable<T> | Iterable<T>, fn: (val: T) => boolean | Promise<boolean>): AsyncGenerator<T, void, undefined>; | ||
declare function filter<T>(source: Iterable<T>, fn: (val: T) => Promise<boolean>): AsyncGenerator<T, void, undefined>; | ||
declare function filter<T>(source: Iterable<T>, fn: (val: T) => boolean): Generator<T, void, undefined>; | ||
declare function filter<T>(source: Iterable<T> | AsyncIterable<T>, fn: (val: T) => boolean | Promise<boolean>): AsyncGenerator<T, void, undefined>; | ||
export default filter; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,11 +0,45 @@ | ||
/** | ||
* Filters the passed (async) iterable by using the filter function | ||
*/ | ||
export default async function* filter(source, fn) { | ||
for await (const entry of source) { | ||
if (await fn(entry)) { | ||
yield entry; | ||
import peek from 'it-peekable'; | ||
function isAsyncIterable(thing) { | ||
return thing[Symbol.asyncIterator] != null; | ||
} | ||
function filter(source, fn) { | ||
if (isAsyncIterable(source)) { | ||
return (async function* () { | ||
for await (const entry of source) { | ||
if (await fn(entry)) { | ||
yield entry; | ||
} | ||
} | ||
})(); | ||
} | ||
// 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 = fn(value); | ||
// @ts-expect-error .then is not present on O | ||
if (typeof res.then === 'function') { | ||
return (async function* () { | ||
if (await res) { | ||
yield value; | ||
} | ||
for await (const entry of peekable) { | ||
if (await fn(entry)) { | ||
yield entry; | ||
} | ||
} | ||
})(); | ||
} | ||
const func = fn; | ||
return (function* () { | ||
for (const entry of source) { | ||
if (func(entry)) { | ||
yield entry; | ||
} | ||
} | ||
} | ||
})(); | ||
} | ||
export default filter; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "it-filter", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"description": "Filters the passed iterable by using the filter function", | ||
@@ -137,2 +137,5 @@ "author": "Alex Potsides <alex@achingbrain.net>", | ||
}, | ||
"dependencies": { | ||
"it-peekable": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -139,0 +142,0 @@ "aegir": "^38.1.7", |
@@ -36,7 +36,24 @@ # it-filter <!-- omit in toc --> | ||
// 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 fn = val => val > 2 // Return boolean or promise of boolean to keep item | ||
const fn = val => val > 2 // Return boolean to keep item | ||
const arr = all(filter(values, fn)) | ||
console.info(arr) // 3, 4 | ||
``` | ||
Async sources and filter functions must be awaited: | ||
```javascript | ||
import all from 'it-all' | ||
import filter from 'it-filter' | ||
const values = async function * () { | ||
yield * [0, 1, 2, 3, 4] | ||
} | ||
const fn = async val => val > 2 // Return boolean or promise of boolean to keep item | ||
const arr = await all(filter(values, fn)) | ||
@@ -43,0 +60,0 @@ |
@@ -0,11 +1,60 @@ | ||
import peek from 'it-peekable' | ||
function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> { | ||
return thing[Symbol.asyncIterator] != null | ||
} | ||
/** | ||
* Filters the passed (async) iterable by using the filter function | ||
*/ | ||
export default async function * filter <T> (source: AsyncIterable<T> | Iterable<T>, fn: (val: T) => boolean | Promise<boolean>): AsyncGenerator<T, void, undefined> { | ||
for await (const entry of source) { | ||
if (await fn(entry)) { | ||
yield entry | ||
function filter <T> (source: Iterable<T>, fn: (val: T) => Promise<boolean>): AsyncGenerator<T, void, undefined> | ||
function filter <T> (source: Iterable<T>, fn: (val: T) => boolean): Generator<T, void, undefined> | ||
function filter <T> (source: Iterable<T> | AsyncIterable<T>, fn: (val: T) => boolean | Promise<boolean>): AsyncGenerator<T, void, undefined> | ||
function filter <T> (source: Iterable<T> | AsyncIterable<T>, fn: (val: T) => boolean | Promise<boolean>): Generator<T, void, undefined> | AsyncGenerator<T, void, undefined> { | ||
if (isAsyncIterable(source)) { | ||
return (async function * () { | ||
for await (const entry of source) { | ||
if (await fn(entry)) { | ||
yield entry | ||
} | ||
} | ||
})() | ||
} | ||
// 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 = fn(value) | ||
// @ts-expect-error .then is not present on O | ||
if (typeof res.then === 'function') { | ||
return (async function * () { | ||
if (await res) { | ||
yield value | ||
} | ||
for await (const entry of peekable) { | ||
if (await fn(entry)) { | ||
yield entry | ||
} | ||
} | ||
})() | ||
} | ||
const func = fn as (val: T) => boolean | ||
return (function * () { | ||
for (const entry of source) { | ||
if (func(entry)) { | ||
yield entry | ||
} | ||
} | ||
} | ||
})() | ||
} | ||
export default filter |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12399
108
73
1
+ Addedit-peekable@^3.0.0
+ Addedit-peekable@3.0.5(transitive)