Last active
October 20, 2022 07:26
-
-
Save SugarDarius/7d29bf51d9bb4447059185d01bd6f78a to your computer and use it in GitHub Desktop.
TypeScript recursive map
This file contains 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
/* | |
* mapRecursive | |
* author: Aurélien Dupays Dexemple | |
*/ | |
export const isUndefined = (obj: any): obj is undefined => typeof obj === 'undefined'; | |
export function mapRecursive<TInput, TOutput>(fn: (e: TInput, index?: number) => TOutput, [first, ...rest]: TInput[], index: number = 0): TOutput[] { | |
return isUndefined(first) ? [] : [fn(first, index), ...mapRecursive(fn, rest, ++index)]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @mcaple.
A relevant example of using this function is mapping on a tree data structure with a lower complexity.
KR,