@formatjs/intl 4.1.5 → 4.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +306 -20
- package/index.js +521 -14
- package/index.js.map +1 -0
- package/package.json +4 -5
- package/src/create-intl.d.ts +0 -14
- package/src/create-intl.js +0 -69
- package/src/dateTime.d.ts +0 -37
- package/src/dateTime.js +0 -86
- package/src/displayName.d.ts +0 -5
- package/src/displayName.js +0 -24
- package/src/error.d.ts +0 -35
- package/src/error.js +0 -68
- package/src/list.d.ts +0 -9
- package/src/list.js +0 -51
- package/src/message.d.ts +0 -15
- package/src/message.js +0 -106
- package/src/number.d.ts +0 -16
- package/src/number.js +0 -50
- package/src/plural.d.ts +0 -5
- package/src/plural.js +0 -19
- package/src/relativeTime.d.ts +0 -6
- package/src/relativeTime.js +0 -28
- package/src/types.d.ts +0 -111
- package/src/types.js +0 -3
- package/src/utils.d.ts +0 -15
- package/src/utils.js +0 -125
package/src/relativeTime.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import "./types.js";
|
|
2
|
-
import { getNamedFormat, filterProps } from "./utils.js";
|
|
3
|
-
import { FormatError, ErrorCode } from "intl-messageformat";
|
|
4
|
-
import { IntlFormatError } from "./error.js";
|
|
5
|
-
const RELATIVE_TIME_FORMAT_OPTIONS = ["numeric", "style"];
|
|
6
|
-
function getFormatter({ locale, formats, onError }, getRelativeTimeFormat, options = {}) {
|
|
7
|
-
const { format } = options;
|
|
8
|
-
const defaults = !!format && getNamedFormat(formats, "relative", format, onError) || {};
|
|
9
|
-
const filteredOptions = filterProps(options, RELATIVE_TIME_FORMAT_OPTIONS, defaults);
|
|
10
|
-
return getRelativeTimeFormat(locale, filteredOptions);
|
|
11
|
-
}
|
|
12
|
-
export function formatRelativeTime(config, getRelativeTimeFormat, value, unit, options = {}) {
|
|
13
|
-
if (!unit) {
|
|
14
|
-
unit = "second";
|
|
15
|
-
}
|
|
16
|
-
const RelativeTimeFormat = Intl.RelativeTimeFormat;
|
|
17
|
-
if (!RelativeTimeFormat) {
|
|
18
|
-
config.onError(new FormatError(`Intl.RelativeTimeFormat is not available in this environment.
|
|
19
|
-
Try polyfilling it using "@formatjs/intl-relativetimeformat"
|
|
20
|
-
`, ErrorCode.MISSING_INTL_API));
|
|
21
|
-
}
|
|
22
|
-
try {
|
|
23
|
-
return getFormatter(config, getRelativeTimeFormat, options).format(value, unit);
|
|
24
|
-
} catch (e) {
|
|
25
|
-
config.onError(new IntlFormatError("Error formatting relative time.", config.locale, e));
|
|
26
|
-
}
|
|
27
|
-
return String(value);
|
|
28
|
-
}
|
package/src/types.d.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { type MessageFormatElement } from "@formatjs/icu-messageformat-parser";
|
|
2
|
-
import { type NumberFormatOptions } from "@formatjs/ecma402-abstract";
|
|
3
|
-
import type { FormatError, IntlMessageFormat } from "intl-messageformat";
|
|
4
|
-
import { type Formats, type FormatXMLElementFn, type Options as IntlMessageFormatOptions, type PrimitiveType } from "intl-messageformat";
|
|
5
|
-
import type { InvalidConfigError, MessageFormatError, MissingDataError, MissingTranslationError, UnsupportedFormatterError } from "./error.js";
|
|
6
|
-
import type { DEFAULT_INTL_CONFIG } from "./utils.js";
|
|
7
|
-
export interface Part<T = string> {
|
|
8
|
-
type: "element" | "literal";
|
|
9
|
-
value: T;
|
|
10
|
-
}
|
|
11
|
-
declare global {
|
|
12
|
-
namespace FormatjsIntl {
|
|
13
|
-
interface Message {}
|
|
14
|
-
interface IntlConfig {}
|
|
15
|
-
interface Formats {}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
type MessageIds = FormatjsIntl.Message extends {
|
|
19
|
-
ids: infer T;
|
|
20
|
-
} ? T extends string ? T : string : string;
|
|
21
|
-
type Locale = FormatjsIntl.IntlConfig extends {
|
|
22
|
-
locale: infer T;
|
|
23
|
-
} ? T extends string ? T : string : string;
|
|
24
|
-
export type OnErrorFn = (err: MissingTranslationError | MessageFormatError | MissingDataError | InvalidConfigError | UnsupportedFormatterError | FormatError) => void;
|
|
25
|
-
export type OnWarnFn = (warning: string) => void;
|
|
26
|
-
/**
|
|
27
|
-
* Config for intl object.
|
|
28
|
-
* Generic type T is the type of potential rich text element. For example:
|
|
29
|
-
* With React, T would be React.ReactNode
|
|
30
|
-
*/
|
|
31
|
-
export interface ResolvedIntlConfig<T = string> {
|
|
32
|
-
locale: Locale;
|
|
33
|
-
timeZone?: string;
|
|
34
|
-
fallbackOnEmptyString?: boolean;
|
|
35
|
-
formats: CustomFormats;
|
|
36
|
-
messages: Record<MessageIds, string> | Record<MessageIds, MessageFormatElement[]>;
|
|
37
|
-
defaultLocale: string;
|
|
38
|
-
defaultFormats: CustomFormats;
|
|
39
|
-
defaultRichTextElements?: Record<string, FormatXMLElementFn<T>>;
|
|
40
|
-
onError: OnErrorFn;
|
|
41
|
-
onWarn?: OnWarnFn;
|
|
42
|
-
}
|
|
43
|
-
export interface CustomFormats extends Partial<Formats> {
|
|
44
|
-
relative?: Record<string, Intl.RelativeTimeFormatOptions>;
|
|
45
|
-
dateTimeRange?: Record<string, Intl.DateTimeFormatOptions>;
|
|
46
|
-
}
|
|
47
|
-
export interface CustomFormatConfig<Source = string> {
|
|
48
|
-
format?: Source extends keyof FormatjsIntl.Formats ? FormatjsIntl.Formats[Source] : string;
|
|
49
|
-
}
|
|
50
|
-
export type FormatDateTimeRangeOptions = Omit<Intl.DateTimeFormatOptions, "localeMatcher"> & CustomFormatConfig<"dateTimeRange">;
|
|
51
|
-
export type FormatDateOptions = Omit<Intl.DateTimeFormatOptions, "localeMatcher"> & CustomFormatConfig<"date">;
|
|
52
|
-
export type FormatTimeOptions = Omit<Intl.DateTimeFormatOptions, "localeMatcher"> & CustomFormatConfig<"time">;
|
|
53
|
-
export type FormatNumberOptions = Omit<NumberFormatOptions, "localeMatcher"> & CustomFormatConfig<"number">;
|
|
54
|
-
export type FormatRelativeTimeOptions = Omit<Intl.RelativeTimeFormatOptions, "localeMatcher"> & CustomFormatConfig<"time">;
|
|
55
|
-
export type FormatPluralOptions = Omit<Intl.PluralRulesOptions, "localeMatcher"> & CustomFormatConfig;
|
|
56
|
-
export type FormatListOptions = Omit<Intl.ListFormatOptions, "localeMatcher">;
|
|
57
|
-
export type FormatDisplayNameOptions = Omit<Intl.DisplayNamesOptions, "localeMatcher">;
|
|
58
|
-
/**
|
|
59
|
-
* `TBase` is the type constraints of the rich text element in the formatted output.
|
|
60
|
-
* For example, with React, `TBase` should be `React.ReactNode`.
|
|
61
|
-
*/
|
|
62
|
-
export interface IntlFormatters<TBase = unknown> {
|
|
63
|
-
formatDateTimeRange(this: void, from: Parameters<Intl.DateTimeFormat["formatRange"]>[0] | string, to: Parameters<Intl.DateTimeFormat["formatRange"]>[1] | string, opts?: FormatDateTimeRangeOptions): string;
|
|
64
|
-
formatDate(this: void, value: Parameters<Intl.DateTimeFormat["format"]>[0] | string, opts?: FormatDateOptions): string;
|
|
65
|
-
formatTime(this: void, value: Parameters<Intl.DateTimeFormat["format"]>[0] | string, opts?: FormatTimeOptions): string;
|
|
66
|
-
formatDateToParts(this: void, value: Parameters<Intl.DateTimeFormat["format"]>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
|
|
67
|
-
formatTimeToParts(this: void, value: Parameters<Intl.DateTimeFormat["format"]>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
|
|
68
|
-
formatRelativeTime(this: void, value: Parameters<Intl.RelativeTimeFormat["format"]>[0], unit?: Parameters<Intl.RelativeTimeFormat["format"]>[1], opts?: FormatRelativeTimeOptions): string;
|
|
69
|
-
formatNumber(this: void, value: Parameters<Intl.NumberFormat["format"]>[0], opts?: FormatNumberOptions): string;
|
|
70
|
-
formatNumberToParts(this: void, value: Parameters<Intl.NumberFormat["format"]>[0], opts?: FormatNumberOptions): Intl.NumberFormatPart[];
|
|
71
|
-
formatPlural(this: void, value: Parameters<Intl.PluralRules["select"]>[0], opts?: FormatPluralOptions): ReturnType<Intl.PluralRules["select"]>;
|
|
72
|
-
formatMessage(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>, opts?: IntlMessageFormatOptions): string;
|
|
73
|
-
formatMessage<
|
|
74
|
-
T extends TBase,
|
|
75
|
-
TValue extends T | FormatXMLElementFn<T>
|
|
76
|
-
>(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | TValue>, opts?: IntlMessageFormatOptions): string | T | Array<string | T>;
|
|
77
|
-
$t(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>, opts?: IntlMessageFormatOptions): string;
|
|
78
|
-
$t<T extends TBase>(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>, opts?: IntlMessageFormatOptions): string | T | (T | string)[];
|
|
79
|
-
formatList(this: void, values: Iterable<string>, opts?: FormatListOptions): string;
|
|
80
|
-
formatList<T extends TBase>(this: void, values: Iterable<string | T>, opts?: FormatListOptions): T | string | (string | T)[];
|
|
81
|
-
formatListToParts<T extends TBase>(this: void, values: Iterable<string | T>, opts?: FormatListOptions): Part[];
|
|
82
|
-
formatDisplayName(this: void, value: Parameters<Intl.DisplayNames["of"]>[0], opts: FormatDisplayNameOptions): string | undefined;
|
|
83
|
-
}
|
|
84
|
-
export interface Formatters {
|
|
85
|
-
getDateTimeFormat(this: void, ...args: ConstructorParameters<typeof Intl.DateTimeFormat>): Intl.DateTimeFormat;
|
|
86
|
-
getNumberFormat(this: void, locales?: string | string[], opts?: NumberFormatOptions): Intl.NumberFormat;
|
|
87
|
-
getMessageFormat(this: void, ...args: ConstructorParameters<typeof IntlMessageFormat>): IntlMessageFormat;
|
|
88
|
-
getRelativeTimeFormat(this: void, ...args: ConstructorParameters<typeof Intl.RelativeTimeFormat>): Intl.RelativeTimeFormat;
|
|
89
|
-
getPluralRules(this: void, ...args: ConstructorParameters<typeof Intl.PluralRules>): Intl.PluralRules;
|
|
90
|
-
getListFormat(this: void, ...args: ConstructorParameters<typeof Intl.ListFormat>): Intl.ListFormat;
|
|
91
|
-
getDisplayNames(this: void, ...args: ConstructorParameters<typeof Intl.DisplayNames>): Intl.DisplayNames;
|
|
92
|
-
}
|
|
93
|
-
export interface IntlShape<T = string> extends ResolvedIntlConfig<T>, IntlFormatters<T> {
|
|
94
|
-
formatters: Formatters;
|
|
95
|
-
}
|
|
96
|
-
export interface IntlCache {
|
|
97
|
-
dateTime: Record<string, Intl.DateTimeFormat>;
|
|
98
|
-
number: Record<string, Intl.NumberFormat>;
|
|
99
|
-
message: Record<string, IntlMessageFormat>;
|
|
100
|
-
relativeTime: Record<string, Intl.RelativeTimeFormat>;
|
|
101
|
-
pluralRules: Record<string, Intl.PluralRules>;
|
|
102
|
-
list: Record<string, Intl.ListFormat>;
|
|
103
|
-
displayNames: Record<string, Intl.DisplayNames>;
|
|
104
|
-
}
|
|
105
|
-
export interface MessageDescriptor {
|
|
106
|
-
id?: MessageIds;
|
|
107
|
-
description?: string | object;
|
|
108
|
-
defaultMessage?: string | MessageFormatElement[];
|
|
109
|
-
}
|
|
110
|
-
export type IntlConfig<T = string> = Omit<ResolvedIntlConfig<T>, keyof typeof DEFAULT_INTL_CONFIG> & Partial<typeof DEFAULT_INTL_CONFIG>;
|
|
111
|
-
export {};
|
package/src/types.js
DELETED
package/src/utils.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { type NumberFormatOptions } from "@formatjs/ecma402-abstract";
|
|
2
|
-
import { type CustomFormats, type Formatters, type IntlCache, type OnErrorFn, type ResolvedIntlConfig } from "./types.js";
|
|
3
|
-
export declare function invariant(condition: boolean, message: string, Err?: any): asserts condition;
|
|
4
|
-
export declare function filterProps<
|
|
5
|
-
T extends Record<string, any>,
|
|
6
|
-
K extends string
|
|
7
|
-
>(props: T, allowlist: Array<K>, defaults?: Partial<T>): Pick<T, K>;
|
|
8
|
-
export declare const DEFAULT_INTL_CONFIG: Pick<ResolvedIntlConfig<any>, "fallbackOnEmptyString" | "formats" | "messages" | "timeZone" | "defaultLocale" | "defaultFormats" | "onError" | "onWarn">;
|
|
9
|
-
export declare function createIntlCache(): IntlCache;
|
|
10
|
-
/**
|
|
11
|
-
* Create intl formatters and populate cache
|
|
12
|
-
* @param cache explicit cache to prevent leaking memory
|
|
13
|
-
*/
|
|
14
|
-
export declare function createFormatters(cache?: IntlCache): Formatters;
|
|
15
|
-
export declare function getNamedFormat<T extends keyof CustomFormats>(formats: CustomFormats, type: T, name: string, onError: OnErrorFn): NumberFormatOptions | Intl.DateTimeFormatOptions | Intl.RelativeTimeFormatOptions | undefined;
|
package/src/utils.js
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import "@formatjs/ecma402-abstract";
|
|
2
|
-
import { memoize, strategies } from "@formatjs/fast-memoize";
|
|
3
|
-
import { IntlMessageFormat } from "intl-messageformat";
|
|
4
|
-
import { UnsupportedFormatterError } from "./error.js";
|
|
5
|
-
import "./types.js";
|
|
6
|
-
export function invariant(condition, message, Err = Error) {
|
|
7
|
-
if (!condition) {
|
|
8
|
-
throw new Err(message);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
export function filterProps(props, allowlist, defaults = {}) {
|
|
12
|
-
return allowlist.reduce((filtered, name) => {
|
|
13
|
-
if (name in props) {
|
|
14
|
-
filtered[name] = props[name];
|
|
15
|
-
} else if (name in defaults) {
|
|
16
|
-
filtered[name] = defaults[name];
|
|
17
|
-
}
|
|
18
|
-
return filtered;
|
|
19
|
-
}, {});
|
|
20
|
-
}
|
|
21
|
-
const defaultErrorHandler = (error) => {
|
|
22
|
-
// @ts-ignore just so we don't need to declare dep on @types/node
|
|
23
|
-
if (process.env.NODE_ENV !== "production") {
|
|
24
|
-
console.error(error);
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
const defaultWarnHandler = (warning) => {
|
|
28
|
-
// @ts-ignore just so we don't need to declare dep on @types/node
|
|
29
|
-
if (process.env.NODE_ENV !== "production") {
|
|
30
|
-
console.warn(warning);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
export const DEFAULT_INTL_CONFIG = {
|
|
34
|
-
formats: {},
|
|
35
|
-
messages: {},
|
|
36
|
-
timeZone: undefined,
|
|
37
|
-
defaultLocale: "en",
|
|
38
|
-
defaultFormats: {},
|
|
39
|
-
fallbackOnEmptyString: true,
|
|
40
|
-
onError: defaultErrorHandler,
|
|
41
|
-
onWarn: defaultWarnHandler
|
|
42
|
-
};
|
|
43
|
-
export function createIntlCache() {
|
|
44
|
-
return {
|
|
45
|
-
dateTime: {},
|
|
46
|
-
number: {},
|
|
47
|
-
message: {},
|
|
48
|
-
relativeTime: {},
|
|
49
|
-
pluralRules: {},
|
|
50
|
-
list: {},
|
|
51
|
-
displayNames: {}
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
function createFastMemoizeCache(store) {
|
|
55
|
-
return { create() {
|
|
56
|
-
return {
|
|
57
|
-
get(key) {
|
|
58
|
-
return store[key];
|
|
59
|
-
},
|
|
60
|
-
set(key, value) {
|
|
61
|
-
store[key] = value;
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
} };
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Create intl formatters and populate cache
|
|
68
|
-
* @param cache explicit cache to prevent leaking memory
|
|
69
|
-
*/
|
|
70
|
-
export function createFormatters(cache = createIntlCache()) {
|
|
71
|
-
const RelativeTimeFormat = Intl.RelativeTimeFormat;
|
|
72
|
-
const ListFormat = Intl.ListFormat;
|
|
73
|
-
const DisplayNames = Intl.DisplayNames;
|
|
74
|
-
const getDateTimeFormat = memoize((...args) => new Intl.DateTimeFormat(...args), {
|
|
75
|
-
cache: createFastMemoizeCache(cache.dateTime),
|
|
76
|
-
strategy: strategies.variadic
|
|
77
|
-
});
|
|
78
|
-
const getNumberFormat = memoize((...args) => new Intl.NumberFormat(...args), {
|
|
79
|
-
cache: createFastMemoizeCache(cache.number),
|
|
80
|
-
strategy: strategies.variadic
|
|
81
|
-
});
|
|
82
|
-
const getPluralRules = memoize((...args) => new Intl.PluralRules(...args), {
|
|
83
|
-
cache: createFastMemoizeCache(cache.pluralRules),
|
|
84
|
-
strategy: strategies.variadic
|
|
85
|
-
});
|
|
86
|
-
return {
|
|
87
|
-
getDateTimeFormat,
|
|
88
|
-
getNumberFormat,
|
|
89
|
-
getMessageFormat: memoize((message, locales, overrideFormats, opts) => new IntlMessageFormat(message, locales, overrideFormats, {
|
|
90
|
-
formatters: {
|
|
91
|
-
getNumberFormat,
|
|
92
|
-
getDateTimeFormat,
|
|
93
|
-
getPluralRules
|
|
94
|
-
},
|
|
95
|
-
...opts
|
|
96
|
-
}), {
|
|
97
|
-
cache: createFastMemoizeCache(cache.message),
|
|
98
|
-
strategy: strategies.variadic
|
|
99
|
-
}),
|
|
100
|
-
getRelativeTimeFormat: memoize((...args) => new RelativeTimeFormat(...args), {
|
|
101
|
-
cache: createFastMemoizeCache(cache.relativeTime),
|
|
102
|
-
strategy: strategies.variadic
|
|
103
|
-
}),
|
|
104
|
-
getPluralRules,
|
|
105
|
-
getListFormat: memoize((...args) => new ListFormat(...args), {
|
|
106
|
-
cache: createFastMemoizeCache(cache.list),
|
|
107
|
-
strategy: strategies.variadic
|
|
108
|
-
}),
|
|
109
|
-
getDisplayNames: memoize((...args) => new DisplayNames(...args), {
|
|
110
|
-
cache: createFastMemoizeCache(cache.displayNames),
|
|
111
|
-
strategy: strategies.variadic
|
|
112
|
-
})
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
export function getNamedFormat(formats, type, name, onError) {
|
|
116
|
-
const formatType = formats && formats[type];
|
|
117
|
-
let format;
|
|
118
|
-
if (formatType) {
|
|
119
|
-
format = formatType[name];
|
|
120
|
-
}
|
|
121
|
-
if (format) {
|
|
122
|
-
return format;
|
|
123
|
-
}
|
|
124
|
-
onError(new UnsupportedFormatterError(`No ${type} format named: ${name}`));
|
|
125
|
-
}
|