Skip to content

Commit b1e7a94

Browse files
authored
Update replace_placeholder.ts
1 parent 8d3dc91 commit b1e7a94

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/format/replace_placeholder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
*
1111
* @param input The string to format
1212
* @param formatArgs An object with the corresponding keys matching the ones in the string
13+
* @param placeholderPattern A pattern to search for the keys
1314
* @returns The formatted string
1415
*/
1516
export default function replacePlaceholder(
1617
input : string,
17-
formatArgs : Record<string, string | number>
18+
formatArgs : Record<string, string | number>,
19+
placeholderPattern : RegExp = /(?<=(?:[^\\])(?:\\\\)*)\{\{\s*(\w+)\s*}}/gm
1820
) : string {
19-
return input.replace(/(?<=(?:[^\\])(?:\\\\)*)\{\{\s*(\w+)\s*}}/gm, (_, formatKey : string) : string => {
21+
return input.replace(placeholderPattern, (_, formatKey : string) : string => {
2022
if (!Object.hasOwn(formatArgs, formatKey)) {
2123
throw new ReferenceError(`Format key '${formatKey}' does not exist on the provided format object`);
2224
}

0 commit comments

Comments
 (0)