Created
April 6, 2022 22:12
-
-
Save snuffyDev/8ddc8dfd1b8c946bd8f475f8e7128fbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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