3
0
Fork 0
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:
Dragory 2020-08-10 01:31:32 +03:00
parent 1d5dd10bff
commit 0af03978e5
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
3 changed files with 94 additions and 15 deletions

View 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;
},
[],
);
}