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-kit

v2.0.2

Published

sprintf parser and basic formatter

Downloads

4,721,601

Readme

Build status Tests coverage npm version

sprintf-kit

sprintf parser and basic formatter

Installation

npm install sprintf-kit

Utilities

Parser

Parses format string into data map with respect to printf syntax

const parse = require("sprintf-kit/parse");

const data = parse("Some %s with %d count");

// `data` resolves with following content:
{
  literals: ["Some ", " with ", " count"],
  placeholders: [
    { type: "s", content: "%s" },
    { type: "d", content: "%d" }
  ],
  isParameterIndexingValid: true
}

data spec:

  • literals - Surrounding string literals
  • placeholders - Meta data of parsed placholders. Placeholder properties map (refer to spec for explanation of each property)
    • parameter - (optional) parameter setting (e.g. 1)
    • flags - (optional) array of flags (e.g. ["0", "-"])
    • width - (optional) width (e.g. 4 or "*" if dynamic)
    • precision - (optional) precision (e.g. 4 or "*" if dynamic)
    • length - (optional) length (e.g. "z")
    • type - Modifier type (e.g. "s" or "d")
    • content - Full string representation of placeholder (e.g. "%s")
  • isParameterIndexingValid - Whether parameter indexing is valid across all placeholders. e.g. if no placeholders come with parameters it'll be true. If some but not all of them will come with parameters, it'll be false (if used, then all placeholders should use them).

Format function generator

// Configure format function that resolves 's' and 'd' modifiers
let format = require("sprintf-kit")({
  d: require("sprintf-kit/modifiers/d"),
  s: require("sprintf-kit/modifiers/s")
});

format("Some %s with %d count %x boo", "foo", 12, "ignored"); // Some foo with 12 count %x boo

// Special `rest` formater can be used to handle leftover arguments

format = require("sprintf-kit")({
  d: require("sprintf-kit/modifiers/d"),
  s: require("sprintf-kit/modifiers/s"),
  rest: args => " " + args.join(" ")
});

format("Some %s with %d count", "foo", 12, "rest", "args"); // Some foo with 12 count rest args

// Message string literals (all but placeholders text) can be additionally decorated
// Useful when we want to apply some specific color to message without affecting format of special arguments

const clc = require("cli-color");

format = require("sprintf-kit")({
  d: require("sprintf-kit/modifiers/d"),
  s: require("sprintf-kit/modifiers/s"),
  literal: literal => clc.red(literal)
});

Parts resolver generator

Resolver returns resolved data in form of object parts, which maybe helpful if additional programmatical processing is needed

// Configure format function that resolves 's' and 'd' modifiers
let resolve = require("sprintf-kit/get-resolver")({
  d: require("sprintf-kit/modifiers/d"),
  s: require("sprintf-kit/modifiers/s")
});

resolve("Some %s with %d count %x boo", "foo", 12, "ignored");
// {
//   literals: ["Some ", " with ", " count ", " boo"],
//   substitutions: [
//     { value: "foo", placeholder: { type: "s", content: "%s" } },
//     { value: "12", placeholder:  { type: "d", content: "%d" } },
//     { value: "%x", placeholder: { type: "x", content: "%x" }
//   ],
//   rest: null
// }

resolve = require("sprintf-kit/get-resolver")({
  d: require("sprintf-kit/modifiers/d"),
  s: require("sprintf-kit/modifiers/s"),
  rest: args => " " + args.join(" ")
});

resolve("Some %s with %d count", "foo", 12, "rest", "args");
// {
//   literals: ["Some ", " with ", " count"],
//   substitutions: [
//     { value: "foo", placeholder: { type: "s", content: "%s" } },
//     { value: "12", placeholder:  { type: "d", content: "%d" } }
//   ],
//   rest: " rest args"
// }

Preconfigured modifiers

Currently just basic modifiers are configured in (PR's welcome to extend this support).

Modifiers can be found at sprintf-kit/modifiers folder.

Preconfigured modifiers

  • d - Number
  • f - Floating point value
  • i - Integer
  • j - JSON
  • s - String

Every modifier is exception safe, in case of approaching invalid value, adequate error message token is displayed in place of placeholder

Tests

npm test

Project cross-browser compatibility supported by:

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.