0.1.4 • Published 10 years ago

sprintf.js v0.1.4

Weekly downloads
72
License
-
Repository
github
Last release
10 years ago

A node.js sprintf implementation

Note

This implementation of sprintf.js is no longer from https://github.com/jakobwesthoff/sprintf.js A new parser has been written enabling the format descriptors to be a bit more "C"-like.

Please note sprintf.js adds the functions printf and sprintf to the global scope and onto the string prototype.

Installation

$ npm install sprintf.js

Capabilities

This library provides an almost complete implementation of the sprintf and printf functions from the standard C library. All options from the C format strings are valid, however where there is no corresponding functionality in JavaScript, nothing happens - it's not an error, there is no functionality in cases where it would make no sense.

The format string has the form:

% [flags] [field_width] [.precision] [length_modifier] conversion_character

Components in brackets [] are optional. The minimum is a % and a conversion character (e.g. %d).

Flags

Flags can be in any order.

Field width

The converted argument will be printed in a field at least this wide, and wider if necessary. If the converted argument has fewer characters than the field width, it will be padded on the left (or right, if left adjustment has been requested) to make up the field width. The padding character is normally ' '(space), but is '0' if the zero padding flag (0) is present. If the field width is specified as *, the value is computed from the next argument, which must be an int.

Precision

A dot '.' separates the field width from the precision. If the precision is specified as '*', the value is computed from the next argument, which must be a number. If the number is negative, the sign is dropped and if the number has a fractional component, that is also discarded.

Length modifier

Length has no meaning in JavaScript - all numbers have the same length - 64 bits. It is possible to emulate what C does, but I can't think a good reason to do so. Right now, the length modifier is parsed, but does nothing. Suggestions are welcome, though.

Conversion character

Usage

To use, simply require the module and then use the global function sprintf and the string prototype method printf:

require('sprintf');

var text = sprintf('Hello %%s, a formatted number is: %3.2f\n', 22/7);
process.stdout.write(text);
printf(text, 'Jakob');   // The format string is the text in the string object.

// we can also do Javascript objects
var obj = { a: 1, b: 'A string', c: 444.1 };

var text = sprintf('This is an object: %j\n', obj);
printf(text);

// if the first argument after the format string is an array, and there are no more
// arguments, it's assumed that array holds the values for the format string.
var values = [ 99, 'luft', 'ballons' ];
var text = sprintf('I have %d %s%s.', values);

After loading the sprintf.js Javascript file, the global functions printf and sprintf are registered and ready for use. Further, the String object is extended with printf mnd sprintf methods.

You may either use the global function sprintf which returns the newly formatted string if supplied with the format string, as well as all needed arguments:

var formatted = sprintf('The number is %.2f', number);

You may use the string prototype's sprintf method directly on the format string::

var formatted = 'The number is %.2f'.sprintf(number);

You can use the string prototype's printf to display the formatted output to standard out:

'I like %s, a lot'.printf('ducks');
var text = 'There are %d geese';
text.printf(22);

Internally the exact the same processing takes place. You may decide which syntax you like better.

License

This code is under the MIT License. Copyright (c) 2013,2014 Edmond Meinfelder

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0.1.4

10 years ago

0.1.3

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago

0.0.8

11 years ago

0.0.7

11 years ago

0.0.6

11 years ago

0.0.5

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago