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

fast-printf

v1.6.10

Published

Fast and spec-compliant printf implementation for Node.js and browser.

Downloads

3,862,141

Readme

fast-printf

Coveralls NPM version Canonical Code Style Twitter Follow

Fast and spec-compliant printf implementation for Node.js and browser.

Usage

import {
  printf,
} from 'fast-printf';

console.log(printf('foo %s', 'bar'));

Handling Unbound Value References

By default, interpolating an unbound expression produces:

  • The expression is left in place
  • A warning is logged using roarr

i.e. printf('%s bar') produces %s bar.

This behavior can be overridden by configuring a fast-printf instance using createPrintf:

import {
  createPrintf,
} from 'fast-printf';

const printf = createPrintf({
  formatUnboundExpression: (
    subject: string,
    token: PlaceholderToken,
    boundValues: any[],
  ): string => {
    console.warn({
      boundValues,
      position: token.position,
      subject,
    }, 'referenced unbound value');

    return token.placeholder;
  };
});

console.log(printf('foo %s', 'bar'));

Benchmark

|implementation|without_placeholders|with_string_placeholder|with_many_string_placeholders| |-|-|-|-| |sprintf|31,772,029|4,154,748|637,229| |printf|651,970|373,615|160,795| |fast-printf|78,068,540|11,820,632|2,552,386|

Results show operations per second (greater is better).

To run the benchmark yourself please see ./benchmark.

Printf Cheatsheet

// %c character
printf('%c', 'b');
// => 'c'

// %C converts to uppercase character (if not already)
printf('%C', 'b');
// => 'B'

// %d decimal integer (base 10)
printf('%d', 100);
// => '100'

// %0Xd zero-fill for X digits
printf('%05d', 1);
// => '00001'

// %Xd right justify for X digits
printf('%5d', 1);
// => '    1'

// %-Xd left justify for X digits
printf('%-5d', 1);
// => '1    '

// %+d adds plus sign(+) to positive integers, minus sign for negative integers(-)
printf('%+5d', 1);
// => '    +1'
printf('%+5d', -1);
// => '    -1'

// %e scientific notation
printf('%e', 52.8);
// => '5.28e+1'

// %E scientific notation with a capital 'E'
printf('%E', 52.8);
// => '5.28E+1'

// %f floating-point number
printf('%f', 52.8);
// => '52.8'

// %.Yf prints Y positions after decimal
printf('%.1f', 1.234);
// => '1.2'

// %Xf takes up X spaces
printf('%5f', 123);
// => '  123'

// %0X.Yf zero-fills
printf('%05.1f', 1.234);
// => '001.2'

// %-X.Yf left justifies
printf('%-5.1f', 1.234);
// => '1.2  '

// %i integer (base 10)
printf('%i', 123);
// => '123'

// %b converts to boolean
printf('%b', true);
// => 'true'
printf('%b', 'true');
// => 'true'
printf('%b', 1);
// => 'true'

// %B converts to uppercase boolean
printf('%b', true);
// => 'TRUE'
printf('%b', 'true');
// => 'TRUE'
printf('%b', 1);
// => 'TRUE'

// %o octal number (base 8)
printf('%o', 8);
// => '10'

// %s a string of characters
printf('%s', 'foo');
// => 'foo'

// %Xs formats string for a minimum of X spaces
printf('%5s', 'foo');
// => '  foo'

// %-Xs left justify
printf('%-5s', 'foo');
// => 'foo  '

// %S converts to a string of uppercase characters (if not already)
printf('%S', 'foo');
// => 'FOO'

// %u unsigned decimal integer
printf('%u', 1);
// => '1'
printf('%u', -1);
// => '4294967295'

// %x number in hexadecimal (base 16)
printf('%x', 255);
// => 'ff'

// %% prints a percent sign
printf('%%');
// => '%'

// \% prints a percent sign
printf('\\%');
// => '%'

// %2$s %1$s positional arguments
printf('%2$s %1$s', 'bar', 'foo');
// => 'foo bar'