JSPM

sprintf-lite

1.3.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 10
  • Score
    100M100P100Q40320F
  • License MIT

Low-fat implementation of the sprintf() function for JavaScript.

Package Exports

  • sprintf-lite

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sprintf-lite) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

sprintf() lite for JavaScript

Low-fat implementation of the sprintf() function for JavaScript that gets you all you need.

For simple string templates:

{person1} gets easily triggered when {person2} makes fun of CS:GO.

For easy Swagger and other API spec tools interation as path/route parameters:

/path/to/{success}/should/not/{be}/hard
/path/to/:success/should/not/:be/hard

Description

string sprintf (string format [, mixed args [, mixed ... ]])

format A string that contains the text to be returned, it can optionally contain embedded format specifiers and keys that are replaced by the values specified in subsequent additional arguments and formatted as requested.

args A number of mixed type arguments for the replacement. Can be a string, array or an object.

Returns a string produced according to the formatting string merged with replacement values.

Compatible specifiers

  • %d — print an integer as is
  • %u — print an integer as is
  • %f — print a float as is
  • %s — print a string as is

Named keys

  • :key - print a value of an object key as is.
  • {key} - print a value of an object key as is.

Install

npm install sprintf-lite

Use

ES5

var sprintf = require('sprintf-lite').default

ES6

import sprintf from 'sprintf-lite'

Examples IRL

Example #1 Basic anonymous keys replacement

Quick string formatting.

sprintf('%s + %d = %s', 'Two', 3, 'Five')
sprintf('/my/%s/api/%s/number/%d', ['awesome', 'endpoint', 1])

The above example will output:

Two + 3 = Five
/my/awesome/api/endpoint/number/1

Example #2 Basic named keys replacement

Embedding object keys into configuration strings or any string templates.

sprintf('Dear {firstName} {lastName}, ...', {firstName: 'John', lastName: 'Doe'})
sprintf('/users/{userId}/orders/{orderId}', {userId: 654321, orderId: 987654321})
sprintf('/users/:userId/orders/:orderId', {userId: 654321, orderId: 987654321})

The above example will output:

Dear John Doe, ...
/users/654321/orders/987654321
/users/654321/orders/987654321

Example #3 Argument swapping and reuse

Keys can be used multiple times in any arbitrary order.

sprintf('In America, you {do} {what}, in Soviet Russia, {what} {do}s YOU!', {do: 'install', what: 'a package'})

The above example will output:

In America, you install a package, in Soviet Russia, a package installs YOU!