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:
Type | Default Formatter |
---|---|
number , bigint , Number | Decimal formatter |
Date | Date formatter |
Array | List formatter, applies autoFormat to each entry |
Localizable | Returned as-is |
undefined , null | Empty value |
string , boolean | Stringification formatter |
Other | Stringification 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 value | Localized value |
---|---|
"Hello, world!" | Hello, world! |
undefined | |
null | |
true | true |
42 | 42 |
Math.PI | 3.142 |
Infinity | ∞ |
-Infinity | -∞ |
NaN | NaN |
[1, 2, 3] | 1, 2, 3 |
{ a: 1, b: 2 } | [object Object] |
/abc/ | /abc/ |
function() {} | function() { } |
new Date(2025, 5, 1) | 6/1/2025 |