printf
Full implementation of the `printf` family in pure JS.
Last updated 4 months ago by david .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install printf 
SYNC missed versions from official npm registry.

Build Status

A complete implementation of the printf C functions family for Node.JS, written in pure JavaScript.

Bonus! You get extra features, like the %O converter (which inspects the argument). See Extra Features below.

Installing

Via NPM:

$ npm install printf

Usage

Use it like you would in C (printf/sprintf):

var printf = require('printf');
var result = printf(format, args...);

It can also output the result for you, as fprintf:

var printf = require('printf');
printf(write_stream, format, args...);

Features

Flag (space)

assert.eql('  -42', printf('% 5d', -42));

Flag + (plus)

assert.eql('  +42', printf('%+5d', 42));

Flag 0 (zero)

assert.eql('00042', printf('%05d', 42));

Flag - (minus)

assert.eql('42   ', printf('%-5d', 42));

Width / precision

assert.eql('42.90', printf('%.2f', 42.8952));
assert.eql('042.90', printf('%06.2f', 42.8952));

Numerical bases

assert.eql('\x7f', printf('%c', 0x7f));
assert.eql('a', printf('%c', 'a'));
assert.eql('"', printf('%c', 34));

Miscellaneous

assert.eql('10%', printf('%d%%', 10));
assert.eql('+hello+', printf('+%s+', 'hello'));
assert.eql("a", printf("%c", "a"));
assert.eql('"', printf("%c", 34));
assert.eql('$', printf('%c', 36));
assert.eql("10", printf("%d", 10));

Extra features!

Inspector

The %O converter will call util.inspect(...) at the argument:

assert.eql("Debug: { hello: 'Node', repeat: false }",
  printf('Debug: %O', {hello: 'Node', "repeat": false})
);
assert.eql("Test: { hello: 'Node' }",
  printf('%2$s: %1$O', {"hello": 'Node'}, 'Test')
);

Important: it's a capital "O", not a zero!

Specifying a precision lets you control the depth up to which the object is formatted:

assert.eql("Debug: { depth0: { depth1_: 0, depth1: [Object] } }",
  printf('Debug: %.1O', {depth0: {depth1: {depth2: {depth3: true}}, depth1_: 0}})
);

You can use the alternative form flag together with %O to disable representation of non-enumerable properties (useful for arrays):

assert.eql("With non-enumerable properties: [ 1, 2, 3, 4, 5, [length]: 5 ]",
  printf('With non-enumerable properties: %O', [1, 2, 3, 4, 5])
);
assert.eql("Without non-enumerable properties: [ 1, 2, 3, 4, 5 ]",
  printf('Without non-enumerable properties: %#O', [1, 2, 3, 4, 5])
);

You can use the sign flag together with %O to enable colors in util.inspect:

assert.eql("With colors: { bar: \u001b[33mtrue\u001b[39m, baz: \u001b[33mfalse\u001b[39m }",
  printf('With colors: %+O', {bar: true, baz: false})
);

Argument mapping

In addition to the old-fashioned n$, you can use hashes and property names!

assert.eql('Hot Pockets',
  printf('%(temperature)s %(crevace)ss', {
    temperature: 'Hot',
    crevace: 'Pocket'
  })
);
assert.eql('Hot Pockets',
  printf('%2$s %1$ss', 'Pocket', 'Hot')
);

Positionals

Length and precision can now be variable:

assert.eql(' foo', printf('%*s', 'foo', 4));
assert.eql('      3.14', printf('%*.*f', 3.14159265, 10, 2));
assert.eql('000003.142', printf('%0*.*f', 3.14159265, 10, 3));
assert.eql('3.1416    ', printf('%-*.*f', 3.14159265, 10, 4));

Development

Tests are written in CoffeeScript and are executed with Mocha. To use it, simple run npm install, it will install Mocha and its dependencies in your project's node_modules directory followed by npm test.

To run the tests:

npm install
npm test

The test suite is run online with Travis against the versions 6, 7, 8 and 9 of Node.js.

Contributors

This package is developed by Adaltas.

Current Tags

  • 0.5.3                                ...           latest (4 months ago)

21 Versions

  • 0.5.3                                ...           4 months ago
  • 0.5.2                                ...           a year ago
  • 0.5.1                                ...           2 years ago
  • 0.4.0                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.2.5                                ...           4 years ago
  • 0.2.4                                ...           4 years ago
  • 0.2.3                                ...           5 years ago
  • 0.2.2                                ...           5 years ago
  • 0.2.1                                ...           5 years ago
  • 0.2.0                                ...           6 years ago
  • 0.1.3                                ...           6 years ago
  • 0.1.2                                ...           7 years ago
  • 0.1.1                                ...           7 years ago
  • 0.1.0                                ...           7 years ago
  • 0.0.7                                ...           8 years ago
  • 0.0.6                                ...           8 years ago
  • 0.0.5                                ...           8 years ago
  • 0.0.4                                ...           9 years ago
  • 0.0.3                                ...           9 years ago
  • 0.0.2                                ...           9 years ago
Maintainers (1)
Downloads
Today 36
This Week 720
This Month 1,226
Last Day 142
Last Week 797
Last Month 3,196
Dependencies (0)
None
Dev Dependencies (4)
Dependents (159)

Copyright 2014 - 2016 © taobao.org |