An open API service indexing awesome lists of open source software.

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

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