46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
export const map = async <T = any, R = any>(
|
|
arrayArg: T[],
|
|
mapFunction: (itemArg: T) => Promise<R>
|
|
): Promise<R[]> => {
|
|
const returnArray: R[] = [];
|
|
for (const itemArg of arrayArg) {
|
|
const mapResult = await mapFunction(itemArg);
|
|
if (mapResult !== undefined) {
|
|
returnArray.push(mapResult);
|
|
}
|
|
}
|
|
return returnArray;
|
|
};
|
|
|
|
export const filter = async <T>(
|
|
arrayArg: T[],
|
|
filterFunction: (itemArg: T) => Promise<boolean>
|
|
): Promise<T[]> => {
|
|
const returnArray: T[] = [];
|
|
for (const itemArg of arrayArg) {
|
|
const filterResult = await filterFunction(itemArg);
|
|
if (filterResult) {
|
|
returnArray.push(itemArg);
|
|
}
|
|
}
|
|
return returnArray;
|
|
};
|
|
|
|
export const deduplicate = async <T, K>(
|
|
arrayArg: T[],
|
|
keyCreation: (itemArg: T) => Promise<K>
|
|
): Promise<T[]> => {
|
|
const keysSet: Set<K> = new Set();
|
|
const returnArray: T[] = [];
|
|
|
|
for (const itemArg of arrayArg) {
|
|
const key = await keyCreation(itemArg);
|
|
if (!keysSet.has(key)) {
|
|
keysSet.add(key);
|
|
returnArray.push(itemArg);
|
|
}
|
|
}
|
|
|
|
return returnArray;
|
|
};
|