https://github.com/alrico88/string-object-formatter
Python-inspired named template formatter for Javascript strings
https://github.com/alrico88/string-object-formatter
format javascript named python string
Last synced: about 1 month ago
JSON representation
Python-inspired named template formatter for Javascript strings
- Host: GitHub
- URL: https://github.com/alrico88/string-object-formatter
- Owner: alrico88
- Created: 2020-04-15T14:12:59.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-10-16T08:53:57.000Z (over 3 years ago)
- Last Synced: 2025-03-24T04:34:03.555Z (about 1 year ago)
- Topics: format, javascript, named, python, string
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/string-object-formatter
- Size: 250 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# string-object-formatter
Inspired by python named formatter function, replace text inside a string based on object properties names and values.
### Example usage
#### 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'
```
## Table of contents
### Constructors
- [constructor](#constructor)
### Properties
- [endDelimiter](#enddelimiter)
- [startDelimiter](#startdelimiter)
### Methods
- [format](#format)
## Constructors
### constructor
\+ **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_](#)
## Properties
### endDelimiter
• **endDelimiter**: _string_
---
### startDelimiter
• **startDelimiter**: _string_
## Methods
### format
▸ **format**(`stringToFormat`: _string_, `formatItems`: _Record_): _string_
Formats string according to object
**`memberof`** Formatter
#### Parameters:
| Name | Type | Description |
| :--------------- | :---------------------- | :------------------- | --------------------------------------------------------------------------------- |
| `stringToFormat` | _string_ | The string to format |
| `formatItems` | \_Record\_ | Ex.: {'toReplace': 'replaced'} turns 'example\_{toReplace}' to 'example_replaced' |
**Returns:** _string_
The replaced string