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

sfmt

v1.0.3

Published

beefed up version of util.format

Downloads

5

Readme

sfmt - an improved util.format

sfmt is a module that provides an updated and improved version of util.format for string formatting.

Installation

npm install sfmt

Usage

sfmt takes a format string, plus values to substitute, just like util.format does. There are three different substitutions you can include in a format string.

Positional

This is just like util.format/printf/etc. Use %x (where x is a format specifier, see below) and the next parameter in the list will be substituted.

Examples:

var assert = require('assert');
var sfmt = require('sfmt');

assert.equal(sfmt('This %s has %d feet', 'duck', 2),
    'This duck has 2 feet');

Indexed

This substitution lets you specify the index of the parameter to substitute. This is particularly useful when you need to use the same substitution in multiple places in the string, or when you have localizable text where the order of words change depending on the language.

This substitution looks like %{d:x} where d is the position in the parameter list, and x is the format specifier.

Example:

var assert = require('assert');
var sfmt = require('sfmt');

assert.equal(sfmt('This %{1:s} has %{2:d} feet. It\'s a %{1:s}', 4, 'dog'),
    'This dog has 4 feet. It\'s a dog');

Named

Sometimes you need to pull fields directly out of an object. Named substitutions substitute the value of an object field by name. The substitution looks like %{name:x} where name is the name of the field, and x is the format specifier. The fields are pulled from the first parameter to sfmt after the format string.

Example:

var assert = require('assert');
var sfmt = require('sfmt');

var critter = {
    species: 'snake',
    feet: 0
};

assert.equal(sfmt('This %{species:s} has %{feet:d} feet.', critter), 'This snake has 0 feet.');

Escaping the % character

If you need a literal % character you can include it in the format string by using %%.

Example:

var assert = require('assert');
var sfmt = require('sfmt');

assert.equal(sfmt('Your score on the %s test was %d%%', 'math', 85),
    'Your score on the math test was 85%');

Format specifiers

The following format specifiers are supported:

  • s - converts value to a string
  • d - converts value to a number
  • j - converts value to json by calling JSON.stringify
  • i - calls util.inspect on the value

Combining substitutions

You can use all three substitutions (positional, indexed, and named) in the same format string if you want to. Positional subtitutions are the only one that consume parameters, using indexed or named substitutions have no effect.

Example:


var assert = require('assert');
var sfmt = require('sfmt');

assert.equal(sfmt('The quick %s %{1:s} jumped over the %{0:s} %s', 'brown', 'dog'),
    'The quick brown dog jumped over the brown dog');

Not quite sure why you'd want to do this, but it does work. :-)