Socket
Socket
Sign inDemoInstall

format

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    format

printf, sprintf, and vsprintf for JavaScript


Version published
Weekly downloads
3M
decreased by-1.09%
Maintainers
1
Install size
8.59 kB
Created
Weekly downloads
 

Package description

What is format?

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.

What are format's main functionalities?

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})

Other packages similar to format

Readme

Source

format

printf, sprintf, and vsprintf for JavaScript

Installation

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.

Usage

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.

License

Copyright 2010 - 2014 Sami Samhuri sami@samhuri.net

MIT license

FAQs

Last updated on 27 Jan 2016

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc