Initial commit
This commit is contained in:
20
wwwroot/core/utils/stringFormatter.js
Normal file
20
wwwroot/core/utils/stringFormatter.js
Normal file
@@ -0,0 +1,20 @@
|
||||
export default class StringFormatter{
|
||||
|
||||
/**
|
||||
* Replaces tokens in a string with their corresponding values.
|
||||
*
|
||||
* Each token in the string should match a key in the `values` array.
|
||||
* For example, given `s = "Hi :name !"` and `values = [{ key: ":name", value: "Foo" }]`,
|
||||
* the function will return "Hi Foo !".
|
||||
*
|
||||
* @param {string} s - The string containing tokens to replace.
|
||||
* @param {Array<{key: string, value: string}>} values - An array of objects mapping each token (`key`) to its replacement (`value`).
|
||||
* @returns {string} The string with tokens replaced by their corresponding values.
|
||||
*/
|
||||
static format(s, values) {
|
||||
values.forEach(v => {
|
||||
s = s.replace(`:${v.key}`, v.value);
|
||||
});
|
||||
return s;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user