mirror of
https://github.com/lifegpc/eh-downloader.git
synced 2026-06-06 05:38:44 +08:00
Update asyncFilter
This commit is contained in:
40
utils.ts
40
utils.ts
@@ -81,14 +81,25 @@ export function try_remove_sync(
|
||||
}
|
||||
}
|
||||
|
||||
export async function asyncFilter<T>(
|
||||
arr: T[],
|
||||
callback: (element: T, index: number, array: T[]) => Promise<boolean>,
|
||||
const fail = Symbol();
|
||||
|
||||
export async function asyncFilter<T, V>(
|
||||
arr: ArrayLike<T>,
|
||||
callback: (
|
||||
this: V | undefined,
|
||||
element: T,
|
||||
index: number,
|
||||
array: ArrayLike<T>,
|
||||
) => Promise<boolean>,
|
||||
thisArg?: V,
|
||||
): Promise<T[]> {
|
||||
const fail = Symbol();
|
||||
const t = <[T][]> (await Promise.all(
|
||||
arr.map(async (item, index, array) =>
|
||||
(await callback(item, index, array)) ? [item] : fail
|
||||
map(
|
||||
arr,
|
||||
async (item, index, array) =>
|
||||
(await callback.apply(thisArg, [item, index, array]))
|
||||
? [item]
|
||||
: fail,
|
||||
),
|
||||
)).filter((i) => i !== fail);
|
||||
return t.map((t) => t[0]);
|
||||
@@ -231,3 +242,20 @@ export async function asyncEvery<T, V>(
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function map<T, S, V>(
|
||||
arr: ArrayLike<T>,
|
||||
callback: (
|
||||
this: S | undefined,
|
||||
element: T,
|
||||
index: number,
|
||||
array: ArrayLike<T>,
|
||||
) => V,
|
||||
thisArg?: S,
|
||||
) {
|
||||
const re: V[] = [];
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
re.push(callback.apply(thisArg, [arr[i], i, arr]));
|
||||
}
|
||||
return re;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
asyncForEach,
|
||||
calFileMd5,
|
||||
filterFilename,
|
||||
map,
|
||||
promiseState,
|
||||
PromiseStatus,
|
||||
sleep,
|
||||
@@ -47,11 +48,19 @@ Deno.test("asyncFilter_test", async () => {
|
||||
});
|
||||
assertEquals(r, [3]);
|
||||
const e = [new Promise<number>((res) => setTimeout(() => res(100), 100))];
|
||||
const v = await asyncFilter(e, async (t) => {
|
||||
const s = await promiseState(t);
|
||||
const v = await asyncFilter(e, async function (d) {
|
||||
assertEquals(this, t);
|
||||
const s = await promiseState(d);
|
||||
return s.status === PromiseStatus.Pending;
|
||||
});
|
||||
}, t);
|
||||
assertEquals(v, e);
|
||||
const e2 = { 0: e[0], length: 1 };
|
||||
const v2 = await asyncFilter(e2, async function (d) {
|
||||
assertEquals(this, t);
|
||||
const s = await promiseState(d);
|
||||
return s.status === PromiseStatus.Pending;
|
||||
}, t);
|
||||
assertEquals(v, v2);
|
||||
await Promise.allSettled(e);
|
||||
});
|
||||
|
||||
@@ -128,3 +137,13 @@ Deno.test("asyncEvery_test", async () => {
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("map_test", () => {
|
||||
const arr = { 0: 1, 1: 2, length: 2 };
|
||||
const d = { d: 1 };
|
||||
const re = map(arr, function (n) {
|
||||
assertEquals(this, d);
|
||||
return n + 2;
|
||||
}, d);
|
||||
assertEquals(re, [3, 4]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user