Skip to content

Instantly share code, notes, and snippets.

@FrameMuse
Created April 5, 2022 12:18
Show Gist options
  • Save FrameMuse/b3aae24540838c2c66ebf0284aceaeda to your computer and use it in GitHub Desktop.
Save FrameMuse/b3aae24540838c2c66ebf0284aceaeda to your computer and use it in GitHub Desktop.
Just a plain interpolation utility but with extracted interpolations to have a look of what variables you can interpolate into
type ExtractInterpolations<T extends string> = T extends `${infer _Start}{${infer V}}${infer Rest}` ? V | ExtractInterpolations<Rest> : never
/**
* Interpolates {variable} in string
*/
function interpolate<T extends string>(value: T, vars: Record<ExtractInterpolations<T>, string | number>): string {
const varKeys = Object.keys(vars) as ExtractInterpolations<T>[]
return varKeys.reduce((result: string, next) => result.replace(new RegExp(`{${next}}`, "g"), String(vars[next])), value)
}
export default function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment