-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't use sanitization for paths but validate
related to #472
- Loading branch information
Showing
5 changed files
with
322 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
// https://stackoverflow.com/questions/7975005/format-a-javascript-string-using-placeholders-and-an-object-of-substitutions | ||
export function formatString(string: string, replacements: Record<string, string>, hideEmpty = false): string { | ||
// return string.replace(/{(\d+)}/g, (match, number) => { | ||
// return typeof args[number] !== "undefined" ? args[number] : match; | ||
// }); | ||
/** | ||
* Replaces placeholders in a string with corresponding values from a replacements object. | ||
* @param string - The string containing placeholders to replace. | ||
* @param replacements - An object containing key-value pairs where the key is the placeholder and the value is the replacement. | ||
* @param hideEmpty - If true, empty placeholders will be replaced with an empty string. Otherwise, they will be left as is. | ||
* @returns The string with placeholders replaced with corresponding values from the replacements object. | ||
*/ | ||
export function formatString( | ||
string: string, | ||
replacements: Record<string, string>, | ||
hideEmpty = false | ||
): string { | ||
return string.replace( | ||
/{(\w+)}/g, | ||
/{(\w+)}/g, | ||
(placeholderWithDelimiters, placeholderWithoutDelimiters) => | ||
hideEmpty ? | ||
replacements[placeholderWithoutDelimiters] !== undefined ? replacements[placeholderWithoutDelimiters] : "" | ||
: | ||
replacements[placeholderWithoutDelimiters] || placeholderWithDelimiters | ||
hideEmpty | ||
? replacements[placeholderWithoutDelimiters] !== undefined | ||
? replacements[placeholderWithoutDelimiters] | ||
: "" | ||
: replacements[placeholderWithoutDelimiters] || | ||
placeholderWithDelimiters | ||
); | ||
} | ||
} |
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
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
Oops, something went wrong.