Skip to content

Instantly share code, notes, and snippets.

@snuffyDev
Created April 6, 2022 22:12
Show Gist options
  • Select an option

  • Save snuffyDev/8ddc8dfd1b8c946bd8f475f8e7128fbe to your computer and use it in GitHub Desktop.

Select an option

Save snuffyDev/8ddc8dfd1b8c946bd8f475f8e7128fbe to your computer and use it in GitHub Desktop.
function filterMap(array, cb, predicate) {
let idx = -1;
const length = array.length;
const result = [];
for (; ++idx < length;) {
const res = cb(array[idx], idx, array);
const passes = predicate(res);
if (!passes) {
continue;
};
result[idx] = res;
}
return result;
};
type ItemCallback<T> = (item: T, index: number, array: Array<T>) => Partial<T>;
export function filterMap<T>(
array: Array<T>,
cb: ItemCallback<T>,
predicate: (item: T) => boolean
): T[] {
let idx = -1;
const length = array.length;
const result: T[] = [];
for (; ++idx < length; ) {
const res = cb(array[idx], idx, array);
const passes = predicate(res);
if (!passes) {
continue;
}
result[idx] = res;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment