intl-messageformat 10.1.1 → 10.1.2
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/BUILD +126 -0
- package/CHANGELOG.md +1141 -0
- package/LICENSE.md +0 -0
- package/README.md +0 -0
- package/index.ts +11 -0
- package/package.json +5 -5
- package/src/core.ts +294 -0
- package/src/error.ts +65 -0
- package/src/formatters.ts +311 -0
- package/tests/benchmark.ts +103 -0
- package/tests/index.test.ts +968 -0
- package/tsconfig.json +5 -0
- package/index.d.ts +0 -6
- package/index.d.ts.map +0 -1
- package/index.js +0 -13
- package/intl-messageformat.esm.js +0 -3717
- package/intl-messageformat.iife.js +0 -3788
- package/lib/index.d.ts +0 -6
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -10
- package/lib/src/core.d.ts +0 -34
- package/lib/src/core.d.ts.map +0 -1
- package/lib/src/core.js +0 -240
- package/lib/src/error.d.ts +0 -28
- package/lib/src/error.d.ts.map +0 -1
- package/lib/src/error.js +0 -48
- package/lib/src/formatters.d.ts +0 -47
- package/lib/src/formatters.d.ts.map +0 -1
- package/lib/src/formatters.js +0 -177
- package/src/core.d.ts +0 -34
- package/src/core.d.ts.map +0 -1
- package/src/core.js +0 -243
- package/src/error.d.ts +0 -28
- package/src/error.d.ts.map +0 -1
- package/src/error.js +0 -51
- package/src/formatters.d.ts +0 -47
- package/src/formatters.d.ts.map +0 -1
- package/src/formatters.js +0 -182
package/src/core.js
DELETED
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
Copyright (c) 2014, Yahoo! Inc. All rights reserved.
|
|
4
|
-
Copyrights licensed under the New BSD License.
|
|
5
|
-
See the accompanying LICENSE file for terms.
|
|
6
|
-
*/
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.IntlMessageFormat = void 0;
|
|
9
|
-
var tslib_1 = require("tslib");
|
|
10
|
-
var icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser");
|
|
11
|
-
var fast_memoize_1 = (0, tslib_1.__importStar)(require("@formatjs/fast-memoize"));
|
|
12
|
-
var formatters_1 = require("./formatters");
|
|
13
|
-
// -- MessageFormat --------------------------------------------------------
|
|
14
|
-
function mergeConfig(c1, c2) {
|
|
15
|
-
if (!c2) {
|
|
16
|
-
return c1;
|
|
17
|
-
}
|
|
18
|
-
return (0, tslib_1.__assign)((0, tslib_1.__assign)((0, tslib_1.__assign)({}, (c1 || {})), (c2 || {})), Object.keys(c1).reduce(function (all, k) {
|
|
19
|
-
all[k] = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, c1[k]), (c2[k] || {}));
|
|
20
|
-
return all;
|
|
21
|
-
}, {}));
|
|
22
|
-
}
|
|
23
|
-
function mergeConfigs(defaultConfig, configs) {
|
|
24
|
-
if (!configs) {
|
|
25
|
-
return defaultConfig;
|
|
26
|
-
}
|
|
27
|
-
return Object.keys(defaultConfig).reduce(function (all, k) {
|
|
28
|
-
all[k] = mergeConfig(defaultConfig[k], configs[k]);
|
|
29
|
-
return all;
|
|
30
|
-
}, (0, tslib_1.__assign)({}, defaultConfig));
|
|
31
|
-
}
|
|
32
|
-
function createFastMemoizeCache(store) {
|
|
33
|
-
return {
|
|
34
|
-
create: function () {
|
|
35
|
-
return {
|
|
36
|
-
get: function (key) {
|
|
37
|
-
return store[key];
|
|
38
|
-
},
|
|
39
|
-
set: function (key, value) {
|
|
40
|
-
store[key] = value;
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
function createDefaultFormatters(cache) {
|
|
47
|
-
if (cache === void 0) { cache = {
|
|
48
|
-
number: {},
|
|
49
|
-
dateTime: {},
|
|
50
|
-
pluralRules: {},
|
|
51
|
-
}; }
|
|
52
|
-
return {
|
|
53
|
-
getNumberFormat: (0, fast_memoize_1.default)(function () {
|
|
54
|
-
var _a;
|
|
55
|
-
var args = [];
|
|
56
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
57
|
-
args[_i] = arguments[_i];
|
|
58
|
-
}
|
|
59
|
-
return new ((_a = Intl.NumberFormat).bind.apply(_a, (0, tslib_1.__spreadArray)([void 0], args, false)))();
|
|
60
|
-
}, {
|
|
61
|
-
cache: createFastMemoizeCache(cache.number),
|
|
62
|
-
strategy: fast_memoize_1.strategies.variadic,
|
|
63
|
-
}),
|
|
64
|
-
getDateTimeFormat: (0, fast_memoize_1.default)(function () {
|
|
65
|
-
var _a;
|
|
66
|
-
var args = [];
|
|
67
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
68
|
-
args[_i] = arguments[_i];
|
|
69
|
-
}
|
|
70
|
-
return new ((_a = Intl.DateTimeFormat).bind.apply(_a, (0, tslib_1.__spreadArray)([void 0], args, false)))();
|
|
71
|
-
}, {
|
|
72
|
-
cache: createFastMemoizeCache(cache.dateTime),
|
|
73
|
-
strategy: fast_memoize_1.strategies.variadic,
|
|
74
|
-
}),
|
|
75
|
-
getPluralRules: (0, fast_memoize_1.default)(function () {
|
|
76
|
-
var _a;
|
|
77
|
-
var args = [];
|
|
78
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
79
|
-
args[_i] = arguments[_i];
|
|
80
|
-
}
|
|
81
|
-
return new ((_a = Intl.PluralRules).bind.apply(_a, (0, tslib_1.__spreadArray)([void 0], args, false)))();
|
|
82
|
-
}, {
|
|
83
|
-
cache: createFastMemoizeCache(cache.pluralRules),
|
|
84
|
-
strategy: fast_memoize_1.strategies.variadic,
|
|
85
|
-
}),
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
var IntlMessageFormat = /** @class */ (function () {
|
|
89
|
-
function IntlMessageFormat(message, locales, overrideFormats, opts) {
|
|
90
|
-
var _this = this;
|
|
91
|
-
if (locales === void 0) { locales = IntlMessageFormat.defaultLocale; }
|
|
92
|
-
this.formatterCache = {
|
|
93
|
-
number: {},
|
|
94
|
-
dateTime: {},
|
|
95
|
-
pluralRules: {},
|
|
96
|
-
};
|
|
97
|
-
this.format = function (values) {
|
|
98
|
-
var parts = _this.formatToParts(values);
|
|
99
|
-
// Hot path for straight simple msg translations
|
|
100
|
-
if (parts.length === 1) {
|
|
101
|
-
return parts[0].value;
|
|
102
|
-
}
|
|
103
|
-
var result = parts.reduce(function (all, part) {
|
|
104
|
-
if (!all.length ||
|
|
105
|
-
part.type !== formatters_1.PART_TYPE.literal ||
|
|
106
|
-
typeof all[all.length - 1] !== 'string') {
|
|
107
|
-
all.push(part.value);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
all[all.length - 1] += part.value;
|
|
111
|
-
}
|
|
112
|
-
return all;
|
|
113
|
-
}, []);
|
|
114
|
-
if (result.length <= 1) {
|
|
115
|
-
return result[0] || '';
|
|
116
|
-
}
|
|
117
|
-
return result;
|
|
118
|
-
};
|
|
119
|
-
this.formatToParts = function (values) {
|
|
120
|
-
return (0, formatters_1.formatToParts)(_this.ast, _this.locales, _this.formatters, _this.formats, values, undefined, _this.message);
|
|
121
|
-
};
|
|
122
|
-
this.resolvedOptions = function () {
|
|
123
|
-
var _a;
|
|
124
|
-
return ({
|
|
125
|
-
locale: ((_a = _this.resolvedLocale) === null || _a === void 0 ? void 0 : _a.toString()) ||
|
|
126
|
-
Intl.NumberFormat.supportedLocalesOf(_this.locales)[0],
|
|
127
|
-
});
|
|
128
|
-
};
|
|
129
|
-
this.getAst = function () { return _this.ast; };
|
|
130
|
-
// Defined first because it's used to build the format pattern.
|
|
131
|
-
this.locales = locales;
|
|
132
|
-
this.resolvedLocale = IntlMessageFormat.resolveLocale(locales);
|
|
133
|
-
if (typeof message === 'string') {
|
|
134
|
-
this.message = message;
|
|
135
|
-
if (!IntlMessageFormat.__parse) {
|
|
136
|
-
throw new TypeError('IntlMessageFormat.__parse must be set to process `message` of type `string`');
|
|
137
|
-
}
|
|
138
|
-
// Parse string messages into an AST.
|
|
139
|
-
this.ast = IntlMessageFormat.__parse(message, {
|
|
140
|
-
ignoreTag: opts === null || opts === void 0 ? void 0 : opts.ignoreTag,
|
|
141
|
-
locale: this.resolvedLocale,
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
this.ast = message;
|
|
146
|
-
}
|
|
147
|
-
if (!Array.isArray(this.ast)) {
|
|
148
|
-
throw new TypeError('A message must be provided as a String or AST.');
|
|
149
|
-
}
|
|
150
|
-
// Creates a new object with the specified `formats` merged with the default
|
|
151
|
-
// formats.
|
|
152
|
-
this.formats = mergeConfigs(IntlMessageFormat.formats, overrideFormats);
|
|
153
|
-
this.formatters =
|
|
154
|
-
(opts && opts.formatters) || createDefaultFormatters(this.formatterCache);
|
|
155
|
-
}
|
|
156
|
-
Object.defineProperty(IntlMessageFormat, "defaultLocale", {
|
|
157
|
-
get: function () {
|
|
158
|
-
if (!IntlMessageFormat.memoizedDefaultLocale) {
|
|
159
|
-
IntlMessageFormat.memoizedDefaultLocale =
|
|
160
|
-
new Intl.NumberFormat().resolvedOptions().locale;
|
|
161
|
-
}
|
|
162
|
-
return IntlMessageFormat.memoizedDefaultLocale;
|
|
163
|
-
},
|
|
164
|
-
enumerable: false,
|
|
165
|
-
configurable: true
|
|
166
|
-
});
|
|
167
|
-
IntlMessageFormat.memoizedDefaultLocale = null;
|
|
168
|
-
IntlMessageFormat.resolveLocale = function (locales) {
|
|
169
|
-
if (typeof Intl.Locale === 'undefined') {
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
var supportedLocales = Intl.NumberFormat.supportedLocalesOf(locales);
|
|
173
|
-
if (supportedLocales.length > 0) {
|
|
174
|
-
return new Intl.Locale(supportedLocales[0]);
|
|
175
|
-
}
|
|
176
|
-
return new Intl.Locale(typeof locales === 'string' ? locales : locales[0]);
|
|
177
|
-
};
|
|
178
|
-
IntlMessageFormat.__parse = icu_messageformat_parser_1.parse;
|
|
179
|
-
// Default format options used as the prototype of the `formats` provided to the
|
|
180
|
-
// constructor. These are used when constructing the internal Intl.NumberFormat
|
|
181
|
-
// and Intl.DateTimeFormat instances.
|
|
182
|
-
IntlMessageFormat.formats = {
|
|
183
|
-
number: {
|
|
184
|
-
integer: {
|
|
185
|
-
maximumFractionDigits: 0,
|
|
186
|
-
},
|
|
187
|
-
currency: {
|
|
188
|
-
style: 'currency',
|
|
189
|
-
},
|
|
190
|
-
percent: {
|
|
191
|
-
style: 'percent',
|
|
192
|
-
},
|
|
193
|
-
},
|
|
194
|
-
date: {
|
|
195
|
-
short: {
|
|
196
|
-
month: 'numeric',
|
|
197
|
-
day: 'numeric',
|
|
198
|
-
year: '2-digit',
|
|
199
|
-
},
|
|
200
|
-
medium: {
|
|
201
|
-
month: 'short',
|
|
202
|
-
day: 'numeric',
|
|
203
|
-
year: 'numeric',
|
|
204
|
-
},
|
|
205
|
-
long: {
|
|
206
|
-
month: 'long',
|
|
207
|
-
day: 'numeric',
|
|
208
|
-
year: 'numeric',
|
|
209
|
-
},
|
|
210
|
-
full: {
|
|
211
|
-
weekday: 'long',
|
|
212
|
-
month: 'long',
|
|
213
|
-
day: 'numeric',
|
|
214
|
-
year: 'numeric',
|
|
215
|
-
},
|
|
216
|
-
},
|
|
217
|
-
time: {
|
|
218
|
-
short: {
|
|
219
|
-
hour: 'numeric',
|
|
220
|
-
minute: 'numeric',
|
|
221
|
-
},
|
|
222
|
-
medium: {
|
|
223
|
-
hour: 'numeric',
|
|
224
|
-
minute: 'numeric',
|
|
225
|
-
second: 'numeric',
|
|
226
|
-
},
|
|
227
|
-
long: {
|
|
228
|
-
hour: 'numeric',
|
|
229
|
-
minute: 'numeric',
|
|
230
|
-
second: 'numeric',
|
|
231
|
-
timeZoneName: 'short',
|
|
232
|
-
},
|
|
233
|
-
full: {
|
|
234
|
-
hour: 'numeric',
|
|
235
|
-
minute: 'numeric',
|
|
236
|
-
second: 'numeric',
|
|
237
|
-
timeZoneName: 'short',
|
|
238
|
-
},
|
|
239
|
-
},
|
|
240
|
-
};
|
|
241
|
-
return IntlMessageFormat;
|
|
242
|
-
}());
|
|
243
|
-
exports.IntlMessageFormat = IntlMessageFormat;
|
package/src/error.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export declare enum ErrorCode {
|
|
2
|
-
MISSING_VALUE = "MISSING_VALUE",
|
|
3
|
-
INVALID_VALUE = "INVALID_VALUE",
|
|
4
|
-
MISSING_INTL_API = "MISSING_INTL_API"
|
|
5
|
-
}
|
|
6
|
-
export declare class FormatError extends Error {
|
|
7
|
-
readonly code: ErrorCode;
|
|
8
|
-
/**
|
|
9
|
-
* Original message we're trying to format
|
|
10
|
-
* `undefined` if we're only dealing w/ AST
|
|
11
|
-
*
|
|
12
|
-
* @type {(string | undefined)}
|
|
13
|
-
* @memberof FormatError
|
|
14
|
-
*/
|
|
15
|
-
readonly originalMessage: string | undefined;
|
|
16
|
-
constructor(msg: string, code: ErrorCode, originalMessage?: string);
|
|
17
|
-
toString(): string;
|
|
18
|
-
}
|
|
19
|
-
export declare class InvalidValueError extends FormatError {
|
|
20
|
-
constructor(variableId: string, value: any, options: string[], originalMessage?: string);
|
|
21
|
-
}
|
|
22
|
-
export declare class InvalidValueTypeError extends FormatError {
|
|
23
|
-
constructor(value: any, type: string, originalMessage?: string);
|
|
24
|
-
}
|
|
25
|
-
export declare class MissingValueError extends FormatError {
|
|
26
|
-
constructor(variableId: string, originalMessage?: string);
|
|
27
|
-
}
|
|
28
|
-
//# sourceMappingURL=error.d.ts.map
|
package/src/error.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../../../../../packages/intl-messageformat/src/error.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS;IAEnB,aAAa,kBAAkB;IAE/B,aAAa,kBAAkB;IAE/B,gBAAgB,qBAAqB;CACtC;AAED,qBAAa,WAAY,SAAQ,KAAK;IACpC,SAAgB,IAAI,EAAE,SAAS,CAAA;IAC/B;;;;;;OAMG;IACH,SAAgB,eAAe,EAAE,MAAM,GAAG,SAAS,CAAA;gBACvC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,eAAe,CAAC,EAAE,MAAM;IAK3D,QAAQ;CAGhB;AAED,qBAAa,iBAAkB,SAAQ,WAAW;gBAE9C,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,GAAG,EACV,OAAO,EAAE,MAAM,EAAE,EACjB,eAAe,CAAC,EAAE,MAAM;CAU3B;AAED,qBAAa,qBAAsB,SAAQ,WAAW;gBACxC,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,eAAe,CAAC,EAAE,MAAM;CAO/D;AAED,qBAAa,iBAAkB,SAAQ,WAAW;gBACpC,UAAU,EAAE,MAAM,EAAE,eAAe,CAAC,EAAE,MAAM;CAOzD"}
|
package/src/error.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MissingValueError = exports.InvalidValueTypeError = exports.InvalidValueError = exports.FormatError = exports.ErrorCode = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
|
-
var ErrorCode;
|
|
6
|
-
(function (ErrorCode) {
|
|
7
|
-
// When we have a placeholder but no value to format
|
|
8
|
-
ErrorCode["MISSING_VALUE"] = "MISSING_VALUE";
|
|
9
|
-
// When value supplied is invalid
|
|
10
|
-
ErrorCode["INVALID_VALUE"] = "INVALID_VALUE";
|
|
11
|
-
// When we need specific Intl API but it's not available
|
|
12
|
-
ErrorCode["MISSING_INTL_API"] = "MISSING_INTL_API";
|
|
13
|
-
})(ErrorCode = exports.ErrorCode || (exports.ErrorCode = {}));
|
|
14
|
-
var FormatError = /** @class */ (function (_super) {
|
|
15
|
-
(0, tslib_1.__extends)(FormatError, _super);
|
|
16
|
-
function FormatError(msg, code, originalMessage) {
|
|
17
|
-
var _this = _super.call(this, msg) || this;
|
|
18
|
-
_this.code = code;
|
|
19
|
-
_this.originalMessage = originalMessage;
|
|
20
|
-
return _this;
|
|
21
|
-
}
|
|
22
|
-
FormatError.prototype.toString = function () {
|
|
23
|
-
return "[formatjs Error: ".concat(this.code, "] ").concat(this.message);
|
|
24
|
-
};
|
|
25
|
-
return FormatError;
|
|
26
|
-
}(Error));
|
|
27
|
-
exports.FormatError = FormatError;
|
|
28
|
-
var InvalidValueError = /** @class */ (function (_super) {
|
|
29
|
-
(0, tslib_1.__extends)(InvalidValueError, _super);
|
|
30
|
-
function InvalidValueError(variableId, value, options, originalMessage) {
|
|
31
|
-
return _super.call(this, "Invalid values for \"".concat(variableId, "\": \"").concat(value, "\". Options are \"").concat(Object.keys(options).join('", "'), "\""), ErrorCode.INVALID_VALUE, originalMessage) || this;
|
|
32
|
-
}
|
|
33
|
-
return InvalidValueError;
|
|
34
|
-
}(FormatError));
|
|
35
|
-
exports.InvalidValueError = InvalidValueError;
|
|
36
|
-
var InvalidValueTypeError = /** @class */ (function (_super) {
|
|
37
|
-
(0, tslib_1.__extends)(InvalidValueTypeError, _super);
|
|
38
|
-
function InvalidValueTypeError(value, type, originalMessage) {
|
|
39
|
-
return _super.call(this, "Value for \"".concat(value, "\" must be of type ").concat(type), ErrorCode.INVALID_VALUE, originalMessage) || this;
|
|
40
|
-
}
|
|
41
|
-
return InvalidValueTypeError;
|
|
42
|
-
}(FormatError));
|
|
43
|
-
exports.InvalidValueTypeError = InvalidValueTypeError;
|
|
44
|
-
var MissingValueError = /** @class */ (function (_super) {
|
|
45
|
-
(0, tslib_1.__extends)(MissingValueError, _super);
|
|
46
|
-
function MissingValueError(variableId, originalMessage) {
|
|
47
|
-
return _super.call(this, "The intl string context variable \"".concat(variableId, "\" was not provided to the string \"").concat(originalMessage, "\""), ErrorCode.MISSING_VALUE, originalMessage) || this;
|
|
48
|
-
}
|
|
49
|
-
return MissingValueError;
|
|
50
|
-
}(FormatError));
|
|
51
|
-
exports.MissingValueError = MissingValueError;
|
package/src/formatters.d.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { NumberFormatOptions } from '@formatjs/ecma402-abstract';
|
|
2
|
-
import { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
|
|
3
|
-
declare global {
|
|
4
|
-
namespace FormatjsIntl {
|
|
5
|
-
interface Message {
|
|
6
|
-
}
|
|
7
|
-
interface IntlConfig {
|
|
8
|
-
}
|
|
9
|
-
interface Formats {
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
declare type Format<Source = string> = Source extends keyof FormatjsIntl.Formats ? FormatjsIntl.Formats[Source] : string;
|
|
14
|
-
export interface Formats {
|
|
15
|
-
number: Record<Format<'number'>, NumberFormatOptions>;
|
|
16
|
-
date: Record<Format<'date'>, Intl.DateTimeFormatOptions>;
|
|
17
|
-
time: Record<Format<'time'>, Intl.DateTimeFormatOptions>;
|
|
18
|
-
}
|
|
19
|
-
export interface FormatterCache {
|
|
20
|
-
number: Record<string, NumberFormatOptions>;
|
|
21
|
-
dateTime: Record<string, Intl.DateTimeFormat>;
|
|
22
|
-
pluralRules: Record<string, Intl.PluralRules>;
|
|
23
|
-
}
|
|
24
|
-
export interface Formatters {
|
|
25
|
-
getNumberFormat(locals?: string | string[], opts?: NumberFormatOptions): Intl.NumberFormat;
|
|
26
|
-
getDateTimeFormat(...args: ConstructorParameters<typeof Intl.DateTimeFormat>): Intl.DateTimeFormat;
|
|
27
|
-
getPluralRules(...args: ConstructorParameters<typeof Intl.PluralRules>): Intl.PluralRules;
|
|
28
|
-
}
|
|
29
|
-
export declare enum PART_TYPE {
|
|
30
|
-
literal = 0,
|
|
31
|
-
object = 1
|
|
32
|
-
}
|
|
33
|
-
export interface LiteralPart {
|
|
34
|
-
type: PART_TYPE.literal;
|
|
35
|
-
value: string;
|
|
36
|
-
}
|
|
37
|
-
export interface ObjectPart<T = any> {
|
|
38
|
-
type: PART_TYPE.object;
|
|
39
|
-
value: T;
|
|
40
|
-
}
|
|
41
|
-
export declare type MessageFormatPart<T> = LiteralPart | ObjectPart<T>;
|
|
42
|
-
export declare type PrimitiveType = string | number | boolean | null | undefined | Date;
|
|
43
|
-
export declare function isFormatXMLElementFn<T>(el: PrimitiveType | T | FormatXMLElementFn<T>): el is FormatXMLElementFn<T>;
|
|
44
|
-
export declare function formatToParts<T>(els: MessageFormatElement[], locales: string | string[], formatters: Formatters, formats: Formats, values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>, currentPluralValue?: number, originalMessage?: string): MessageFormatPart<T>[];
|
|
45
|
-
export declare type FormatXMLElementFn<T, R = string | T | Array<string | T>> = (parts: Array<string | T>) => R;
|
|
46
|
-
export {};
|
|
47
|
-
//# sourceMappingURL=formatters.d.ts.map
|
package/src/formatters.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../../../../../../packages/intl-messageformat/src/formatters.ts"],"names":[],"mappings":"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"}
|
package/src/formatters.js
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatToParts = exports.isFormatXMLElementFn = exports.PART_TYPE = void 0;
|
|
4
|
-
var icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser");
|
|
5
|
-
var error_1 = require("./error");
|
|
6
|
-
var PART_TYPE;
|
|
7
|
-
(function (PART_TYPE) {
|
|
8
|
-
PART_TYPE[PART_TYPE["literal"] = 0] = "literal";
|
|
9
|
-
PART_TYPE[PART_TYPE["object"] = 1] = "object";
|
|
10
|
-
})(PART_TYPE = exports.PART_TYPE || (exports.PART_TYPE = {}));
|
|
11
|
-
function mergeLiteral(parts) {
|
|
12
|
-
if (parts.length < 2) {
|
|
13
|
-
return parts;
|
|
14
|
-
}
|
|
15
|
-
return parts.reduce(function (all, part) {
|
|
16
|
-
var lastPart = all[all.length - 1];
|
|
17
|
-
if (!lastPart ||
|
|
18
|
-
lastPart.type !== PART_TYPE.literal ||
|
|
19
|
-
part.type !== PART_TYPE.literal) {
|
|
20
|
-
all.push(part);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
lastPart.value += part.value;
|
|
24
|
-
}
|
|
25
|
-
return all;
|
|
26
|
-
}, []);
|
|
27
|
-
}
|
|
28
|
-
function isFormatXMLElementFn(el) {
|
|
29
|
-
return typeof el === 'function';
|
|
30
|
-
}
|
|
31
|
-
exports.isFormatXMLElementFn = isFormatXMLElementFn;
|
|
32
|
-
// TODO(skeleton): add skeleton support
|
|
33
|
-
function formatToParts(els, locales, formatters, formats, values, currentPluralValue,
|
|
34
|
-
// For debugging
|
|
35
|
-
originalMessage) {
|
|
36
|
-
// Hot path for straight simple msg translations
|
|
37
|
-
if (els.length === 1 && (0, icu_messageformat_parser_1.isLiteralElement)(els[0])) {
|
|
38
|
-
return [
|
|
39
|
-
{
|
|
40
|
-
type: PART_TYPE.literal,
|
|
41
|
-
value: els[0].value,
|
|
42
|
-
},
|
|
43
|
-
];
|
|
44
|
-
}
|
|
45
|
-
var result = [];
|
|
46
|
-
for (var _i = 0, els_1 = els; _i < els_1.length; _i++) {
|
|
47
|
-
var el = els_1[_i];
|
|
48
|
-
// Exit early for string parts.
|
|
49
|
-
if ((0, icu_messageformat_parser_1.isLiteralElement)(el)) {
|
|
50
|
-
result.push({
|
|
51
|
-
type: PART_TYPE.literal,
|
|
52
|
-
value: el.value,
|
|
53
|
-
});
|
|
54
|
-
continue;
|
|
55
|
-
}
|
|
56
|
-
// TODO: should this part be literal type?
|
|
57
|
-
// Replace `#` in plural rules with the actual numeric value.
|
|
58
|
-
if ((0, icu_messageformat_parser_1.isPoundElement)(el)) {
|
|
59
|
-
if (typeof currentPluralValue === 'number') {
|
|
60
|
-
result.push({
|
|
61
|
-
type: PART_TYPE.literal,
|
|
62
|
-
value: formatters.getNumberFormat(locales).format(currentPluralValue),
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
var varName = el.value;
|
|
68
|
-
// Enforce that all required values are provided by the caller.
|
|
69
|
-
if (!(values && varName in values)) {
|
|
70
|
-
throw new error_1.MissingValueError(varName, originalMessage);
|
|
71
|
-
}
|
|
72
|
-
var value = values[varName];
|
|
73
|
-
if ((0, icu_messageformat_parser_1.isArgumentElement)(el)) {
|
|
74
|
-
if (!value || typeof value === 'string' || typeof value === 'number') {
|
|
75
|
-
value =
|
|
76
|
-
typeof value === 'string' || typeof value === 'number'
|
|
77
|
-
? String(value)
|
|
78
|
-
: '';
|
|
79
|
-
}
|
|
80
|
-
result.push({
|
|
81
|
-
type: typeof value === 'string' ? PART_TYPE.literal : PART_TYPE.object,
|
|
82
|
-
value: value,
|
|
83
|
-
});
|
|
84
|
-
continue;
|
|
85
|
-
}
|
|
86
|
-
// Recursively format plural and select parts' option — which can be a
|
|
87
|
-
// nested pattern structure. The choosing of the option to use is
|
|
88
|
-
// abstracted-by and delegated-to the part helper object.
|
|
89
|
-
if ((0, icu_messageformat_parser_1.isDateElement)(el)) {
|
|
90
|
-
var style = typeof el.style === 'string'
|
|
91
|
-
? formats.date[el.style]
|
|
92
|
-
: (0, icu_messageformat_parser_1.isDateTimeSkeleton)(el.style)
|
|
93
|
-
? el.style.parsedOptions
|
|
94
|
-
: undefined;
|
|
95
|
-
result.push({
|
|
96
|
-
type: PART_TYPE.literal,
|
|
97
|
-
value: formatters
|
|
98
|
-
.getDateTimeFormat(locales, style)
|
|
99
|
-
.format(value),
|
|
100
|
-
});
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
if ((0, icu_messageformat_parser_1.isTimeElement)(el)) {
|
|
104
|
-
var style = typeof el.style === 'string'
|
|
105
|
-
? formats.time[el.style]
|
|
106
|
-
: (0, icu_messageformat_parser_1.isDateTimeSkeleton)(el.style)
|
|
107
|
-
? el.style.parsedOptions
|
|
108
|
-
: formats.time.medium;
|
|
109
|
-
result.push({
|
|
110
|
-
type: PART_TYPE.literal,
|
|
111
|
-
value: formatters
|
|
112
|
-
.getDateTimeFormat(locales, style)
|
|
113
|
-
.format(value),
|
|
114
|
-
});
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
if ((0, icu_messageformat_parser_1.isNumberElement)(el)) {
|
|
118
|
-
var style = typeof el.style === 'string'
|
|
119
|
-
? formats.number[el.style]
|
|
120
|
-
: (0, icu_messageformat_parser_1.isNumberSkeleton)(el.style)
|
|
121
|
-
? el.style.parsedOptions
|
|
122
|
-
: undefined;
|
|
123
|
-
if (style && style.scale) {
|
|
124
|
-
value =
|
|
125
|
-
value *
|
|
126
|
-
(style.scale || 1);
|
|
127
|
-
}
|
|
128
|
-
result.push({
|
|
129
|
-
type: PART_TYPE.literal,
|
|
130
|
-
value: formatters
|
|
131
|
-
.getNumberFormat(locales, style)
|
|
132
|
-
.format(value),
|
|
133
|
-
});
|
|
134
|
-
continue;
|
|
135
|
-
}
|
|
136
|
-
if ((0, icu_messageformat_parser_1.isTagElement)(el)) {
|
|
137
|
-
var children = el.children, value_1 = el.value;
|
|
138
|
-
var formatFn = values[value_1];
|
|
139
|
-
if (!isFormatXMLElementFn(formatFn)) {
|
|
140
|
-
throw new error_1.InvalidValueTypeError(value_1, 'function', originalMessage);
|
|
141
|
-
}
|
|
142
|
-
var parts = formatToParts(children, locales, formatters, formats, values, currentPluralValue);
|
|
143
|
-
var chunks = formatFn(parts.map(function (p) { return p.value; }));
|
|
144
|
-
if (!Array.isArray(chunks)) {
|
|
145
|
-
chunks = [chunks];
|
|
146
|
-
}
|
|
147
|
-
result.push.apply(result, chunks.map(function (c) {
|
|
148
|
-
return {
|
|
149
|
-
type: typeof c === 'string' ? PART_TYPE.literal : PART_TYPE.object,
|
|
150
|
-
value: c,
|
|
151
|
-
};
|
|
152
|
-
}));
|
|
153
|
-
}
|
|
154
|
-
if ((0, icu_messageformat_parser_1.isSelectElement)(el)) {
|
|
155
|
-
var opt = el.options[value] || el.options.other;
|
|
156
|
-
if (!opt) {
|
|
157
|
-
throw new error_1.InvalidValueError(el.value, value, Object.keys(el.options), originalMessage);
|
|
158
|
-
}
|
|
159
|
-
result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values));
|
|
160
|
-
continue;
|
|
161
|
-
}
|
|
162
|
-
if ((0, icu_messageformat_parser_1.isPluralElement)(el)) {
|
|
163
|
-
var opt = el.options["=".concat(value)];
|
|
164
|
-
if (!opt) {
|
|
165
|
-
if (!Intl.PluralRules) {
|
|
166
|
-
throw new error_1.FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", error_1.ErrorCode.MISSING_INTL_API, originalMessage);
|
|
167
|
-
}
|
|
168
|
-
var rule = formatters
|
|
169
|
-
.getPluralRules(locales, { type: el.pluralType })
|
|
170
|
-
.select(value - (el.offset || 0));
|
|
171
|
-
opt = el.options[rule] || el.options.other;
|
|
172
|
-
}
|
|
173
|
-
if (!opt) {
|
|
174
|
-
throw new error_1.InvalidValueError(el.value, value, Object.keys(el.options), originalMessage);
|
|
175
|
-
}
|
|
176
|
-
result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values, value - (el.offset || 0)));
|
|
177
|
-
continue;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
return mergeLiteral(result);
|
|
181
|
-
}
|
|
182
|
-
exports.formatToParts = formatToParts;
|