Created
August 2, 2022 17:08
-
-
Save taxilian/3bc98bdb0f3258d48520912a5ece956f to your computer and use it in GitHub Desktop.
Typescript string template literal function creator
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
/** | |
* Use this with a template literal in order to create reusable string template; | |
* use interpolation to add strings for each variable you want to use in the template. | |
* | |
* e.g.: | |
* | |
* const reUsableStringTemplate = stringTpl`${'name'} and ${'otherName'} are going to ${'place'}`; | |
* | |
* You can then call it with: | |
* | |
* const filled = reUsableStringTemplate({name: 'John', otherName: 'Jane', place: 'Paris'}); | |
* // John and Jane are going to Paris | |
* | |
* reUsableStringTemplate will have types and know the names of your variables | |
* | |
* @returns String template function with full typescript types | |
*/ | |
export function stringTpl<keys extends string>(parts: TemplateStringsArray, ...keys: keys[]) { | |
return (opts: Record<keys, any>) => { | |
let outStr = ''; | |
for (let i = 0; i < parts.length; ++i) { | |
outStr += parts[i]; | |
const key = keys.shift(); | |
if (key && key in opts) { | |
outStr += opts[key]; | |
} else { | |
outStr += key ?? ''; | |
} | |
} | |
return outStr; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment