
Product
Introducing Custom Tabs for Org Alerts
Create and share saved alert views with custom tabs on the org alerts page, making it easier for teams to return to consistent, named filter sets.
var format = require('format');
format.string('First: {0}, Second: {1}, Third: {2}', 'test', true, 42);
// First: test, Second: true, Third: 42
format.string('Mass: {0f:3} kg, Age: {0i} years, Hex ID: 0x{0i:32x}',
74.21, 26, 64206);
// Mass: 74.2 kg, Age: 26 years, Hex ID: 0xface
/*
* Optional monkey-patch:
* Adds method to String.prototype.format which proxies to format.string
*/
format.moreConflict();
/* With moreConflict called, we can use this shorthand: */
'First: {0}, Second: {1}, Third: {2}'.format('test', true, 42);
// First: test, Second: true, Third: 42
The formatStr string is a string which contains zero or more format specifiers.
A format specifier is contained in braces.
{spec}
Open-brace in the format string should be escaped if it is not the start of a format specifier:
not a format specifier => \{0}
is a format specifier => {0}
Remember that backslashes must also be escaped in JavaScript strings!
var format = "not a format specifier => \\{0}";
Close-brace within a format specifier must be escaped by preceeding with backslash.
{ \} }
var fmt = "true/false => \\{/} result={0b:{:\\}}";
/* true/false => {/} result={ */
format.string(fmt, true);
/* true/false => {/} result=} */
format.string(fmt, false);
The specifier contains one or more fields, separated by colons:
{f1:f2:f3}
Colons within a field must be escaped by preceeding with a backslash.
{ escape close brace \} : escape colon \: }
The first field is a numerical zero-based index, specifying the index of the value to use. The first value in the value array is "{0}", the second value in the array is "{1}" and so forth.
The other fields depend on the type of value being formatted - see the formatters in the next section.
If other fields are specified, then a character after the index indicates the formatter to use:
If not specified, format.string will attempt to guess which formatter to use.
Examples:
format.string('age: {0i} years, mass: {1f:.2} kg, active: {2b:yes:no}',
26, 74.2, true);
Produces:
age: 26 years, mass: 74.20 kg, active: yes
delim : quote/escape : prefix : suffix
,:qe:[:]
force-sign width base
[+][width][base]
force-sign significant-figures
force-sign width.places
[+][digits]
true-string : other-string : false-string
[true:[other:]false]
FAQs
Value formatter and string interpolator
The npm package bs-format receives a total of 0 weekly downloads. As such, bs-format popularity was classified as not popular.
We found that bs-format demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Create and share saved alert views with custom tabs on the org alerts page, making it easier for teams to return to consistent, named filter sets.

Product
Socket’s Rust and Cargo support is now generally available, providing dependency analysis and supply chain visibility for Rust projects.

Security News
Chrome 144 introduces the Temporal API, a modern approach to date and time handling designed to fix long-standing issues with JavaScript’s Date object.