JSPM

strfmt

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 10
  • Score
    100M100P100Q42848F
  • License MIT

Named string interpolation, with format specifiers.

Package Exports

  • strfmt

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 (strfmt) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

strfmt.js npm Version Build Status Coverage Status

Named string interpolation, with format specifiers.

Usage

var tmpl = '{ date.day : %02d }-{ date.month : %02d }-{ date.year }';
var data = {
  date: {
    day: 25,
    month: 1,
    year: 2015
  }
};

strfmt(tmpl)(data); //=> '25-01-2015'
  1. Keys in tmpl are delimited by { and }.
  2. Reference a nested value in data using a dot-delimited string (eg. date.day above).
  3. Optionally state a format specifier for each key after a : character. The %s format specifier is assumed for keys without one.

More usage examples are in the tests.

Note that under the hood, strfmt uses:

API

strfmt(tmpl)(data)

Returns a String, the result of interpolating data into tmpl.

  • tmpl — The template String.
  • data — An Object literal of values.

Installation

Install via npm:

$ npm i --save strfmt

Install via bower:

$ bower i --save yuanqing/strfmt

To use strfmt in the browser, include the minified script in your HTML:

<body>
  <!-- ... -->
  <script src="path/to/dist/strfmt.min.js"></script>
  <script>
    // strfmt available here
  </script>
</body>

Changelog

  • 0.1.0
    • Initial release

License

MIT license