smartarray/ts/index.ts

32 lines
730 B
TypeScript
Raw Normal View History

2023-08-02 12:39:58 +00:00
export const filter = async <T>(
arrayArg: T[],
filterFunction: (itemArg: T) => Promise<boolean>
): Promise<T[]> => {
2021-06-29 22:30:11 +00:00
const returnArray: T[] = [];
for (const itemArg of arrayArg) {
const filterResult = await filterFunction(itemArg);
if (filterResult) {
returnArray.push(itemArg);
}
}
return returnArray;
2023-08-02 12:39:58 +00:00
};
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;
};