
Product
Go Support Is Now Generally Available
Socket's Go support is now generally available, bringing automatic scanning and deep code analysis to all users with Go projects.
The 'format' npm package is designed to provide string formatting capabilities similar to the sprintf function in C or Python's str.format(). It allows users to embed variables inside a string template with a specific format, making it easier to generate formatted messages or strings dynamically.
String substitution
This feature allows for simple substitution of strings into a template. The '%s' specifier is replaced by the 'World' string, resulting in 'Hello, World'.
"Hello, %s".format('World')
Number formatting
Enables formatting of numbers with fixed decimal places. Here, '%.2f' formats the number to two decimal places, resulting in 'Your balance is $123.46'.
"Your balance is $%.2f".format(123.456)
JSON object substitution
Allows embedding JSON objects directly into the string. '%j' is replaced with the JSON string representation of the object, producing a string like '{"name":"John","age":30}'.
"%j".format({name: 'John', age: 30})
Provides similar functionality to 'format' with additional support for positional arguments and a wider range of format specifiers. It's more closely aligned with the sprintf function in C.
Offers a simpler approach to string formatting, using named placeholders instead of format specifiers. It's more intuitive for basic use cases but lacks the advanced formatting options available in 'format'.
printf, sprintf, and vsprintf for JavaScript
npm install format
The code works in browsers as well, you can copy these functions into your project or otherwise include them with your other JavaScript.
var format = require('format')
, printf = format.printf
, vsprintf = format.vsprintf
// or if you want to keep it old school
, sprintf = format
// Print 'hello world'
printf('%s world', 'hello')
var what = 'life, the universe, and everything'
format('%d is the answer to %s', 42, what)
// => '42 is the answer to life, the universe, and everything'
vsprintf('%d is the answer to %s', [42, what])
// => '42 is the answer to life, the universe, and everything'
Supported format specifiers: b, c, d, f, o, s, x, and X.
See man 3 printf
or man 1 printf
for details.
Precision is supported for floating point numbers.
Copyright 2010 - 2014 Sami Samhuri sami@samhuri.net
FAQs
printf, sprintf, and vsprintf for JavaScript
The npm package format receives a total of 3,298,340 weekly downloads. As such, format popularity was classified as popular.
We found that 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 Go support is now generally available, bringing automatic scanning and deep code analysis to all users with Go projects.
Security News
vlt adds real-time security selectors powered by Socket, enabling developers to query and analyze package risks directly in their dependency graph.
Security News
CISA extended MITRE’s CVE contract by 11 months, avoiding a shutdown but leaving long-term governance and coordination issues unresolved.