JSPM

string-object-formatter

1.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 210
  • Score
    100M100P100Q94060F
  • 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

Default delimiters

const Formatter = require('string-object-formatter');
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 = require('string-object-formatter');
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'

Formatter

Kind: global class

new Formatter()

Formatter Class

formatter.format(stringToFormat, formatItems) ⇒ string

Formats string according to object

Kind: instance method of Formatter

Param Type Description
stringToFormat string
formatItems object Ex.: {'toReplace': 'replaced'} turns 'example_{toReplace}' to 'example_replaced'

Formatter.Formatter

Kind: static class of Formatter

new Formatter([startDelimiter], [endDelimiter])

Creates an instance of Formatter.

Param Type Default
[startDelimiter] string "'{'"
[endDelimiter] string "'}'"