Skip to content

autoFormat
@localizer/format 1.1.1

This formatter transforms any value into a locale-aware Localizable by leveraging preconfigured formatters. By default, it applies the following formatter based on the value's type:

TypeDefault Formatter
number, bigint, NumberDecimal formatter
DateDate formatter
ArrayList formatter, applies autoFormat to each entry
LocalizableReturned as-is
undefined, nullEmpty value
string, booleanStringification formatter
OtherStringification formatter

You can customize the behavior of autoFormat by configuring the DefaultFormatters settings. This allows you to specify which formatters should be applied to different value types.

typescript
configure(
  { DefaultFormatters },
  {
    DefaultFormatters: {
      number: decimal,
      date: date,
      array: list,
      boolean: stringify,
      string: stringify,
      default: stringify,
    },
  },
);

WARNING

Use autoFormat carefully. It handles various value types but lacks strict type safety and customization options.

Usage

typescript
import { 
autoFormat
} from '@localizer/format';
const
result
=
autoFormat
('Automatic formatting');

Demo

Input valueLocalized value
"Hello, world!"Hello, world!
undefined
null
truetrue
4242
Math.PI3.142
Infinity
-Infinity-∞
NaNNaN
[1, 2, 3]1, 2, 3
{ a: 1, b: 2 }[object Object]
/abc//abc/
function() {}function() { }
new Date(2025, 5, 1)6/1/2025

See also