JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 267604
  • Score
    100M100P100Q203917F

Full implementation of the `printf` family in pure JS.

Package Exports

  • printf

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (printf) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

            _       _    __ 
           (_)     | |  / _|
 _ __  _ __ _ _ __ | |_| |_ 
| '_ \| '__| | '_ \| __|  _|
| |_) | |  | | | | | |_| |  
| .__/|_|  |_|_| |_|\__|_|  
| |                         
|_| 

A complete implementation of the printf C functions family for Node.JS, written in pure JavaScript.
The code is strongly inspired by the one availabe in the Dojo Toolkit.

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

var printf = require('printf');

Flags

  (space)
assert.eql('  -42', printf('% 5d', -42));
+ (plus)
assert.eql('  +42', printf('%+5d', 42));
0 (zero)
assert.eql('00042', printf('%05d', 42));
- (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('$', printf('%c", 36));

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!

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

Lenght 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('0000003.14', printf('%0*.*f', 3.14159265, 10, 2));
assert.eql('3.14      ', printf('%-*.*f', 3.14159265, 10, 2));

Test

Using Expresso:

expresso