3.0.0 • Published 2 years ago

string-object-formatter v3.0.0

Weekly downloads
59
License
MIT
Repository
github
Last release
2 years ago

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

const Formatter = require('string-object-formatter');

Using import

import Formatter from 'string-object-formatter';

Default delimiters

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 = 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

Properties

Methods

Constructors

constructor

+ new default(startDelimiter?: string, endDelimiter?: string, silent: boolean): default

Creates an instance of Formatter.

memberof Formatter

Parameters:

NameTypeDefault value
startDelimiterstring'{'
endDelimiterstring'}'
silentbooleanfalse

Returns: default

Properties

silent

silent: boolean


endDelimiter

endDelimiter: string


startDelimiter

startDelimiter: string

Methods

format

format(stringToFormat: string, formatItems: FormatObject): string

Formats string according to object

memberof Formatter

Parameters:

NameTypeDescription
stringToFormatstringThe string to format
formatItemsFormatObjectEx.: {'toReplace': 'replaced'} turns 'example_{toReplace}' to 'example_replaced'

Returns: string

The replaced string