npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sprintf.js

v0.1.4

Published

This module provides an almost complete implementation of the printf and sprintf functions from the standard C library.

Downloads

94

Readme

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.