string-object-formatter
Inspired by python named formatter function, replace text inside a string based on object properties names and values.
Example usage
Default delimiters
const Formatter = require('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'
Custom delimiters
const Formatter = require('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'
Formatter
Kind: global class
new Formatter()
Formatter Class
formatter.format(stringToFormat, formatItems) ⇒ string
Formats string according to object
Kind: instance method of Formatter
| stringToFormat | string | |
| formatItems | object | Ex.: {'toReplace': 'replaced'} turns 'example_{toReplace}' to 'example_replaced' |
Formatter.Formatter
Kind: static class of Formatter
new Formatter([startDelimiter], [endDelimiter])
Creates an instance of Formatter.
| [startDelimiter] | string | "'{'" |
| [endDelimiter] | string | "'}'" |