{"name":"string-format","version":"0.5.0","description":"Adds a `format` method to `String.prototype`. Inspired by Python's `str.format()`.","author":{"name":"David Chambers","email":"dc@davidchambers.me"},"keywords":["string","formatting","language","util"],"main":"./lib/string-format","scripts":{"prepublish":"make clean && make","test":"make test"},"homepage":"https://github.com/davidchambers/string-format","bugs":{"url":"https://github.com/davidchambers/string-format/issues"},"licenses":[{"type":"WTFPL","url":"https://raw.github.com/davidchambers/string-format/master/LICENSE"}],"repository":{"type":"git","url":"git://github.com/davidchambers/string-format.git"},"files":["README.md","lib/string-format.js","package.json"],"devDependencies":{"coffee-script":"1.8.x","mocha":"2.x.x","ramda":"0.8.x","xyz":"0.5.x"},"gitHead":"e98595d385a460edb8fe9bd384fe1af3da307a31","_id":"string-format@0.5.0","_shasum":"bfc4a69a250f17f273d97336797daf5dca6ecf30","_from":".","_npmVersion":"1.4.28","_npmUser":{"name":"davidchambers","email":"dc@davidchambers.me"},"maintainers":[{"name":"davidchambers","email":"dc@hashify.me"}],"dist":{"shasum":"bfc4a69a250f17f273d97336797daf5dca6ecf30","tarball":"http://registry-npm.ywwl.com/string-format/download/string-format-0.5.0.tgz","integrity":"sha512-c/CiKQMy7uuEzi+Tsvnn63/PQw/F7IOSLHNuQ44Eypd0x5VvFnDXMd2T9H0ntphv8nrHAKoZcINPb/yitOAB/g==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCEPqzu2vm0N6Kpg31/VkkgD4jC+Ox7bFgB4U5KC2uzTgIgRScao20RQyVxPTaUk2cqZk4Mtgnd41e0HebHttrxkTs="}],"size":3502,"noattachment":false,"key":"/string-format/-/string-format-0.5.0.tgz"},"directories":{},"readme":"# string-format\n\nstring-format is a small JavaScript library for formatting strings, based on\nPython's [`str.format()`][1]. For example:\n\n```javascript\n'\"{firstName} {lastName}\" <{email}>'.format(user)\n// => '\"Jane Smith\" <jsmith@example.com>'\n```\n\nThe equivalent concatenation:\n\n```javascript\n'\"' + user.firstName + ' ' + user.lastName + '\" <' + user.email + '>'\n// => '\"Jane Smith\" <jsmith@example.com>'\n```\n\n### Installation\n\n#### Node\n\n1.  Install:\n\n    ```console\n    $ npm install string-format\n    ```\n\n2.  Require:\n\n    ```javascript\n    const format = require('string-format')\n    ```\n\n#### Browser\n\n1.  Define `window.format`:\n\n    ```html\n    <script src=\"path/to/string-format.js\"></script>\n    ```\n\n### Modes\n\nstring-format can be used in two modes: [function mode](#function-mode) and\n[method mode](#method-mode).\n\n#### Function mode\n\n```javascript\nformat('Hello, {}!', 'Alice')\n// => 'Hello, Alice!'\n```\n\nIn this mode the first argument is a template string and the remaining\narguments are values to be interpolated.\n\n#### Method mode\n\n```javascript\n'Hello, {}!'.format('Alice')\n// => 'Hello, Alice!'\n```\n\nIn this mode values to be interpolated are supplied to the `format` method\nof a template string. This mode is not enabled by default. The method must\nfirst be defined via [`format.extend`](#formatextendprototype-transformers):\n\n```javascript\nformat.extend(String.prototype, {})\n```\n\n`format(template, $0, $1, …, $N)` and `template.format($0, $1, …, $N)` can then\nbe used interchangeably.\n\n### `format(template, $0, $1, …, $N)`\n\nReturns the result of replacing each `{…}` placeholder in the template string\nwith its corresponding replacement.\n\nPlaceholders may contain numbers which refer to positional arguments:\n\n```javascript\n'{0}, you have {1} unread message{2}'.format('Holly', 2, 's')\n// => 'Holly, you have 2 unread messages'\n```\n\nUnmatched placeholders produce no output:\n\n```javascript\n'{0}, you have {1} unread message{2}'.format('Steve', 1)\n// => 'Steve, you have 1 unread message'\n```\n\nA format string may reference a positional argument multiple times:\n\n```javascript\n\"The name's {1}. {0} {1}.\".format('James', 'Bond')\n// => \"The name's Bond. James Bond.\"\n```\n\nPositional arguments may be referenced implicitly:\n\n```javascript\n'{}, you have {} unread message{}'.format('Steve', 1)\n// => 'Steve, you have 1 unread message'\n```\n\nA format string must not contain both implicit and explicit references:\n\n```javascript\n'My name is {} {}. Do you like the name {0}?'.format('Lemony', 'Snicket')\n// => ValueError: cannot switch from implicit to explicit numbering\n```\n\n`{{` and `}}` in format strings produce `{` and `}`:\n\n```javascript\n'{{}} creates an empty {} in {}'.format('dictionary', 'Python')\n// => '{} creates an empty dictionary in Python'\n```\n\nDot notation may be used to reference object properties:\n\n```javascript\nconst bobby = {firstName: 'Bobby', lastName: 'Fischer'}\nconst garry = {firstName: 'Garry', lastName: 'Kasparov'}\n\n'{0.firstName} {0.lastName} vs. {1.firstName} {1.lastName}'.format(bobby, garry)\n// => 'Bobby Fischer vs. Garry Kasparov'\n```\n\n`0.` may be omitted when referencing a property of `{0}`:\n\n```javascript\nconst repo = {owner: 'davidchambers', slug: 'string-format'}\n\n'https://github.com/{owner}/{slug}'.format(repo)\n// => 'https://github.com/davidchambers/string-format'\n```\n\nIf the referenced property is a method, it is invoked with no arguments to\ndetermine the replacement:\n\n```javascript\nconst sheldon = {\n  firstName: 'Sheldon',\n  lastName: 'Cooper',\n  dob: new Date('1970-01-01'),\n  fullName: function() { return this.firstName + ' ' + this.lastName },\n  quip: function() { return 'Bazinga!' },\n}\n\n'{fullName} was born at precisely {dob.toISOString}'.format(sheldon)\n// => 'Sheldon Cooper was born at precisely 1970-01-01T00:00:00.000Z'\n\n\"I've always wanted to go to a goth club. {quip.toUpperCase}\".format(sheldon)\n// => \"I've always wanted to go to a goth club. BAZINGA!\"\n```\n\n### `format.create(transformers)`\n\nThis function takes an object mapping names to transformers and returns a\nformatting function. A transformer is applied if its name appears, prefixed\nwith `!`, after a field name in a template string.\n\n```javascript\nconst fmt = format.create({\n  escape: s => s.replace(/[&<>\"'`]/g, c => '&#' + c.charCodeAt(0) + ';'),\n  upper: s => s.toUpperCase(),\n})\n\nfmt('Hello, {!upper}!', 'Alice')\n// => 'Hello, ALICE!'\n\nconst restaurant = {name: 'Anchor & Hope', url: 'http://anchorandhopesf.com/'}\n\nfmt('<a href=\"{url!escape}\">{name!escape}</a>', restaurant)\n// => '<a href=\"http://anchorandhopesf.com/\">Anchor &#38; Hope</a>'\n```\n\n### `format.extend(prototype, transformers)`\n\nThis function takes a prototype (presumably `String.prototype`) and an object\nmapping names to transformers, and defines a `format` method on the prototype.\nA transformer is applied if its name appears, prefixed with `!`, after a field\nname in a template string.\n\n```javascript\nformat.extend(String.prototype, {\n  escape: s => s.replace(/[&<>\"'`]/g, c => '&#' + c.charCodeAt(0) + ';'),\n  upper: s => s.toUpperCase(),\n})\n\n'Hello, {!upper}!'.format('Alice')\n// => 'Hello, ALICE!'\n\nconst restaurant = {name: 'Anchor & Hope', url: 'http://anchorandhopesf.com/'}\n\n'<a href=\"{url!escape}\">{name!escape}</a>'.format(restaurant)\n// => '<a href=\"http://anchorandhopesf.com/\">Anchor &#38; Hope</a>'\n```\n\n### Running the test suite\n\n```console\n$ npm install\n$ npm test\n```\n\n\n[1]: http://docs.python.org/library/stdtypes.html#str.format\n","publish_time":1422083132068,"_cnpm_publish_time":1422083132068,"dist-tags":{"latest":"2.0.0"}}