パッケージの詳細

@visulima/fmt

visulima3.5kMIT1.1.15

Util.format-like string formatting utility.

anolilab, fmt, format, formatter

readme

Visulima fmt (util.format)

Util.format-like unescaped string formatting utility based on quick-format-unescaped.


typescript-image npm-image license-image

Daniel Bannert's open source work is supported by the community on GitHub Sponsors


Install

npm install @visulima/fmt
yarn add @visulima/fmt
pnpm add @visulima/fmt

Usage

import { format } from "@visulima/fmt";

const formatted = format("hello %s %j %d", ["world", [{ obj: true }, 4, { another: "obj" }]]);

console.log(formatted); // hello world [{"obj":true},4,{"another":"obj"}] NaN

format(fmt, parameters, [options])

fmt

A printf-like format string. Example: 'hello %s %j %d'

parameters

Array of values to be inserted into the format string. Example: ['world', {obj:true}]

options.stringify

Passing an options object as the third parameter with a stringify will mean any objects will be passed to the supplied function instead of an the internal tryStringify function. This can be useful when using augmented capability serializers such as fast-safe-stringify or fast-redact.

uses JSON.stringify instead of util.inspect, this means functions will not be serialized.

build

With the build function you can generate a format function that is optimized for your use case.

import { build } from "@visulima/fmt";

const format = build({
    formatters: {
        // Pass in whatever % interpolator you want, as long as it's a single character;
        // in this case, it's `t`.
        // The formatter should be a function that takes in a value and returns the formatted value.
        t: (time) => new Date(time).toLocaleString(),
    },
});

const formatted = format("hello %s at %t", ["world", Date.now()]);

console.log(formatted); // hello world at 1/1/1970, 1:00:00 AM

Format Specifiers

Format specifiers are dependent on the type of data-elements that are to be added to the string. The most commonly used format specifiers supported are:

Specifier Description
%s Converts all values except for BigInt, -0 and Object to a string.
%d Used to convert any value to Number of any type other than BigInt and Symbol.
%i Used for all values except BigInt and Symbol.
%f Used to convert a value to type Float. It does not support conversion of values of type Symbol.
%j Used to add JSON data. If a circular reference is present, the string ‘[Circular]’ is added instead.
%o Adds the string representation of an object. Note that it does not contain non-enumerable characteristics of the object.
%O Adds the string representation of an object. Note that it will contain all characteristics of the object, including non-enumerable ones.
%c Will parse basic CSS from the substitution subject like color: red into ANSI color codes. These codes will then be placed where the %c specifier is. Supported CSS properties are color, background-color, font-weight, font-style, text-decoration, text-decoration-color, and text-decoration-line. Unsupported CSS properties are ignored. An empty %c CSS string substitution will become an ANSI style reset. If color is disabled, %c is ignored.
%% Used to add the % sign.

Benchmark

See benchmark

Supported Node.js Versions

Libraries in this ecosystem make the best effort to track Node.js’ release schedule. Here’s a post on why we think this is important.

Contributing

If you would like to help take a look at the list of issues and check our Contributing guild.

Note: please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Credits

License

The visulima fmt is open-sourced software licensed under the MIT

更新履歴

@visulima/fmt 1.1.15 (2025-03-07)

Bug Fixes

  • updated @visulima/packem and other dev deps, for better bundling size (e940581)

Miscellaneous Chores

  • updated dev dependencies (487a976)

@visulima/fmt 1.1.14 (2025-01-25)

Bug Fixes

  • fixed wrong node version range in package.json (4ae2929)

Miscellaneous Chores

  • fixed typescript url (fe65a8c)
  • updated all dev dependencies (37fb298)
  • updated all dev dependencies and all dependencies in the app folder (87f4ccb)

@visulima/fmt 1.1.13 (2025-01-12)

Bug Fixes

  • updated @visulima/packem, and all other dev dependencies (7797a1c)

Miscellaneous Chores

  • updated dev dependencies (9de2eab)

@visulima/fmt 1.1.12 (2024-12-12)

Bug Fixes

  • allow node v23 (8ca929a)
  • allowed node 23, updated dev dependencies (f99d34e)
  • updated packem to v1.8.2 (23f869b)
  • updated packem to v1.9.2 (47bdc2d)

Styles

Miscellaneous Chores

  • updated dev dependencies (a916944)

@visulima/fmt 1.1.11 (2024-10-05)

Bug Fixes

  • updated dev dependencies, updated packem to v1.0.7, fixed naming of some lint config files (c071a9c)

@visulima/fmt 1.1.10 (2024-09-24)

Bug Fixes

  • update packem to v1 (05f3bc9)
  • updated esbuild from v0.23 to v0.24 (3793010)

Miscellaneous Chores

  • updated dev dependencies (05edb67)

@visulima/fmt 1.1.9 (2024-09-11)

Bug Fixes

Miscellaneous Chores

  • updated dev dependencies (28b5ee5)

@visulima/fmt 1.1.8 (2024-09-07)

Bug Fixes

  • fixed broken chunk splitting from packem (1aaf277)

@visulima/fmt 1.1.7 (2024-09-07)

Bug Fixes

  • added types support for node10 (604583f)

Miscellaneous Chores

  • update dev dependencies (0738f98)
  • updated dev dependencies (45c2a76)

@visulima/fmt 1.1.6 (2024-08-01)

Bug Fixes

  • fmt: switched to packem from tsup (24d86a9)

Miscellaneous Chores

  • changed typescript version back to 5.4.5 (55d28bb)
  • cs fixes (ab59375)
  • updated all dev deps (ef143ce)
  • updated dev dependencies (ac67ec1)
  • updated dev dependencies and sorted the package.json (9571572)

Build System

  • fixed found audit error, updated all dev package deps, updated deps in apps and examples (4c51950)

@visulima/fmt 1.1.5 (2024-06-06)

Bug Fixes

Styles

  • cs fixes on some package.json files (12fc0f7)

Miscellaneous Chores

  • changed semantic-release-npm to pnpm (b6d100a)
  • deps: updated dev deps (d91ac38)
  • downgrade eslint-plugin-vitest (0162771)
  • update dev dependencies (09c4854)
  • updated dev dependencies (a2e0504)
  • updated dev dependencies (abd319c)
  • updated dev dependencies (0767afe)
  • updated dev dependencies (d7791e3)
  • updated dev dependencies (6005345)
  • updated dev dependencies (87dee15)
  • updated dev dependencies (bf2c635)
  • updated dev dependencies (f67c7f1)

@visulima/fmt 1.1.4 (2024-03-27)

Bug Fixes

  • added missing os key to package.json (4ad1268)

@visulima/fmt 1.1.3 (2024-03-04)

Bug Fixes

  • fixed all found type issues (eaa40d1)
  • minifyWhitespace on prod build, removed @tsconfig/* configs (410cb73)

@visulima/fmt 1.1.2 (2024-02-26)

Bug Fixes

  • moved custom formatters into the default switch call (eaf550b)

@visulima/fmt 1.1.1 (2024-02-19)

Bug Fixes

@visulima/fmt 1.1.0 (2024-02-19)

Features

  • added %c to ANSI transform (dcf0bff)

@visulima/fmt 1.0.1 (2024-01-19)

Bug Fixes

  • changed wrong typing (e11089e)
  • updated all deps, updated test based on eslint errors (909f8f3)

@visulima/fmt 1.0.0 (2023-12-13)

Features