Inspired by python named formatter function, replace text inside a string based on object properties names and values.
In a CommonJS environment
const Formatter = require("string-object-formatter");Using import
import Formatter from "string-object-formatter";const formatter = new Formatter();
const toFormat = "My name is {firstName} {lastName}";
const formatted = formatter.format(toFormat, {
firstName: "John",
lastName: "Doe",
});
// formatted is 'My name is John Doe'const formatter = new Formatter("{{", "}}");
const toFormat = "My name is {{firstName}} {{lastName}}";
const formatted = formatter.format(toFormat, {
firstName: "John",
lastName: "Doe",
});
// formatted is 'My name is John Doe'+ new default(startDelimiter?: string, endDelimiter?: string, silent: boolean): default
Creates an instance of Formatter.
memberof Formatter
| Name | Type | Default value |
|---|---|---|
startDelimiter |
string | '{' |
endDelimiter |
string | '}' |
Returns: default
• endDelimiter: string
• startDelimiter: string
▸ format(stringToFormat: string, formatItems: Record<string, string | number>): string
Formats string according to object
memberof Formatter
| Name | Type | Description |
| :--------------- | :---------------------- | :------------------- | --------------------------------------------------------------------------------- |
| stringToFormat | string | The string to format |
| formatItems | _Record<string, string | number>_ | Ex.: {'toReplace': 'replaced'} turns 'example_{toReplace}' to 'example_replaced' |
Returns: string
The replaced string