Python inspired named template formatter for Javascript strings
npm install string-object-formatterInspired by python named formatter function, replace text inside a string based on object properties names and values.
#### Import it in your code
In a CommonJS environment
``javascript`
const Formatter = require("string-object-formatter");
Using import
`javascript`
import Formatter from "string-object-formatter";
#### Default delimiters
`javascript
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
`javascript
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'
`
- endDelimiter
- startDelimiter
- format
\+ new default(startDelimiter?: _string_, endDelimiter?: _string_, silent: _boolean_): _default_
Creates an instance of Formatter.
memberof Formatter
#### Parameters:
| Name | Type | Default value |
| :--------------- | :------- | :------------ |
| startDelimiter | _string_ | '{' |endDelimiter
| | _string_ | '}' |
Returns: _default_
• endDelimiter: _string_
---
• startDelimiter: _string_
▸ format(stringToFormat: _string_, formatItems: _Record
Formats string according to object
memberof Formatter
#### Parameters:
| Name | Type | Description |
| :--------------- | :---------------------- | :------------------- | --------------------------------------------------------------------------------- |
| stringToFormat | _string_ | The string to format |formatItems` | \_Record
|
Returns: _string_
The replaced string