String format function for javascript.
JavaScript HTML
Latest commit 1f09b02 Dec 25, 2016 @tmaeda1981jp Bump to v1.0.0
Permalink
Failed to load latest commit information.
test Fix #3 Mar 27, 2016
.gitignore
.travis.yml
Gruntfile.js Update browser test task to use grunt-mocha-phantomjs plugin Sep 12, 2014
LICENSE.txt Add LICENSE.txt Jan 3, 2014
README.md
bower.json Bump to v1.0.0 Dec 25, 2016
format.js Fix #3 Mar 27, 2016
format.min.js
package.json Bump to v1.0.0 Dec 25, 2016
yarn.lock Add coverage settings Dec 25, 2016

README.md

string-format-js

License NPM version Build Status Coverage Status Downloads

Synopsis

String format function for javascript.

Code Example

%d

'%d'.format(10) === '10'
'%d, %d'.format(5, 10) === '5, 10'
'%d, %d and %d'.format(5, 10, 15) === '5, 10 and 15'
'%05d'.format(123) === '00123'
'%03d, %05d'.format(1, 123) === '001, 00123'
'[%5d]'.format(123) === '[ 123]'
'[%10d]'.format(123) === '[      123]'
'[%-5d]'.format(123) === '[123 ]'
'[%-10d]'.format(123) === '[123      ]'

%s

'This is a %s'.format('pen') === 'This is a pen'
'This is %s %s'.format('a', 'pen') === 'This is a pen'
'This %s %s %s'.format('is', 'a', 'pen') === 'This is a pen'
'[%5s]'.format('abc') === '[  abc]'
'[%12s]'.format('abc') === '[         abc]'
'[%-5s]'.format('abc') === '[abc  ]'
'[%-12s]'.format('abc') === '[abc         ]'
'[%.4s]'.format('abcde') === '[abcd]'
'[%.12s]'.format('abcdefghijklmn') === '[abcdefghijkl]'
'[%5.4s]'.format('abcde') === '[ abcd]'
'[%12.10s]'.format('abcdefghijklmnopq') === '[  abcdefghij]'
'[%-5.4s]'.format('abcde') === '[abcd ]'
'[%-12.10s]'.format('abcdefghijklmnopq') === '[  abcdefghij]'
'[%-5.4s]'.format('あいうえお') === '[あいうえ ]'
'%s'.format(true) === 'true'
'%s'.format('true') === 'true'
'%s'.format(false) === 'false'
'%s'.format('false') === 'false'
'%s'.format('$$') === '$$'
'%s'.format('$$$$') === '$$$$'
'%s'.format('$&') === '$&'
'%s'.format('$`') === '$`'
'%s'.format('$'') === '$''
'[%5s]'.format('$$') === '[   $$]'
'[%-5s]'.format('$$') === '[$$   ]'
'[%.5s]'.format('abc$$fg') === '[abc$$]'
'[%.4s]'.format('abc$$fg') === '[abc$]'
'[%5.5s]'.format('abc$$fg') === '[abc$$]'
'[%5.4s]'.format('abc$$fg') === '[ abc$]'
'[%-5.5s]'.format('abc$$fg') === '[abc$$]'
'[%-5.4s]'.format('abc$$fg') === '[abc$ ]'

%o

'123 => %o'.format(123) === '123 => 173'
'0x7b => %o'.format(0x7b) === '0x7b => 173'

%b

'123 => %b'.format(123) === '123 => 1111011'
'0x7b => %b'.format(0x7b) === '0x7b => 1111011'

%x

'123 => %x'.format(123) === '123 => 7b'

%X

'123 => %X'.format(123) === '123 => 7B'

%u

'%u'.format(0x12345678 ^ 0xFFFFFFFF) === '3989547399'
'%u'.format(-1) === '4294967295'

%c

'%c'.format(97) === 'a'
'%c'.format(0x61) === 'a'

%f

'%f'.format(1.0) === '1.000000'
'%.2f'.format(1.0) === '1.00'
'[%10f]'.format(1.0) === '[1.00000000]'
'[%10.2f]'.format(1.0) === '[      1.00]'
'[%10.2f]'.format(1.2345) === '[      1.23]'
'[%-10.2f]'.format(1.0) === '[1.00      ]'

%e

'%e'.format(123) === '1.23e+2'
'%e'.format(123.45) === '1.2345e+2'
'%.5e'.format(123.45) === '1.23450e+2'
'[%15e]'.format(123.45) === '[1.2345000000e+2]'
'[%20e]'.format(12345678901.45) === '[1.23456789014500e+10]'
'[%15.2e]'.format(123.45) === '[        1.23e+2]'
'[%7.2e]'.format(123.45) === '[1.23e+2]'
'[%-15.2e]'.format(123.45) === '[1.23e+2        ]'

hash

'#{name}'.format({name:'Takashi Maeda'}) === 'Takashi Maeda'
'#{first} #{last}'.format({first:'Takashi', last:'Maeda'}) === 'Takashi Maeda'
'#{a} #{b}, #{c} #{d}'.format(a:'Easy', b:'come', c:'easy', d:'go'}) === 'Easy come, easy go'
'#{a} #{b}, #{a} #{c}'.format({a:'Easy', b:'come', c:'go'}) === 'Easy come, Easy go'
'#{a} #{a} #{a}'.format({a:'hello'}) === 'hello hello hello'

Installation

node

$ npm install string-format-js

bower

$ bower install string-format-js

Tests

node

$ grunt mochaTest

browser

$ grunt browserTest

License

This software is released under the MIT License, see LICENSE.txt.