Last active
June 25, 2022 13:04
-
-
Save OlaoluwaM/d61c00bd37c08f0cdb70cc3e3c2947d1 to your computer and use it in GitHub Desktop.
Revisions
-
OlaoluwaM revised this gist
Jun 25, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ const namedParamExampleFn = toNamedParams(exampleFn) console.log(namedParamExampleFn({c: true, a: 1})) // test #2 function testFn(a: number, b: string) { return a + b } -
OlaoluwaM revised this gist
Jun 25, 2022 . 2 changed files with 16 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ // test #1 const exampleFn = (a: number, b:string, c: boolean) => [a, b, c] const namedParamExampleFn = toNamedParams(exampleFn) console.log(namedParamExampleFn({c: true, a: 1})) # test #2 function testFn(a: number, b: string) { return a + b } const namedTestFn = toNamedParams(testFn) console.log(namedTestFn({b: 's', a: 1})) 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 charactersOriginal file line number Diff line number Diff line change @@ -45,14 +45,4 @@ function generateInitialParamsObj(functionParams: FuncParams) { function generateEmptyParamObj(obj: Record<string, undefined>, param: string): Record<string, undefined> { return {...obj, [param]: undefined} } -
OlaoluwaM created this gist
Jun 25, 2022 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,58 @@ type AnyFunction<P = any, RT = unknown> = (...args: P[]) => RT type FuncParams = string[] function toNamedParams<Fn extends AnyFunction>(fn: Fn) { const initialFuncParamObj = generateInitialParamsObj(getFuncPositionalParams(fn)) return (passedArgsObj: Record<string, Parameters<Fn>[number]) => { const funcArgsArr = Object.values({...initialFuncParamObj, ...passedArgsObj}) return fn.apply(null, funcArgsArr) } } function getFuncPositionalParams(fn: AnyFunction) { const REGEX_FOR = { CONTENT_WITHIN_PARENTHESES: /\([^)]*\)/m, PARENTHESES: /\(|\)/g, SPACE_AND_COMMA_DELIMITED_LIST: /\s*,\s*/ } const funcParamsStrMatchArr = fn.toString().match(REGEX_FOR.CONTENT_WITHIN_PARENTHESES) if (!funcParamsStrMatchArr) throw new Error(`Could not extract args for ${fn.name} function`) const [funcParamsStr] = funcParamsStrMatchArr const funcParamsStrWithoutBraces = funcParamsStr.replace(REGEX_FOR.PARENTHESES, '') const funcParamsArr = funcParamsStrWithoutBraces.split(REGEX_FOR.SPACE_AND_COMMA_DELIMITED_LIST).map(removeEverythingAfterFirstSpace) return funcParamsArr } function removeEverythingAfterFirstSpace(str: string): string { const SPACE_CHAR = ' ' const indexOfFirstSpaceCharInStr = str.indexOf(SPACE_CHAR) if (indexOfFirstSpaceCharInStr === -1) return str const truncatedStr = str.slice(0, indexOfFirstSpaceCharInStr) return truncatedStr } function generateInitialParamsObj(functionParams: FuncParams) { const funcParamsObj = functionParams.reduce(generateEmptyParamObj, {}) return funcParamsObj } function generateEmptyParamObj(obj: Record<string, undefined>, param: string): Record<string, undefined> { return {...obj, [param]: undefined} } // Example Usage function testFn(a: number, b: string) { return a + b } const namedTestFn = toNamedParams(testFn) console.log(namedTestFn({b: 's', a: 1}))