@formatjs/intl 3.1.8 → 4.0.1
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 +14 -14
- package/index.js +13 -36
- package/package.json +10 -7
- package/src/create-intl.d.ts +1 -1
- package/src/create-intl.js +17 -20
- package/src/dateTime.d.ts +1 -1
- package/src/dateTime.js +17 -25
- package/src/displayName.d.ts +1 -1
- package/src/displayName.js +7 -10
- package/src/error.d.ts +1 -1
- package/src/error.js +17 -20
- package/src/list.d.ts +1 -1
- package/src/list.js +10 -14
- package/src/message.d.ts +1 -1
- package/src/message.js +19 -23
- package/src/number.d.ts +1 -1
- package/src/number.js +9 -14
- package/src/plural.d.ts +1 -1
- package/src/plural.js +7 -10
- package/src/relativeTime.d.ts +1 -1
- package/src/relativeTime.js +8 -11
- package/src/types.d.ts +4 -3
- package/src/types.js +1 -2
- package/src/utils.d.ts +1 -1
- package/src/utils.js +33 -41
- package/lib/index.d.ts +0 -16
- package/lib/index.js +0 -17
- package/lib/src/create-intl.d.ts +0 -10
- package/lib/src/create-intl.js +0 -51
- package/lib/src/dateTime.d.ts +0 -37
- package/lib/src/dateTime.js +0 -117
- package/lib/src/displayName.d.ts +0 -5
- package/lib/src/displayName.js +0 -23
- package/lib/src/error.d.ts +0 -35
- package/lib/src/error.js +0 -91
- package/lib/src/list.d.ts +0 -9
- package/lib/src/list.js +0 -61
- package/lib/src/message.d.ts +0 -15
- package/lib/src/message.js +0 -103
- package/lib/src/number.d.ts +0 -16
- package/lib/src/number.js +0 -58
- package/lib/src/plural.d.ts +0 -5
- package/lib/src/plural.js +0 -19
- package/lib/src/relativeTime.d.ts +0 -6
- package/lib/src/relativeTime.js +0 -29
- package/lib/src/types.d.ts +0 -109
- package/lib/src/types.js +0 -1
- package/lib/src/utils.d.ts +0 -12
- package/lib/src/utils.js +0 -168
package/lib/src/types.d.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
|
|
2
|
-
import { NumberFormatOptions } from '@formatjs/ecma402-abstract';
|
|
3
|
-
import { FormatError, Formats, FormatXMLElementFn, IntlMessageFormat, Options as IntlMessageFormatOptions, PrimitiveType } from 'intl-messageformat';
|
|
4
|
-
import { InvalidConfigError, MessageFormatError, MissingDataError, MissingTranslationError, UnsupportedFormatterError } from './error';
|
|
5
|
-
import { DEFAULT_INTL_CONFIG } from './utils';
|
|
6
|
-
export interface Part<T = string> {
|
|
7
|
-
type: 'element' | 'literal';
|
|
8
|
-
value: T;
|
|
9
|
-
}
|
|
10
|
-
declare global {
|
|
11
|
-
namespace FormatjsIntl {
|
|
12
|
-
interface Message {
|
|
13
|
-
}
|
|
14
|
-
interface IntlConfig {
|
|
15
|
-
}
|
|
16
|
-
interface Formats {
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
type MessageIds = FormatjsIntl.Message extends {
|
|
21
|
-
ids: infer T;
|
|
22
|
-
} ? T extends string ? T : string : string;
|
|
23
|
-
type Locale = FormatjsIntl.IntlConfig extends {
|
|
24
|
-
locale: infer T;
|
|
25
|
-
} ? T extends string ? T : string : string;
|
|
26
|
-
export type OnErrorFn = (err: MissingTranslationError | MessageFormatError | MissingDataError | InvalidConfigError | UnsupportedFormatterError | FormatError) => void;
|
|
27
|
-
export type OnWarnFn = (warning: string) => void;
|
|
28
|
-
/**
|
|
29
|
-
* Config for intl object.
|
|
30
|
-
* Generic type T is the type of potential rich text element. For example:
|
|
31
|
-
* With React, T would be React.ReactNode
|
|
32
|
-
*/
|
|
33
|
-
export interface ResolvedIntlConfig<T = string> {
|
|
34
|
-
locale: Locale;
|
|
35
|
-
timeZone?: string;
|
|
36
|
-
fallbackOnEmptyString?: boolean;
|
|
37
|
-
formats: CustomFormats;
|
|
38
|
-
messages: Record<MessageIds, string> | Record<MessageIds, MessageFormatElement[]>;
|
|
39
|
-
defaultLocale: string;
|
|
40
|
-
defaultFormats: CustomFormats;
|
|
41
|
-
defaultRichTextElements?: Record<string, FormatXMLElementFn<T>>;
|
|
42
|
-
onError: OnErrorFn;
|
|
43
|
-
onWarn?: OnWarnFn;
|
|
44
|
-
}
|
|
45
|
-
export interface CustomFormats extends Partial<Formats> {
|
|
46
|
-
relative?: Record<string, Intl.RelativeTimeFormatOptions>;
|
|
47
|
-
dateTimeRange?: Record<string, Intl.DateTimeFormatOptions>;
|
|
48
|
-
}
|
|
49
|
-
export interface CustomFormatConfig<Source = string> {
|
|
50
|
-
format?: Source extends keyof FormatjsIntl.Formats ? FormatjsIntl.Formats[Source] : string;
|
|
51
|
-
}
|
|
52
|
-
export type FormatDateTimeRangeOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'dateTimeRange'>;
|
|
53
|
-
export type FormatDateOptions = Omit<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'date'>;
|
|
54
|
-
export type FormatNumberOptions = Omit<NumberFormatOptions, 'localeMatcher'> & CustomFormatConfig<'number'>;
|
|
55
|
-
export type FormatRelativeTimeOptions = Omit<Intl.RelativeTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig<'time'>;
|
|
56
|
-
export type FormatPluralOptions = Omit<Intl.PluralRulesOptions, 'localeMatcher'> & CustomFormatConfig;
|
|
57
|
-
export type FormatListOptions = Omit<Intl.ListFormatOptions, 'localeMatcher'>;
|
|
58
|
-
export type FormatDisplayNameOptions = Omit<Intl.DisplayNamesOptions, 'localeMatcher'>;
|
|
59
|
-
/**
|
|
60
|
-
* `TBase` is the type constraints of the rich text element in the formatted output.
|
|
61
|
-
* For example, with React, `TBase` should be `React.ReactNode`.
|
|
62
|
-
*/
|
|
63
|
-
export interface IntlFormatters<TBase = unknown> {
|
|
64
|
-
formatDateTimeRange(this: void, from: Parameters<Intl.DateTimeFormat['formatRange']>[0] | string, to: Parameters<Intl.DateTimeFormat['formatRange']>[1] | string, opts?: FormatDateTimeRangeOptions): string;
|
|
65
|
-
formatDate(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
66
|
-
formatTime(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
|
|
67
|
-
formatDateToParts(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
|
|
68
|
-
formatTimeToParts(this: void, value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
|
|
69
|
-
formatRelativeTime(this: void, value: Parameters<Intl.RelativeTimeFormat['format']>[0], unit?: Parameters<Intl.RelativeTimeFormat['format']>[1], opts?: FormatRelativeTimeOptions): string;
|
|
70
|
-
formatNumber(this: void, value: Parameters<Intl.NumberFormat['format']>[0], opts?: FormatNumberOptions): string;
|
|
71
|
-
formatNumberToParts(this: void, value: Parameters<Intl.NumberFormat['format']>[0], opts?: FormatNumberOptions): Intl.NumberFormatPart[];
|
|
72
|
-
formatPlural(this: void, value: Parameters<Intl.PluralRules['select']>[0], opts?: FormatPluralOptions): ReturnType<Intl.PluralRules['select']>;
|
|
73
|
-
formatMessage(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>, opts?: IntlMessageFormatOptions): string;
|
|
74
|
-
formatMessage<T extends TBase, TValue extends T | FormatXMLElementFn<T>>(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | TValue>, opts?: IntlMessageFormatOptions): string | T | Array<string | T>;
|
|
75
|
-
$t(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>, opts?: IntlMessageFormatOptions): string;
|
|
76
|
-
$t<T extends TBase>(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>, opts?: IntlMessageFormatOptions): string | T | (T | string)[];
|
|
77
|
-
formatList(this: void, values: Iterable<string>, opts?: FormatListOptions): string;
|
|
78
|
-
formatList<T extends TBase>(this: void, values: Iterable<string | T>, opts?: FormatListOptions): T | string | (string | T)[];
|
|
79
|
-
formatListToParts<T extends TBase>(this: void, values: Iterable<string | T>, opts?: FormatListOptions): Part[];
|
|
80
|
-
formatDisplayName(this: void, value: Parameters<Intl.DisplayNames['of']>[0], opts: FormatDisplayNameOptions): string | undefined;
|
|
81
|
-
}
|
|
82
|
-
export interface Formatters {
|
|
83
|
-
getDateTimeFormat(this: void, ...args: ConstructorParameters<typeof Intl.DateTimeFormat>): Intl.DateTimeFormat;
|
|
84
|
-
getNumberFormat(this: void, locales?: string | string[], opts?: NumberFormatOptions): Intl.NumberFormat;
|
|
85
|
-
getMessageFormat(this: void, ...args: ConstructorParameters<typeof IntlMessageFormat>): IntlMessageFormat;
|
|
86
|
-
getRelativeTimeFormat(this: void, ...args: ConstructorParameters<typeof Intl.RelativeTimeFormat>): Intl.RelativeTimeFormat;
|
|
87
|
-
getPluralRules(this: void, ...args: ConstructorParameters<typeof Intl.PluralRules>): Intl.PluralRules;
|
|
88
|
-
getListFormat(this: void, ...args: ConstructorParameters<typeof Intl.ListFormat>): Intl.ListFormat;
|
|
89
|
-
getDisplayNames(this: void, ...args: ConstructorParameters<typeof Intl.DisplayNames>): Intl.DisplayNames;
|
|
90
|
-
}
|
|
91
|
-
export interface IntlShape<T = string> extends ResolvedIntlConfig<T>, IntlFormatters<T> {
|
|
92
|
-
formatters: Formatters;
|
|
93
|
-
}
|
|
94
|
-
export interface IntlCache {
|
|
95
|
-
dateTime: Record<string, Intl.DateTimeFormat>;
|
|
96
|
-
number: Record<string, Intl.NumberFormat>;
|
|
97
|
-
message: Record<string, IntlMessageFormat>;
|
|
98
|
-
relativeTime: Record<string, Intl.RelativeTimeFormat>;
|
|
99
|
-
pluralRules: Record<string, Intl.PluralRules>;
|
|
100
|
-
list: Record<string, Intl.ListFormat>;
|
|
101
|
-
displayNames: Record<string, Intl.DisplayNames>;
|
|
102
|
-
}
|
|
103
|
-
export interface MessageDescriptor {
|
|
104
|
-
id?: MessageIds;
|
|
105
|
-
description?: string | object;
|
|
106
|
-
defaultMessage?: string | MessageFormatElement[];
|
|
107
|
-
}
|
|
108
|
-
export type IntlConfig<T = string> = Omit<ResolvedIntlConfig<T>, keyof typeof DEFAULT_INTL_CONFIG> & Partial<typeof DEFAULT_INTL_CONFIG>;
|
|
109
|
-
export {};
|
package/lib/src/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/src/utils.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { NumberFormatOptions } from '@formatjs/ecma402-abstract';
|
|
2
|
-
import { CustomFormats, Formatters, IntlCache, OnErrorFn, ResolvedIntlConfig } from './types';
|
|
3
|
-
export declare function invariant(condition: boolean, message: string, Err?: any): asserts condition;
|
|
4
|
-
export declare function filterProps<T extends Record<string, any>, K extends string>(props: T, allowlist: Array<K>, defaults?: Partial<T>): Pick<T, K>;
|
|
5
|
-
export declare const DEFAULT_INTL_CONFIG: Pick<ResolvedIntlConfig<any>, 'fallbackOnEmptyString' | 'formats' | 'messages' | 'timeZone' | 'defaultLocale' | 'defaultFormats' | 'onError' | 'onWarn'>;
|
|
6
|
-
export declare function createIntlCache(): IntlCache;
|
|
7
|
-
/**
|
|
8
|
-
* Create intl formatters and populate cache
|
|
9
|
-
* @param cache explicit cache to prevent leaking memory
|
|
10
|
-
*/
|
|
11
|
-
export declare function createFormatters(cache?: IntlCache): Formatters;
|
|
12
|
-
export declare function getNamedFormat<T extends keyof CustomFormats>(formats: CustomFormats, type: T, name: string, onError: OnErrorFn): NumberFormatOptions | Intl.DateTimeFormatOptions | Intl.RelativeTimeFormatOptions | undefined;
|
package/lib/src/utils.js
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import { __assign, __spreadArray } from "tslib";
|
|
2
|
-
import { memoize, strategies } from '@formatjs/fast-memoize';
|
|
3
|
-
import { IntlMessageFormat } from 'intl-messageformat';
|
|
4
|
-
import { UnsupportedFormatterError } from './error';
|
|
5
|
-
export function invariant(condition, message, Err) {
|
|
6
|
-
if (Err === void 0) { Err = Error; }
|
|
7
|
-
if (!condition) {
|
|
8
|
-
throw new Err(message);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
export function filterProps(props, allowlist, defaults) {
|
|
12
|
-
if (defaults === void 0) { defaults = {}; }
|
|
13
|
-
return allowlist.reduce(function (filtered, name) {
|
|
14
|
-
if (name in props) {
|
|
15
|
-
filtered[name] = props[name];
|
|
16
|
-
}
|
|
17
|
-
else if (name in defaults) {
|
|
18
|
-
filtered[name] = defaults[name];
|
|
19
|
-
}
|
|
20
|
-
return filtered;
|
|
21
|
-
}, {});
|
|
22
|
-
}
|
|
23
|
-
var defaultErrorHandler = function (error) {
|
|
24
|
-
// @ts-ignore just so we don't need to declare dep on @types/node
|
|
25
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
26
|
-
console.error(error);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
var defaultWarnHandler = function (warning) {
|
|
30
|
-
// @ts-ignore just so we don't need to declare dep on @types/node
|
|
31
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
32
|
-
console.warn(warning);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
export var DEFAULT_INTL_CONFIG = {
|
|
36
|
-
formats: {},
|
|
37
|
-
messages: {},
|
|
38
|
-
timeZone: undefined,
|
|
39
|
-
defaultLocale: 'en',
|
|
40
|
-
defaultFormats: {},
|
|
41
|
-
fallbackOnEmptyString: true,
|
|
42
|
-
onError: defaultErrorHandler,
|
|
43
|
-
onWarn: defaultWarnHandler,
|
|
44
|
-
};
|
|
45
|
-
export function createIntlCache() {
|
|
46
|
-
return {
|
|
47
|
-
dateTime: {},
|
|
48
|
-
number: {},
|
|
49
|
-
message: {},
|
|
50
|
-
relativeTime: {},
|
|
51
|
-
pluralRules: {},
|
|
52
|
-
list: {},
|
|
53
|
-
displayNames: {},
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
function createFastMemoizeCache(store) {
|
|
57
|
-
return {
|
|
58
|
-
create: function () {
|
|
59
|
-
return {
|
|
60
|
-
get: function (key) {
|
|
61
|
-
return store[key];
|
|
62
|
-
},
|
|
63
|
-
set: function (key, value) {
|
|
64
|
-
store[key] = value;
|
|
65
|
-
},
|
|
66
|
-
};
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Create intl formatters and populate cache
|
|
72
|
-
* @param cache explicit cache to prevent leaking memory
|
|
73
|
-
*/
|
|
74
|
-
export function createFormatters(cache) {
|
|
75
|
-
if (cache === void 0) { cache = createIntlCache(); }
|
|
76
|
-
var RelativeTimeFormat = Intl.RelativeTimeFormat;
|
|
77
|
-
var ListFormat = Intl.ListFormat;
|
|
78
|
-
var DisplayNames = Intl.DisplayNames;
|
|
79
|
-
var getDateTimeFormat = memoize(function () {
|
|
80
|
-
var _a;
|
|
81
|
-
var args = [];
|
|
82
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
83
|
-
args[_i] = arguments[_i];
|
|
84
|
-
}
|
|
85
|
-
return new ((_a = Intl.DateTimeFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
|
|
86
|
-
}, {
|
|
87
|
-
cache: createFastMemoizeCache(cache.dateTime),
|
|
88
|
-
strategy: strategies.variadic,
|
|
89
|
-
});
|
|
90
|
-
var getNumberFormat = memoize(function () {
|
|
91
|
-
var _a;
|
|
92
|
-
var args = [];
|
|
93
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
94
|
-
args[_i] = arguments[_i];
|
|
95
|
-
}
|
|
96
|
-
return new ((_a = Intl.NumberFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
|
|
97
|
-
}, {
|
|
98
|
-
cache: createFastMemoizeCache(cache.number),
|
|
99
|
-
strategy: strategies.variadic,
|
|
100
|
-
});
|
|
101
|
-
var getPluralRules = memoize(function () {
|
|
102
|
-
var _a;
|
|
103
|
-
var args = [];
|
|
104
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
105
|
-
args[_i] = arguments[_i];
|
|
106
|
-
}
|
|
107
|
-
return new ((_a = Intl.PluralRules).bind.apply(_a, __spreadArray([void 0], args, false)))();
|
|
108
|
-
}, {
|
|
109
|
-
cache: createFastMemoizeCache(cache.pluralRules),
|
|
110
|
-
strategy: strategies.variadic,
|
|
111
|
-
});
|
|
112
|
-
return {
|
|
113
|
-
getDateTimeFormat: getDateTimeFormat,
|
|
114
|
-
getNumberFormat: getNumberFormat,
|
|
115
|
-
getMessageFormat: memoize(function (message, locales, overrideFormats, opts) {
|
|
116
|
-
return new IntlMessageFormat(message, locales, overrideFormats, __assign({ formatters: {
|
|
117
|
-
getNumberFormat: getNumberFormat,
|
|
118
|
-
getDateTimeFormat: getDateTimeFormat,
|
|
119
|
-
getPluralRules: getPluralRules,
|
|
120
|
-
} }, (opts || {})));
|
|
121
|
-
}, {
|
|
122
|
-
cache: createFastMemoizeCache(cache.message),
|
|
123
|
-
strategy: strategies.variadic,
|
|
124
|
-
}),
|
|
125
|
-
getRelativeTimeFormat: memoize(function () {
|
|
126
|
-
var args = [];
|
|
127
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
128
|
-
args[_i] = arguments[_i];
|
|
129
|
-
}
|
|
130
|
-
return new (RelativeTimeFormat.bind.apply(RelativeTimeFormat, __spreadArray([void 0], args, false)))();
|
|
131
|
-
}, {
|
|
132
|
-
cache: createFastMemoizeCache(cache.relativeTime),
|
|
133
|
-
strategy: strategies.variadic,
|
|
134
|
-
}),
|
|
135
|
-
getPluralRules: getPluralRules,
|
|
136
|
-
getListFormat: memoize(function () {
|
|
137
|
-
var args = [];
|
|
138
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
139
|
-
args[_i] = arguments[_i];
|
|
140
|
-
}
|
|
141
|
-
return new (ListFormat.bind.apply(ListFormat, __spreadArray([void 0], args, false)))();
|
|
142
|
-
}, {
|
|
143
|
-
cache: createFastMemoizeCache(cache.list),
|
|
144
|
-
strategy: strategies.variadic,
|
|
145
|
-
}),
|
|
146
|
-
getDisplayNames: memoize(function () {
|
|
147
|
-
var args = [];
|
|
148
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
149
|
-
args[_i] = arguments[_i];
|
|
150
|
-
}
|
|
151
|
-
return new (DisplayNames.bind.apply(DisplayNames, __spreadArray([void 0], args, false)))();
|
|
152
|
-
}, {
|
|
153
|
-
cache: createFastMemoizeCache(cache.displayNames),
|
|
154
|
-
strategy: strategies.variadic,
|
|
155
|
-
}),
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
export function getNamedFormat(formats, type, name, onError) {
|
|
159
|
-
var formatType = formats && formats[type];
|
|
160
|
-
var format;
|
|
161
|
-
if (formatType) {
|
|
162
|
-
format = formatType[name];
|
|
163
|
-
}
|
|
164
|
-
if (format) {
|
|
165
|
-
return format;
|
|
166
|
-
}
|
|
167
|
-
onError(new UnsupportedFormatterError("No ".concat(type, " format named: ").concat(name)));
|
|
168
|
-
}
|