
Product
Rust Support in Socket Is Now Generally Available
Socket’s Rust and Cargo support is now generally available, providing dependency analysis and supply chain visibility for Rust projects.
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. If the index is omitted, then the next item is used:
string.format('{0i} {i} {4i} {i} {}', 0, 1, 2, 3, 4, 5, 6);
// Produces 0 1 4 5 6
string.format('{2} {} {} {}', 0, 1, 2, 3, 4, 5, 6);
// Produces 2 3 4 5
string.format('{i} {i} {i}', 0, 1, 2, 3, 4, 5, 6);
// Produces 0 1 2
string.format('{} {} {}', 0, 1, 2, 3, 4, 5, 6);
// Produces 0 1 2
The other fields depend on the type of value being formatted - see the formatters in the next section.
A character after the index indicates the formatter to use:
If not specified, format.string will attempt to guess which formatter to use.
Guessing wrongly may produce incorrect results, so it is advised to always
specify 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]
Running either the following
npm test
node test
will run a suite of tests and report the result to STDERR. When debugging, to run specific test(s) only, run either of:
npm test [index] [index]...
node test [index] [index] ...
For example, run:
node test 23
to execute only test #23.
The test suite can output demos in Markdown format. To generate this, run:
node test md > TESTS.md
The usual test output will be logged to STDERR, but the test cases will be printed to STDOUT, complete with source code & expected result & actual result.
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
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.

Research
Five coordinated Chrome extensions enable session hijacking and block security controls across enterprise HR and ERP platforms.