mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Fixes to RegExpRunner usage in !search and !bansearch, validate that input regex is valid
This commit is contained in:
parent
1d5dd10bff
commit
0af03978e5
3 changed files with 94 additions and 15 deletions
54
backend/src/utils/async.ts
Normal file
54
backend/src/utils/async.ts
Normal file
|
@ -0,0 +1,54 @@
|
|||
import { Awaitable } from "knub/dist/utils";
|
||||
|
||||
export async function asyncReduce<T, V>(
|
||||
arr: T[],
|
||||
callback: (accumulator: V, currentValue: T, index: number, array: T[]) => Awaitable<V>,
|
||||
initialValue?: V,
|
||||
): Promise<V> {
|
||||
let accumulator;
|
||||
let arrayToIterate;
|
||||
if (initialValue !== undefined) {
|
||||
accumulator = initialValue;
|
||||
arrayToIterate = arr;
|
||||
} else {
|
||||
accumulator = arr[0];
|
||||
arrayToIterate = arr.slice(1);
|
||||
}
|
||||
|
||||
for (const [i, currentValue] of arr.entries()) {
|
||||
accumulator = await callback(accumulator, currentValue, i, arr);
|
||||
}
|
||||
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
export function asyncFilter<T>(
|
||||
arr: T[],
|
||||
callback: (element: T, index: number, array: T[]) => Awaitable<boolean>,
|
||||
): Promise<T[]> {
|
||||
return asyncReduce(
|
||||
arr,
|
||||
async (newArray, element, i, _arr) => {
|
||||
if (await callback(element, i, _arr)) {
|
||||
newArray.push(element);
|
||||
}
|
||||
|
||||
return newArray;
|
||||
},
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
export function asyncMap<T, V>(
|
||||
arr: T[],
|
||||
callback: (currentValue: T, index: number, array: T[]) => Awaitable<V>,
|
||||
): Promise<V[]> {
|
||||
return asyncReduce(
|
||||
arr,
|
||||
async (newArray, element, i, _arr) => {
|
||||
newArray.push(await callback(element, i, _arr));
|
||||
return newArray;
|
||||
},
|
||||
[],
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue