JSPM

string-object-formatter

2.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 723
  • Score
    100M100P100Q98720F
  • License MIT

Python inspired named template formatter for Javascript strings

Package Exports

  • string-object-formatter

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (string-object-formatter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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

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): default

Creates an instance of Formatter.

memberof Formatter

Parameters:

Name Type Default value
startDelimiter string '{'
endDelimiter string '}'

Returns: default

Defined in: index.ts:12

Properties

endDelimiter

endDelimiter: string

Defined in: index.ts:12


startDelimiter

startDelimiter: string

Defined in: index.ts:11

Methods

format

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

Formats string according to object

memberof Formatter

Parameters:

Name Type Description
stringToFormat string The string to format
formatItems FormatObject Ex.: {'toReplace': 'replaced'} turns 'example_{toReplace}' to 'example_replaced'

Returns: string

The replaced string

Defined in: index.ts:36