string-format
Advanced tools
Comparing version
{ | ||
"name": "string-format", | ||
"version": "0.5.0", | ||
"description": "Adds a `format` method to `String.prototype`. Inspired by Python's `str.format()`.", | ||
"version": "1.0.0", | ||
"description": "String formatting inspired by Python's str.format()", | ||
"author": "David Chambers <dc@davidchambers.me>", | ||
@@ -12,15 +12,5 @@ "keywords": [ | ||
], | ||
"main": "./lib/string-format", | ||
"scripts": { | ||
"prepublish": "make clean && make", | ||
"test": "make test" | ||
}, | ||
"homepage": "https://github.com/davidchambers/string-format", | ||
"bugs": "https://github.com/davidchambers/string-format/issues", | ||
"licenses": [ | ||
{ | ||
"type": "WTFPL", | ||
"url": "https://raw.github.com/davidchambers/string-format/master/LICENSE" | ||
} | ||
], | ||
"license": "(WTFPL OR MIT)", | ||
"repository": { | ||
@@ -31,12 +21,17 @@ "type": "git", | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"lib/string-format.js", | ||
"index.js", | ||
"package.json" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"coffee-script": "1.8.x", | ||
"mocha": "2.x.x", | ||
"ramda": "0.8.x", | ||
"xyz": "0.5.x" | ||
"sanctuary-scripts": "1.2.x" | ||
}, | ||
"scripts": { | ||
"doctest": "sanctuary-doctest", | ||
"lint": "sanctuary-lint", | ||
"release": "sanctuary-release", | ||
"test": "npm run lint && sanctuary-test && npm run doctest" | ||
} | ||
} |
@@ -1,4 +0,4 @@ | ||
# String::format | ||
# string-format | ||
String::format is a small JavaScript library for formatting strings, based on | ||
string-format is a small JavaScript library for formatting strings, based on | ||
Python's [`str.format()`][1]. For example: | ||
@@ -24,7 +24,11 @@ | ||
$ npm install string-format | ||
```console | ||
$ npm install string-format | ||
``` | ||
2. Require: | ||
var format = require('string-format') | ||
```javascript | ||
const format = require('string-format') | ||
``` | ||
@@ -35,7 +39,9 @@ #### Browser | ||
<script src="path/to/string-format.js"></script> | ||
```html | ||
<script src="path/to/string-format.js"></script> | ||
``` | ||
### Modes | ||
String::format can be used in two modes: [function mode](#function-mode) and | ||
string-format can be used in two modes: [function mode](#function-mode) and | ||
[method mode](#method-mode). | ||
@@ -65,3 +71,3 @@ | ||
```javascript | ||
format.extend(String.prototype) | ||
format.extend(String.prototype, {}) | ||
``` | ||
@@ -122,4 +128,4 @@ | ||
```javascript | ||
var bobby = {firstName: 'Bobby', lastName: 'Fischer'} | ||
var garry = {firstName: 'Garry', lastName: 'Kasparov'} | ||
const bobby = {firstName: 'Bobby', lastName: 'Fischer'} | ||
const garry = {firstName: 'Garry', lastName: 'Kasparov'} | ||
@@ -133,3 +139,3 @@ '{0.firstName} {0.lastName} vs. {1.firstName} {1.lastName}'.format(bobby, garry) | ||
```javascript | ||
var repo = {owner: 'davidchambers', slug: 'string-format'} | ||
const repo = {owner: 'davidchambers', slug: 'string-format'} | ||
@@ -144,8 +150,8 @@ 'https://github.com/{owner}/{slug}'.format(repo) | ||
```javascript | ||
var sheldon = { | ||
const sheldon = { | ||
firstName: 'Sheldon', | ||
lastName: 'Cooper', | ||
dob: new Date('1970-01-01'), | ||
fullName: function() { return '{firstName} {lastName}'.format(this) }, | ||
quip: function() { return 'Bazinga!' } | ||
fullName: function() { return this.firstName + ' ' + this.lastName }, | ||
quip: function() { return 'Bazinga!' }, | ||
} | ||
@@ -160,6 +166,27 @@ | ||
### `format.extend(prototype[, transformers])` | ||
### `format.create(transformers)` | ||
This function defines a `format` method on the provided prototype (presumably | ||
`String.prototype`). One may provide an object mapping names to transformers. | ||
This function takes an object mapping names to transformers and returns a | ||
formatting function. A transformer is applied if its name appears, prefixed | ||
with `!`, after a field name in a template string. | ||
```javascript | ||
const fmt = format.create({ | ||
escape: s => s.replace(/[&<>"'`]/g, c => '&#' + c.charCodeAt(0) + ';'), | ||
upper: s => s.toUpperCase(), | ||
}) | ||
fmt('Hello, {!upper}!', 'Alice') | ||
// => 'Hello, ALICE!' | ||
const restaurant = {name: 'Anchor & Hope', url: 'http://anchorandhopesf.com/'} | ||
fmt('<a href="{url!escape}">{name!escape}</a>', restaurant) | ||
// => '<a href="http://anchorandhopesf.com/">Anchor & Hope</a>' | ||
``` | ||
### `format.extend(prototype, transformers)` | ||
This function takes a prototype (presumably `String.prototype`) and an object | ||
mapping names to transformers, and defines a `format` method on the prototype. | ||
A transformer is applied if its name appears, prefixed with `!`, after a field | ||
@@ -170,8 +197,4 @@ name in a template string. | ||
format.extend(String.prototype, { | ||
escape: function(s) { | ||
return s.replace(/[&<>"'`]/g, function(c) { | ||
return '&#' + c.charCodeAt(0) + ';' | ||
}) | ||
}, | ||
upper: function(s) { return s.toUpperCase() } | ||
escape: s => s.replace(/[&<>"'`]/g, c => '&#' + c.charCodeAt(0) + ';'), | ||
upper: s => s.toUpperCase(), | ||
}) | ||
@@ -182,6 +205,3 @@ | ||
var restaurant = { | ||
name: 'Anchor & Hope', | ||
url: 'http://anchorandhopesf.com/' | ||
} | ||
const restaurant = {name: 'Anchor & Hope', url: 'http://anchorandhopesf.com/'} | ||
@@ -201,2 +221,1 @@ '<a href="{url!escape}">{name!escape}</a>'.format(restaurant) | ||
[1]: http://docs.python.org/library/stdtypes.html#str.format | ||
[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9549
6.48%1
-75%4
33.33%0
-100%212
9.84%1
Infinity%86
-3.37%1
Infinity%