node.js string-format文字列フォーマット
14672 ワード
String::formas is a small JavaScript library formating stings,based on Python's str.format()For example:
Node
Install:
Define window.format:
String::format can be used in two modes:function mode and method mode.
Functionモード
Method mode
format(template、$0、$1、...、N)
Returns the replecing each{…}placholder in the template string with its coress ponding replaccement.
Place Holders may contain numbers which refer to positional argments:
This function defines a format method on the provided prototype.One may providen.prototype.One may provide an oject mapping names.A tranformes.A tranformed is appied name pears,prefixed with!after a field name in a template string.
'"{firstName} {lastName}" '.format(user)
// => '"Jane Smith" '
The equivalent concatent:'"' + user.firstName + ' ' + user.lastName + '" + user.email + '>'
// => '"Jane Smith" '
InstallationNode
Install:
$ npm install string-format
Require:var format = require('string-format')
BrowserDefine window.format:
<script src="path/to/string-format.js">script>
ModesString::format can be used in two modes:function mode and method mode.
Functionモード
format('Hello, {}!', 'Alice')
// => 'Hello, Alice!'
In this mode the first argment is a template string and the remaning argments are values to be interpolated.Method mode
'Hello, {}!'.format('Alice')
// => 'Hello, Alice!'
In this mode values to be interpolated are supplied to the format Method of a template sting.This mode is not enabed by default.The method must first be defined via format.exted:format.extend(String.prototype)
format(template、$0、$1、...、$N)and template.format($0、$1、...N)can then be uster change bly.format(template、$0、$1、...、N)
Returns the replecing each{…}placholder in the template string with its coress ponding replaccement.
Place Holders may contain numbers which refer to positional argments:
'{0}, you have {1} unread message{2}'.format('Holly', 2, 's')
// => 'Holly, you have 2 unread messages'
Ultched plceholders produceのoutput:'{0}, you have {1} unread message{2}'.format('Steve', 1)
// => 'Steve, you have 1 unread message'
A format string may reference a positional argment multiple times:"The name's {1}. {0} {1}.".format('James', 'Bond')
// => "The name's Bond. James Bond."
Positional argments may be referenced implicity:'{}, you have {} unread message{}'.format('Steve', 1)
// => 'Steve, you have 1 unread message'
A format string must not contain both implicit and explicit references:'My name is {} {}. Do you like the name {0}?'.format('Lemony', 'Snicket')
// => ValueError: cannot switch from implicit to explicit numbering
{and}in format stings produce{and}:'{{}} creates an empty {} in {}'.format('dictionary', 'Python')
// => '{} creates an empty dictionary in Python'
Dot notation may be used to reference oject properties:var bobby = {firstName: 'Bobby', lastName: 'Fischer'}
var garry = {firstName: 'Garry', lastName: 'Kasparov'}
'{0.firstName} {0.lastName} vs. {1.firstName} {1.lastName}'.format(bobby, garry)
// => 'Bobby Fischer vs. Garry Kasparov'
may be omitted when referencing a property of{0}:var repo = {owner: 'davidchambers', slug: 'string-format'}
'https://github.com/{owner}/{slug}'.format(repo)
// => 'https://github.com/davidchambers/string-format'
If the referenced property is a method、it is invoked withのargments to determine the replaccement:var sheldon = {
firstName: 'Sheldon',
lastName: 'Cooper',
dob: new Date('1970-01-01'),
fullName: function() { return '{firstName} {lastName}'.format(this) },
quip: function() { return 'Bazinga!' }
}
'{fullName} was born at precisely {dob.toISOString}'.format(sheldon)
// => 'Sheldon Cooper was born at precisely 1970-01-01T00:00:00.000Z'
"I've always wanted to go to a goth club. {quip.toUpperCase}".format(sheldon)
// => "I've always wanted to go to a goth club. BAZINGA!"
format.exted(prototype[、tranformers])This function defines a format method on the provided prototype.One may providen.prototype.One may provide an oject mapping names.A tranformes.A tranformed is appied name pears,prefixed with!after a field 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() }
})
'Hello, {!upper}!'.format('Alice')
// => 'Hello, ALICE!'
var restaurant = {
name: 'Anchor & Hope',
url: 'http://anchorandhopesf.com/'
}
'{name!escape}'.format(restaurant)
// => 'アンチョー&ホープ'
Running the test suite$ npm install
$ npm test