@formatjs/intl 4.0.7 → 4.0.9
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 +19 -15
- package/index.js +14 -13
- package/package.json +5 -5
- package/src/create-intl.d.ts +11 -7
- package/src/create-intl.js +62 -44
- package/src/dateTime.d.ts +32 -32
- package/src/dateTime.js +79 -110
- package/src/displayName.d.ts +5 -5
- package/src/displayName.js +22 -21
- package/src/error.d.ts +19 -19
- package/src/error.js +68 -91
- package/src/list.d.ts +7 -7
- package/src/list.js +47 -57
- package/src/message.d.ts +14 -14
- package/src/message.js +103 -96
- package/src/number.d.ts +14 -14
- package/src/number.js +46 -54
- package/src/plural.d.ts +5 -5
- package/src/plural.js +18 -18
- package/src/relativeTime.d.ts +5 -5
- package/src/relativeTime.js +26 -27
- package/src/types.d.ts +84 -80
- package/src/types.js +3 -1
- package/src/utils.d.ts +10 -7
- package/src/utils.js +112 -155
package/src/utils.js
CHANGED
|
@@ -1,168 +1,125 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { memoize, strategies } from
|
|
3
|
-
import { IntlMessageFormat } from
|
|
4
|
-
import { UnsupportedFormatterError } from
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
10
|
}
|
|
11
|
-
export function filterProps(props, allowlist, defaults) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return filtered;
|
|
21
|
-
}, {});
|
|
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
|
+
}, {});
|
|
22
20
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
+
}
|
|
28
26
|
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
+
}
|
|
34
32
|
};
|
|
35
|
-
export
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
|
44
42
|
};
|
|
45
43
|
export function createIntlCache() {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
44
|
+
return {
|
|
45
|
+
dateTime: {},
|
|
46
|
+
number: {},
|
|
47
|
+
message: {},
|
|
48
|
+
relativeTime: {},
|
|
49
|
+
pluralRules: {},
|
|
50
|
+
list: {},
|
|
51
|
+
displayNames: {}
|
|
52
|
+
};
|
|
55
53
|
}
|
|
56
54
|
function createFastMemoizeCache(store) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
},
|
|
68
|
-
};
|
|
55
|
+
return { create() {
|
|
56
|
+
return {
|
|
57
|
+
get(key) {
|
|
58
|
+
return store[key];
|
|
59
|
+
},
|
|
60
|
+
set(key, value) {
|
|
61
|
+
store[key] = value;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
} };
|
|
69
65
|
}
|
|
70
66
|
/**
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
export function createFormatters(cache) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
};
|
|
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
|
+
};
|
|
157
114
|
}
|
|
158
115
|
export function getNamedFormat(formats, type, name, onError) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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}`));
|
|
168
125
|
}
|