-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f80712f
commit a19e409
Showing
2 changed files
with
21 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const TPL = /\{(\d+)\}/g; | ||
|
||
/** | ||
* Takes a string template with embedded `{number}` style terms and any | ||
* number of args. Replaces numbered terms with their respective args | ||
* given. | ||
* | ||
* @example | ||
* ```ts | ||
* interpolate("let {0}: {2} = {1};", "a", 42, "number") | ||
* // "let a: number = 42;" | ||
* ``` | ||
* | ||
* @param src | ||
* @param args | ||
*/ | ||
export const interpolate = (src: string, ...args: any[]) => | ||
args.length > 0 | ||
? src.replace(TPL, (m) => String(args[parseInt(m[1], 10)])) | ||
: src; |