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

@stdlib/string-format

v0.2.2

Published

Insert supplied variable values into a format string.

Downloads

1,678,849

Readme

format

NPM version Build Status Coverage Status

Insert supplied variable values into a format string.

Installation

npm install @stdlib/string-format

Usage

var format = require( '@stdlib/string-format' );

format( str, ...args )

Inserts supplied variable values into a format string.

var str = 'Hello, %s! My name is %s.';
var out = format( str, 'world', 'Bob' );
// returns 'Hello, world! My name is Bob.'

The format string is a string literal containing zero or more conversion specifications, each of which results in a string value being inserted to the output string. A conversion specification consists of a percent sign (%) followed by one or more of the following flags, width, precision, and conversion type characters. It thus takes the following form:

%[flags][width][.precision]specifier

Arguments following the format string are used to replace the placeholders in the format string. The number of arguments following the format string should be equal to the number of placeholders in the format string.

var str = '%s %s';
var out = format( str, 'Hello', 'World' );
// returns 'Hello World'

To supply arguments in a different order than they appear in the format string, positional placeholders as indicated by a $ character in the format string are used. In this case, the conversion specification takes the form:

%[pos$][flags][width][.precision]specifier
var str = '%3$s %2$s %1$s';
var out = format( str, 'foo', 'bar', 'baz' );
// returns 'baz bar foo'

The following table summarizes the supported specifiers:

| type | description | example | | ---- | ---------------------------------- | ------------ | | s | string | beep boop | | c | character | a | | d, i | signed decimal integer | -12 | | u | unsigned decimal integer | 390 | | b | unsigned binary integer | 11011011 | | o | unsigned octal integer | 510 | | x | unsigned hexadecimal (lowercase) | 7b | | X | unsigned hexadecimal (uppercase) | 7B | | f, F | decimal floating point | 390.24 | | e | scientific notation (lowercase) | 3.9e+1 | | E | scientific notation (uppercase) | 3.9E+1 | | g | shortest representation (e/f) | 3.9 | | G | shortest representation (E/F) | 3.9 |

var str = '%i written as a binary number is %b.';
var out = format( str, 9, 9 );
// returns '9 written as a binary number is 1001.'

str = '%i written as an octal number is %o.';
out = format( str, 17, 17 );
// returns '17 written as an octal number is 21.'

str = '%i written as a hexadecimal number is %x.';
out = format( str, 255, 255 );
// returns '255 written as a hexadecimal number is ff.'

str = '%i written as a hexadecimal number is %X (uppercase letters).';
out = format( str, 255, 255 );
// returns '255 written as a hexadecimal number is FF (uppercase letters).'

str = '%i written as a floating point number with default precision is %f!';
out = format( str, 8, 8 );
// returns '8 written as a floating point number with default precision is 8.000000!'

str = 'Scientific notation: %e';
out = format( str, 3.14159 );
// returns 'Scientific notation: 3.141590e+00'

str = 'Scientific notation: %E (uppercase).';
out = format( str, 3.14159 );
// returns 'Scientific notation: 3.141590E+00 (uppercase).'

str = '%g (shortest representation)';
out = format( str, 3.14159 );
// returns '3.14159'

A conversion specification may contain zero or more flags, which modify the behavior of the conversion. The following flags are supported:

| flag | description | | ----- | ------------------------------------------------------------------------------------------ | | - | left-justify the output within the given field width by padding with spaces on the right | | 0 | left-pad the output with zeros instead of spaces when padding is required | | # | use an alternative format for o and x conversions | | + | prefix the output with a plus (+) or minus (-) sign even if the value is a positive number | | space | prefix the value with a space character if no sign is written |

var str = 'Always prefix with a sign: %+i';
var out = format( str, 9 );
// returns 'Always prefix with a sign: +9'

out = format( str, -9 );
// returns 'Always prefix with a sign: -9'

str = 'Only prefix with a sign if negative: %i';
out = format( str, 6 );
// returns 'Only prefix with a sign if negative: 6'

out = format( str, -6 );
// returns 'Only prefix with a sign if negative: -6'

str = 'Prefix with a sign if negative and a space if positive: % i';
out = format( str, 3 );
// returns 'Prefix with a sign if negative and a space if positive:  3'

out = format( str, -3 );
// returns 'Prefix with a sign if negative and a space if positive: -3'

The width may be specified as a decimal integer representing the minimum number of characters to be written to the output. If the value to be written is shorter than this number, the result is padded with spaces on the left. The value is not truncated even if the result is larger. Alternatively, the width may be specified as an asterisk character (*), in which case the argument preceding the conversion specification is used as the minimum field width.

var str = '%5s';
var out = format( str, 'baz' );
// returns '  baz'

str = '%-5s';
out = format( str, 'baz' );
// returns 'baz  '

str = '%05i';
out = format( str, 2 );
// returns '00002'

str = '%*i';
out = format( str, 5, 2 );
// returns '   2'

The precision may be specified as a decimal integer or as an asterisk character (*), in which case the argument preceding the conversion specification is used as the precision value. The behavior of the precision differs depending on the conversion type:

  • For s specifiers, the precision specifies the maximum number of characters to be written to the output.
  • For floating point specifiers (f, F, e, E), the precision specifies the number of digits after the decimal point to be written to the output (by default, this is 6).
  • For g and G specifiers, the precision specifies the maximum number of significant digits to be written to the output.
  • For integer specifiers (d, i, u, b, o, x, X), the precision specifies the minimum number of digits to be written to the output. If the value to be written is shorter than this number, the result is padded with zeros on the left. The value is not truncated even if the result is longer. For

Alternatively, the precision may be specified as an asterisk character (*), in which case the argument preceding the conversion specification is used as the minimum number of digits.

var str = '%5.2s';
var out = format( str, 'baz' );
// returns '   ba'

str = 'PI: ~%.2f';
out = format( str, 3.14159 );
// returns 'PI: ~3.14'

str = 'Agent %.3i';
out = format( str, 7 );
// returns 'Agent 007'

Examples

var format = require( '@stdlib/string-format' );

var out = format( '%s %s!', 'Hello', 'World' );
// returns 'Hello World!'

out = format( 'Pi: ~%.2f', 3.141592653589793 );
// returns 'Pi: ~3.14'

out = format( '%-10s %-10s', 'a', 'b' );
// returns 'a       b       '

out = format( '%10s %10s', 'a', 'b' );
// returns '       a       b'

out = format( '%2$s %1$s %3$s', 'b', 'a', 'c' );
// returns 'a b c'

Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.