|
| 1 | +// deno-lint-ignore-file no-explicit-any |
| 2 | + |
| 3 | +export type WrappedAsyncIterator<T> = { |
| 4 | + unwrap: () => AsyncIterator<T>; |
| 5 | + map: <U>(mapperFn: (t: T) => U | Promise<U>) => WrappedAsyncIterator<U>; |
| 6 | + filter: { |
| 7 | + <U extends T>( |
| 8 | + filtererFn: (t: T) => t is U, |
| 9 | + ): WrappedAsyncIterator<U>; |
| 10 | + ( |
| 11 | + filtererFn: (t: T) => boolean | Promise<boolean>, |
| 12 | + ): WrappedAsyncIterator<T>; |
| 13 | + }; |
| 14 | + take: (limit: number) => WrappedAsyncIterator<T>; |
| 15 | + drop: (limit: number) => WrappedAsyncIterator<T>; |
| 16 | + asIndexedPairs: () => WrappedAsyncIterator<[number, T]>; |
| 17 | + flatMap: <U>( |
| 18 | + mapperFn: (t: T) => U | U[] | Promise<U | U[]>, |
| 19 | + ) => WrappedAsyncIterator<U>; |
| 20 | + reduce: { |
| 21 | + <U>( |
| 22 | + reducerFn: (u: U, t: T) => U | Promise<U>, |
| 23 | + initialValue: U, |
| 24 | + ): Promise<U>; |
| 25 | + <U>( |
| 26 | + reducerFn: (u: U | undefined, t: T) => U | Promise<U>, |
| 27 | + ): Promise<U | undefined>; |
| 28 | + }; |
| 29 | + toArray: () => Promise<T[]>; |
| 30 | + forEach: (fn: (t: T) => void | Promise<void>) => Promise<void>; |
| 31 | + some: (fn: (t: T) => boolean | Promise<boolean>) => Promise<boolean>; |
| 32 | + every: (fn: (t: T) => boolean | Promise<boolean>) => Promise<boolean>; |
| 33 | + find: (fn: (t: T) => boolean | Promise<boolean>) => Promise<T | undefined>; |
| 34 | +}; |
| 35 | + |
| 36 | +export const wrapAsyncIterator = <T>( |
| 37 | + ite: AsyncIterator<T>, |
| 38 | +): WrappedAsyncIterator<T> => { |
| 39 | + return { |
| 40 | + unwrap: () => ite, |
| 41 | + map: (mapperFn) => { |
| 42 | + if (typeof mapperFn !== "function") { |
| 43 | + throw new TypeError(`${mapperFn} is not a function`); |
| 44 | + } |
| 45 | + return wrapAsyncIterator((async function* () { |
| 46 | + for await (const v of { [Symbol.asyncIterator]: () => ite }) { |
| 47 | + yield await mapperFn(v); |
| 48 | + } |
| 49 | + })()); |
| 50 | + }, |
| 51 | + filter: (filtererFn: any) => { |
| 52 | + if (typeof filtererFn !== "function") { |
| 53 | + throw new TypeError(`${filtererFn} is not a function`); |
| 54 | + } |
| 55 | + return wrapAsyncIterator((async function* () { |
| 56 | + for await (const v of { [Symbol.asyncIterator]: () => ite }) { |
| 57 | + if (await filtererFn(v)) yield v; |
| 58 | + } |
| 59 | + })()); |
| 60 | + }, |
| 61 | + take: (limit) => { |
| 62 | + if (typeof limit === "symbol") { |
| 63 | + throw new TypeError("Cannot convert a Symbol value to a number"); |
| 64 | + } |
| 65 | + if (typeof limit === "bigint") { |
| 66 | + throw new TypeError("Cannot convert a BigInt value to a number"); |
| 67 | + } |
| 68 | + if (limit < 0) throw new RangeError(`Invalid limit value`); |
| 69 | + if (Number.isFinite(limit)) limit = Math.floor(limit); |
| 70 | + if (Number.isNaN(limit)) limit = 0; |
| 71 | + return wrapAsyncIterator((async function* () { |
| 72 | + let remaining = limit; |
| 73 | + for await (const v of { [Symbol.asyncIterator]: () => ite }) { |
| 74 | + if (remaining === 0) break; |
| 75 | + if (remaining !== Infinity) remaining -= 1; |
| 76 | + yield v; |
| 77 | + if (remaining === 0) break; |
| 78 | + } |
| 79 | + })()); |
| 80 | + }, |
| 81 | + drop: (limit) => { |
| 82 | + if (typeof limit === "symbol") { |
| 83 | + throw new TypeError("Cannot convert a Symbol value to a number"); |
| 84 | + } |
| 85 | + if (typeof limit === "bigint") { |
| 86 | + throw new TypeError("Cannot convert a BigInt value to a number"); |
| 87 | + } |
| 88 | + if (limit < 0) throw new RangeError(`Invalid limit value`); |
| 89 | + if (Number.isFinite(limit)) limit = Math.floor(limit); |
| 90 | + if (Number.isNaN(limit)) limit = 0; |
| 91 | + return wrapAsyncIterator((async function* () { |
| 92 | + let remaining = limit; |
| 93 | + for await (const v of { [Symbol.asyncIterator]: () => ite }) { |
| 94 | + if (remaining > 0 && remaining !== Infinity) { |
| 95 | + remaining -= 1; |
| 96 | + continue; |
| 97 | + } |
| 98 | + yield v; |
| 99 | + } |
| 100 | + })()); |
| 101 | + }, |
| 102 | + asIndexedPairs: () => { |
| 103 | + return wrapAsyncIterator((async function* () { |
| 104 | + let i = 0; |
| 105 | + for await (const v of { [Symbol.asyncIterator]: () => ite }) { |
| 106 | + yield [i, v] as [number, T]; |
| 107 | + i += 1; |
| 108 | + } |
| 109 | + })()); |
| 110 | + }, |
| 111 | + flatMap: (mapperFn) => { |
| 112 | + if (typeof mapperFn !== "function") { |
| 113 | + throw new TypeError(`${mapperFn} is not a function`); |
| 114 | + } |
| 115 | + return wrapAsyncIterator((async function* () { |
| 116 | + for await (const v of { [Symbol.asyncIterator]: () => ite }) { |
| 117 | + const arr = await mapperFn(v); |
| 118 | + if (Array.isArray(arr)) { |
| 119 | + for (const e of arr) yield e; |
| 120 | + } else { |
| 121 | + yield arr; |
| 122 | + } |
| 123 | + } |
| 124 | + })()); |
| 125 | + }, |
| 126 | + reduce: (async (reducerFn: any, initialValue: any) => { |
| 127 | + if (typeof reducerFn !== "function") { |
| 128 | + throw new TypeError(`${reducerFn} is not a function`); |
| 129 | + } |
| 130 | + let current = initialValue; |
| 131 | + for await (const v of { [Symbol.asyncIterator]: () => ite }) { |
| 132 | + current = await reducerFn(current, v); |
| 133 | + } |
| 134 | + return current; |
| 135 | + }) as any, |
| 136 | + toArray: async () => { |
| 137 | + const arr: T[] = []; |
| 138 | + for await (const v of { [Symbol.asyncIterator]: () => ite }) { |
| 139 | + arr.push(v); |
| 140 | + } |
| 141 | + return arr; |
| 142 | + }, |
| 143 | + forEach: async (fn) => { |
| 144 | + if (typeof fn !== "function") { |
| 145 | + throw new TypeError(`${fn} is not a function`); |
| 146 | + } |
| 147 | + for await (const v of { [Symbol.asyncIterator]: () => ite }) { |
| 148 | + await fn(v); |
| 149 | + } |
| 150 | + }, |
| 151 | + some: async (fn) => { |
| 152 | + if (typeof fn !== "function") { |
| 153 | + throw new TypeError(`${fn} is not a function`); |
| 154 | + } |
| 155 | + for await (const v of { [Symbol.asyncIterator]: () => ite }) { |
| 156 | + if (await fn(v)) return true; |
| 157 | + } |
| 158 | + return false; |
| 159 | + }, |
| 160 | + every: async (fn) => { |
| 161 | + if (typeof fn !== "function") { |
| 162 | + throw new TypeError(`${fn} is not a function`); |
| 163 | + } |
| 164 | + for await (const v of { [Symbol.asyncIterator]: () => ite }) { |
| 165 | + if (!await fn(v)) return false; |
| 166 | + } |
| 167 | + return true; |
| 168 | + }, |
| 169 | + find: async (fn) => { |
| 170 | + if (typeof fn !== "function") { |
| 171 | + throw new TypeError(`${fn} is not a function`); |
| 172 | + } |
| 173 | + for await (const v of { [Symbol.asyncIterator]: () => ite }) { |
| 174 | + if (await fn(v)) return v; |
| 175 | + } |
| 176 | + }, |
| 177 | + }; |
| 178 | +}; |
0 commit comments