{"id":14045,"library":"sprintf-kit","title":"sprintf-kit","description":"sprintf-kit is a JavaScript library providing a modular `printf` format string parser and basic formatter. It allows developers to create custom `sprintf`-like functions by explicitly configuring specific modifier resolvers (e.g., for `%s`, `%d`). The library's core is its detailed `printf` syntax parser, which breaks down format strings into literals and placeholder metadata. It also offers a format function generator and a parts resolver generator, enabling granular control over how strings are processed and substitutions are applied. Currently at version 2.0.2, the package has seen infrequent but recent maintenance, with the last major update (v2.0.0) occurring in 2018, focusing on internal structure and advanced parsing capabilities. Its modularity and explicit modifier configuration differentiate it from simpler `sprintf` implementations that might bundle all modifiers by default.","status":"active","version":"2.0.2","language":"javascript","source_language":"en","source_url":"git://github.com/medikoo/sprintf-kit","tags":["javascript","printf","sprintf","log","format","string"],"install":[{"cmd":"npm install sprintf-kit","lang":"bash","label":"npm"},{"cmd":"yarn add sprintf-kit","lang":"bash","label":"yarn"},{"cmd":"pnpm add sprintf-kit","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used in examples for decorating literal strings in the format function; it is an optional utility for styling output.","package":"cli-color","optional":true}],"imports":[{"note":"The default export is a function generator that takes a configuration object for modifiers. The package is CommonJS-first.","wrong":"import format from 'sprintf-kit';","symbol":"default (format function)","correct":"const format = require('sprintf-kit');"},{"note":"Parses the format string into a data map of literals and placeholders. Explicit CommonJS `require` for sub-paths.","wrong":"import { parse } from 'sprintf-kit';","symbol":"parse","correct":"const parse = require('sprintf-kit/parse');"},{"note":"Generates a resolver function that returns resolved parts with metadata. In v2.0.0, it was renamed from `getResolver` to `getPartsResolver` in internal commits, but the public API `get-resolver` path remained stable.","wrong":"import { getResolver } from 'sprintf-kit';","symbol":"getResolver","correct":"const getResolver = require('sprintf-kit/get-resolver');"},{"note":"Individual modifiers are imported from sub-paths to be configured with the main format function. This granular approach allows for tree-shaking and custom modifier sets.","wrong":"import { s } from 'sprintf-kit/modifiers/s';","symbol":"modifiers/s (string modifier)","correct":"const s = require('sprintf-kit/modifiers/s');"}],"quickstart":{"code":"const format = require('sprintf-kit');\nconst s = require('sprintf-kit/modifiers/s');\nconst d = require('sprintf-kit/modifiers/d');\nconst clc = require('cli-color');\n\n// Configure a format function with string and decimal modifiers\nconst customFormat = format({\n  s: s,\n  d: d,\n  // Optional: add a 'rest' formatter for unhandled arguments\n  rest: args => args.length > 0 ? ` (and ${args.join(', ')})` : ''\n});\n\nconsole.log(customFormat('Hello %s, you have %d new messages.', 'Alice', 5));\n// Expected output: \"Hello Alice, you have 5 new messages.\"\n\nconsole.log(customFormat('User %s logged in from %s, but ignored %d.', 'Bob', '192.168.1.1', 10, 'extra arg'));\n// Expected output: \"User Bob logged in from 192.168.1.1, but ignored 10. (and extra arg)\"\n\n// Example with custom literal decoration (e.g., for console coloring)\nconst coloredFormat = format({\n  s: s,\n  d: d,\n  literal: literal => clc.green(literal) // Make non-placeholder text green\n});\n\nconsole.log(coloredFormat('This is a %s message with a %d number.', 'colorful', 123));","lang":"javascript","description":"This quickstart demonstrates how to set up a custom `sprintf` formatter using `sprintf-kit`, including defining specific modifiers and optionally styling literal parts of the string with an external library like `cli-color`."},"warnings":[{"fix":"Ensure you are importing the resolver via `require('sprintf-kit/get-resolver')`.","message":"In v2.0.0, the `getResolver` utility was renamed to `getPartsResolver` internally, however, the public API path `sprintf-kit/get-resolver` remains the correct way to import the resolver. The change was internal and might affect direct deep imports that bypassed the `/get-resolver` entrypoint.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Update any code that processes the `flags` property to expect a string instead of an array. For example, `placeholder.flags.includes('0')` should become `placeholder.flags && placeholder.flags.includes('0')`.","message":"With v2.0.0, the `flags` property within the placeholder metadata returned by the `parse` utility changed from an array to a string for simplicity.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always pass an object mapping modifier types (e.g., `s`, `d`) to their respective resolver functions (e.g., `require('sprintf-kit/modifiers/s')`) when creating your format function.","message":"Unlike some `sprintf` implementations that provide a full suite of default modifiers, `sprintf-kit` requires you to explicitly configure which modifiers (`%s`, `%d`, etc.) your format function will support by providing them as an object to the `sprintf-kit` factory function. Unconfigured modifiers will be output as literal strings.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For consistent and predictable behavior, either use parameter indexing for all placeholders or for none. Review `isParameterIndexingValid` to ensure your format string logic is as intended.","message":"The `parse` utility returns an `isParameterIndexingValid` property. If you use parameter indexing (e.g., `%2$s`) in some but not all placeholders within a format string, this property will be `false`, indicating inconsistent usage. While not strictly an error, it flags a potential logical inconsistency in your format string design.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'2.0.2':87 '2018':104 'advanc':110 'allow':22 'also':61 'appli':83 'basic':19 'break':51 'bundl':127 'capabl':112 'configur':32,118 'control':74 'core':43 'creat':25 'current':84 'custom':26 'd':39 'default':131 'detail':46 'develop':23 'differenti':119 'e.g':36 'enabl':72 'explicit':31,116 'focus':105 'format':15,53,64,136 'formatt':20 'function':29,65 'generat':66,71 'granular':73 'implement':124 'infrequ':92 'intern':107 'javascript':9,132 'kit':3,6 'last':98 'librari':10,41 'like':28 'liter':56 'log':135 'mainten':95 'major':99 'metadata':59 'might':126 'modifi':34,117,129 'modular':13,114 'occur':102 'offer':62 'packag':89 'pars':111 'parser':17,49 'part':69 'placehold':58 'printf':14,47,133 'process':79 'provid':11 'recent':94 'resolv':35,70 'seen':91 'simpler':122 'specif':33 'sprintf':2,5,27,123,134 'sprintf-kit':1,4 'string':16,54,77,137 'structur':108 'substitut':81 'syntax':48 'updat':100 'v2.0.0':101 'version':86","created_at":"2026-04-20T01:57:38.968751+00:00","updated_at":"2026-04-20T01:57:38.968751+00:00","problems":[{"fix":"Ensure all desired modifiers are explicitly passed to the `require('sprintf-kit')` factory function. For example: `require('sprintf-kit')({ s: require('sprintf-kit/modifiers/s') });`","cause":"Attempting to use a placeholder type (e.g., %s) without explicitly configuring its corresponding modifier when creating the format function.","error":"TypeError: Cannot read properties of undefined (reading 's')"},{"fix":"This was a bug fixed in v2.0.1. Ensure your `sprintf-kit` version is 2.0.1 or newer. If on an older version, manually replace `%%` with a single `%` in your format string or upgrade the package.","cause":"Incorrect handling of the `%%` escape sequence, which should produce a literal percent sign.","error":"Output contains literal '%%' instead of a single '%' character."},{"fix":"Ensure the argument types match the expected modifier types. For complex objects, consider providing a custom modifier or a `j` (JSON) modifier if available and appropriate, or explicitly stringify the object before passing it.","cause":"The default modifiers (e.g., `d` for numbers, `s` for strings) might output placeholder tokens like `[Object]` or `[Error]` for values they cannot process or serialize, such as non-JSON-serializable objects.","error":"Invalid value: [Object] displayed in output instead of formatted content."}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/medikoo/sprintf-kit","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/sprintf-kit","openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}