@e22m4u/js-format
English | Русский
The string interpolation utility for JavaScript.
- standard specifiers
%s
, %d
, and %j
(see util.format
) %v
outputs primitive value or constructor name%l
outputs an array as a comma-separated list "foo", 10, true
Additionally:
- built-in
Errorf
class with error message interpolation
Motivation
Аn error message may need to interpolate certain values
in the message string. In this case, a problem is accurately
determine the type of the output value, because the standard
specifier %s
treats strings, numbers, and other types
in the same way.
import {format} from 'util';
console.log(format(`A boolean required, but %s given.`, 8));
console.log(format(`A boolean required, but %s given.`, '8'));
In the example above, the string '8'
and the number 8
are output identically. A similar problem occurs with objects.
import {format} from 'util';
console.log(format(`A boolean required, but %s given.`, new Date()));
console.log(format(`A boolean required, but %s given.`, 'Oct 18 2024 13:04:30'));
The module extends standard specifiers with additional
%v
and %l
, which wrap strings in quotes and output
a constructor name for objects.
import {format} from '@e22m4u/js-format';
console.log(format(`A boolean required, but %v given.`, 8));
console.log(format(`A boolean required, but %v given.`, '8'));
console.log(format(`A boolean required, but %v given.`, new Date()));
console.log(format(`A boolean required, but %v given.`, 'Oct 18 2024 13:04:30'));
For more details about the new specifiers,
see the Specifiers section.
Installation
npm install @e22m4u/js-format
The module supports ESM and CommonJS standards.
ESM
import {format} from '@e22m4u/js-format';
CommonJS
const {format} = require('@e22m4u/js-format');
Specifiers
The format
method returns a formatted string using
the first argument as a printf-like template which can
contain zero or more format specifiers. Each specifier
is replaced with the converted value from the corresponding
argument.
specifier | description |
---|
%s | String will be used to convert a value |
%d | Number will be used to convert a value |
%j | JSON representation |
%v | see below |
%l | see below |
%v
Strings are wrapped in quotes, other primitives are converted
to strings, and for more complex types the constructor name
is output.
format('It is %v', 'foo');
format('It is %v', '');
format('It is %v', 10);
format('It is %v', 0);
format('It is %v', NaN);
format('It is %v', Infinity);
format('It is %v', true);
format('It is %v', false);
format('It is %v', {foo: 'bar'});
format('It is %v', new Date());
format('It is %v', new Map());
format('It is %v', () => 10);
format('It is %v', undefined);
format('It is %v', null);
The %v
specifier was designed to output values in error messages
when it's important to have an idea of their types. However, outputting
the contents of an object may be excessive for such a task. For this reason,
objects are converted to the constructor name, which allows for a relatively
accurate determination of the output value's type.
class MyClass {}
format('It is %v', 'MyClass');
format('It is %v', MyClass);
format('It is %v', new MyClass());
%l
Outputs array elements separated by commas.
format('An array of %l', ['foo', 10, true]);
Array elements are converted to strings following
the logic of the %v
specifier.
Errorf
The Errorf
class constructor passes its arguments
to the format
function to form an error message.
Example:
import {Errorf} from '@e22m4u/js-format';
throw new Errorf(
'It requires one of %l, but %v given.',
[true, false, 'y', 'n'],
new Map(),
);
Tests
npm run test
License
MIT