20 lines
794 B
JavaScript
20 lines
794 B
JavaScript
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;
|
|
}
|
|
} |