intl-messageformat 10.5.4 → 10.5.5

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/lib/index.js CHANGED
@@ -1,10 +1,13 @@
1
+ "use strict";
1
2
  /*
2
3
  Copyright (c) 2014, Yahoo! Inc. All rights reserved.
3
4
  Copyrights licensed under the New BSD License.
4
5
  See the accompanying LICENSE file for terms.
5
6
  */
6
- import { IntlMessageFormat } from './src/core';
7
- export * from './src/formatters';
8
- export * from './src/core';
9
- export * from './src/error';
10
- export default IntlMessageFormat;
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ var tslib_1 = require("tslib");
9
+ var core_1 = require("./src/core");
10
+ tslib_1.__exportStar(require("./src/formatters"), exports);
11
+ tslib_1.__exportStar(require("./src/core"), exports);
12
+ tslib_1.__exportStar(require("./src/error"), exports);
13
+ exports.default = core_1.IntlMessageFormat;
package/lib/src/core.js CHANGED
@@ -1,19 +1,22 @@
1
+ "use strict";
1
2
  /*
2
3
  Copyright (c) 2014, Yahoo! Inc. All rights reserved.
3
4
  Copyrights licensed under the New BSD License.
4
5
  See the accompanying LICENSE file for terms.
5
6
  */
6
- import { __assign, __rest, __spreadArray } from "tslib";
7
- import { parse, } from '@formatjs/icu-messageformat-parser';
8
- import { memoize, strategies } from '@formatjs/fast-memoize';
9
- import { formatToParts, PART_TYPE, } from './formatters';
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 = require("@formatjs/fast-memoize");
12
+ var formatters_1 = require("./formatters");
10
13
  // -- MessageFormat --------------------------------------------------------
11
14
  function mergeConfig(c1, c2) {
12
15
  if (!c2) {
13
16
  return c1;
14
17
  }
15
- return __assign(__assign(__assign({}, (c1 || {})), (c2 || {})), Object.keys(c1).reduce(function (all, k) {
16
- all[k] = __assign(__assign({}, c1[k]), (c2[k] || {}));
18
+ return tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, (c1 || {})), (c2 || {})), Object.keys(c1).reduce(function (all, k) {
19
+ all[k] = tslib_1.__assign(tslib_1.__assign({}, c1[k]), (c2[k] || {}));
17
20
  return all;
18
21
  }, {}));
19
22
  }
@@ -24,7 +27,7 @@ function mergeConfigs(defaultConfig, configs) {
24
27
  return Object.keys(defaultConfig).reduce(function (all, k) {
25
28
  all[k] = mergeConfig(defaultConfig[k], configs[k]);
26
29
  return all;
27
- }, __assign({}, defaultConfig));
30
+ }, tslib_1.__assign({}, defaultConfig));
28
31
  }
29
32
  function createFastMemoizeCache(store) {
30
33
  return {
@@ -47,45 +50,45 @@ function createDefaultFormatters(cache) {
47
50
  pluralRules: {},
48
51
  }; }
49
52
  return {
50
- getNumberFormat: memoize(function () {
53
+ getNumberFormat: (0, fast_memoize_1.memoize)(function () {
51
54
  var _a;
52
55
  var args = [];
53
56
  for (var _i = 0; _i < arguments.length; _i++) {
54
57
  args[_i] = arguments[_i];
55
58
  }
56
- return new ((_a = Intl.NumberFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
59
+ return new ((_a = Intl.NumberFormat).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))();
57
60
  }, {
58
61
  cache: createFastMemoizeCache(cache.number),
59
- strategy: strategies.variadic,
62
+ strategy: fast_memoize_1.strategies.variadic,
60
63
  }),
61
- getDateTimeFormat: memoize(function () {
64
+ getDateTimeFormat: (0, fast_memoize_1.memoize)(function () {
62
65
  var _a;
63
66
  var args = [];
64
67
  for (var _i = 0; _i < arguments.length; _i++) {
65
68
  args[_i] = arguments[_i];
66
69
  }
67
- return new ((_a = Intl.DateTimeFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
70
+ return new ((_a = Intl.DateTimeFormat).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))();
68
71
  }, {
69
72
  cache: createFastMemoizeCache(cache.dateTime),
70
- strategy: strategies.variadic,
73
+ strategy: fast_memoize_1.strategies.variadic,
71
74
  }),
72
- getPluralRules: memoize(function () {
75
+ getPluralRules: (0, fast_memoize_1.memoize)(function () {
73
76
  var _a;
74
77
  var args = [];
75
78
  for (var _i = 0; _i < arguments.length; _i++) {
76
79
  args[_i] = arguments[_i];
77
80
  }
78
- return new ((_a = Intl.PluralRules).bind.apply(_a, __spreadArray([void 0], args, false)))();
81
+ return new ((_a = Intl.PluralRules).bind.apply(_a, tslib_1.__spreadArray([void 0], args, false)))();
79
82
  }, {
80
83
  cache: createFastMemoizeCache(cache.pluralRules),
81
- strategy: strategies.variadic,
84
+ strategy: fast_memoize_1.strategies.variadic,
82
85
  }),
83
86
  };
84
87
  }
85
- export var IntlMessageFormat = /** @class */ (function () {
88
+ var IntlMessageFormat = /** @class */ (function () {
86
89
  function IntlMessageFormat(message, locales, overrideFormats, opts) {
87
- if (locales === void 0) { locales = IntlMessageFormat.defaultLocale; }
88
90
  var _this = this;
91
+ if (locales === void 0) { locales = IntlMessageFormat.defaultLocale; }
89
92
  this.formatterCache = {
90
93
  number: {},
91
94
  dateTime: {},
@@ -99,7 +102,7 @@ export var IntlMessageFormat = /** @class */ (function () {
99
102
  }
100
103
  var result = parts.reduce(function (all, part) {
101
104
  if (!all.length ||
102
- part.type !== PART_TYPE.literal ||
105
+ part.type !== formatters_1.PART_TYPE.literal ||
103
106
  typeof all[all.length - 1] !== 'string') {
104
107
  all.push(part.value);
105
108
  }
@@ -114,7 +117,7 @@ export var IntlMessageFormat = /** @class */ (function () {
114
117
  return result;
115
118
  };
116
119
  this.formatToParts = function (values) {
117
- return formatToParts(_this.ast, _this.locales, _this.formatters, _this.formats, values, undefined, _this.message);
120
+ return (0, formatters_1.formatToParts)(_this.ast, _this.locales, _this.formatters, _this.formats, values, undefined, _this.message);
118
121
  };
119
122
  this.resolvedOptions = function () {
120
123
  var _a;
@@ -132,9 +135,9 @@ export var IntlMessageFormat = /** @class */ (function () {
132
135
  if (!IntlMessageFormat.__parse) {
133
136
  throw new TypeError('IntlMessageFormat.__parse must be set to process `message` of type `string`');
134
137
  }
135
- var _a = opts || {}, formatters = _a.formatters, parseOpts = __rest(_a, ["formatters"]);
138
+ var _a = opts || {}, formatters = _a.formatters, parseOpts = tslib_1.__rest(_a, ["formatters"]);
136
139
  // Parse string messages into an AST.
137
- this.ast = IntlMessageFormat.__parse(message, __assign(__assign({}, parseOpts), { locale: this.resolvedLocale }));
140
+ this.ast = IntlMessageFormat.__parse(message, tslib_1.__assign(tslib_1.__assign({}, parseOpts), { locale: this.resolvedLocale }));
138
141
  }
139
142
  else {
140
143
  this.ast = message;
@@ -170,7 +173,7 @@ export var IntlMessageFormat = /** @class */ (function () {
170
173
  }
171
174
  return new Intl.Locale(typeof locales === 'string' ? locales : locales[0]);
172
175
  };
173
- IntlMessageFormat.__parse = parse;
176
+ IntlMessageFormat.__parse = icu_messageformat_parser_1.parse;
174
177
  // Default format options used as the prototype of the `formats` provided to the
175
178
  // constructor. These are used when constructing the internal Intl.NumberFormat
176
179
  // and Intl.DateTimeFormat instances.
@@ -235,3 +238,4 @@ export var IntlMessageFormat = /** @class */ (function () {
235
238
  };
236
239
  return IntlMessageFormat;
237
240
  }());
241
+ exports.IntlMessageFormat = IntlMessageFormat;
package/lib/src/error.js CHANGED
@@ -1,5 +1,8 @@
1
- import { __extends } from "tslib";
2
- export var ErrorCode;
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;
3
6
  (function (ErrorCode) {
4
7
  // When we have a placeholder but no value to format
5
8
  ErrorCode["MISSING_VALUE"] = "MISSING_VALUE";
@@ -7,9 +10,9 @@ export var ErrorCode;
7
10
  ErrorCode["INVALID_VALUE"] = "INVALID_VALUE";
8
11
  // When we need specific Intl API but it's not available
9
12
  ErrorCode["MISSING_INTL_API"] = "MISSING_INTL_API";
10
- })(ErrorCode || (ErrorCode = {}));
13
+ })(ErrorCode || (exports.ErrorCode = ErrorCode = {}));
11
14
  var FormatError = /** @class */ (function (_super) {
12
- __extends(FormatError, _super);
15
+ tslib_1.__extends(FormatError, _super);
13
16
  function FormatError(msg, code, originalMessage) {
14
17
  var _this = _super.call(this, msg) || this;
15
18
  _this.code = code;
@@ -21,28 +24,28 @@ var FormatError = /** @class */ (function (_super) {
21
24
  };
22
25
  return FormatError;
23
26
  }(Error));
24
- export { FormatError };
27
+ exports.FormatError = FormatError;
25
28
  var InvalidValueError = /** @class */ (function (_super) {
26
- __extends(InvalidValueError, _super);
29
+ tslib_1.__extends(InvalidValueError, _super);
27
30
  function InvalidValueError(variableId, value, options, originalMessage) {
28
31
  return _super.call(this, "Invalid values for \"".concat(variableId, "\": \"").concat(value, "\". Options are \"").concat(Object.keys(options).join('", "'), "\""), ErrorCode.INVALID_VALUE, originalMessage) || this;
29
32
  }
30
33
  return InvalidValueError;
31
34
  }(FormatError));
32
- export { InvalidValueError };
35
+ exports.InvalidValueError = InvalidValueError;
33
36
  var InvalidValueTypeError = /** @class */ (function (_super) {
34
- __extends(InvalidValueTypeError, _super);
37
+ tslib_1.__extends(InvalidValueTypeError, _super);
35
38
  function InvalidValueTypeError(value, type, originalMessage) {
36
39
  return _super.call(this, "Value for \"".concat(value, "\" must be of type ").concat(type), ErrorCode.INVALID_VALUE, originalMessage) || this;
37
40
  }
38
41
  return InvalidValueTypeError;
39
42
  }(FormatError));
40
- export { InvalidValueTypeError };
43
+ exports.InvalidValueTypeError = InvalidValueTypeError;
41
44
  var MissingValueError = /** @class */ (function (_super) {
42
- __extends(MissingValueError, _super);
45
+ tslib_1.__extends(MissingValueError, _super);
43
46
  function MissingValueError(variableId, originalMessage) {
44
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;
45
48
  }
46
49
  return MissingValueError;
47
50
  }(FormatError));
48
- export { MissingValueError };
51
+ exports.MissingValueError = MissingValueError;
@@ -1,10 +1,13 @@
1
- import { isArgumentElement, isDateElement, isDateTimeSkeleton, isLiteralElement, isNumberElement, isNumberSkeleton, isPluralElement, isPoundElement, isSelectElement, isTimeElement, isTagElement, } from '@formatjs/icu-messageformat-parser';
2
- import { MissingValueError, InvalidValueError, ErrorCode, FormatError, InvalidValueTypeError, } from './error';
3
- export var PART_TYPE;
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;
4
7
  (function (PART_TYPE) {
5
8
  PART_TYPE[PART_TYPE["literal"] = 0] = "literal";
6
9
  PART_TYPE[PART_TYPE["object"] = 1] = "object";
7
- })(PART_TYPE || (PART_TYPE = {}));
10
+ })(PART_TYPE || (exports.PART_TYPE = PART_TYPE = {}));
8
11
  function mergeLiteral(parts) {
9
12
  if (parts.length < 2) {
10
13
  return parts;
@@ -22,15 +25,16 @@ function mergeLiteral(parts) {
22
25
  return all;
23
26
  }, []);
24
27
  }
25
- export function isFormatXMLElementFn(el) {
28
+ function isFormatXMLElementFn(el) {
26
29
  return typeof el === 'function';
27
30
  }
31
+ exports.isFormatXMLElementFn = isFormatXMLElementFn;
28
32
  // TODO(skeleton): add skeleton support
29
- export function formatToParts(els, locales, formatters, formats, values, currentPluralValue,
33
+ function formatToParts(els, locales, formatters, formats, values, currentPluralValue,
30
34
  // For debugging
31
35
  originalMessage) {
32
36
  // Hot path for straight simple msg translations
33
- if (els.length === 1 && isLiteralElement(els[0])) {
37
+ if (els.length === 1 && (0, icu_messageformat_parser_1.isLiteralElement)(els[0])) {
34
38
  return [
35
39
  {
36
40
  type: PART_TYPE.literal,
@@ -42,7 +46,7 @@ originalMessage) {
42
46
  for (var _i = 0, els_1 = els; _i < els_1.length; _i++) {
43
47
  var el = els_1[_i];
44
48
  // Exit early for string parts.
45
- if (isLiteralElement(el)) {
49
+ if ((0, icu_messageformat_parser_1.isLiteralElement)(el)) {
46
50
  result.push({
47
51
  type: PART_TYPE.literal,
48
52
  value: el.value,
@@ -51,7 +55,7 @@ originalMessage) {
51
55
  }
52
56
  // TODO: should this part be literal type?
53
57
  // Replace `#` in plural rules with the actual numeric value.
54
- if (isPoundElement(el)) {
58
+ if ((0, icu_messageformat_parser_1.isPoundElement)(el)) {
55
59
  if (typeof currentPluralValue === 'number') {
56
60
  result.push({
57
61
  type: PART_TYPE.literal,
@@ -63,10 +67,10 @@ originalMessage) {
63
67
  var varName = el.value;
64
68
  // Enforce that all required values are provided by the caller.
65
69
  if (!(values && varName in values)) {
66
- throw new MissingValueError(varName, originalMessage);
70
+ throw new error_1.MissingValueError(varName, originalMessage);
67
71
  }
68
72
  var value = values[varName];
69
- if (isArgumentElement(el)) {
73
+ if ((0, icu_messageformat_parser_1.isArgumentElement)(el)) {
70
74
  if (!value || typeof value === 'string' || typeof value === 'number') {
71
75
  value =
72
76
  typeof value === 'string' || typeof value === 'number'
@@ -82,10 +86,10 @@ originalMessage) {
82
86
  // Recursively format plural and select parts' option — which can be a
83
87
  // nested pattern structure. The choosing of the option to use is
84
88
  // abstracted-by and delegated-to the part helper object.
85
- if (isDateElement(el)) {
89
+ if ((0, icu_messageformat_parser_1.isDateElement)(el)) {
86
90
  var style = typeof el.style === 'string'
87
91
  ? formats.date[el.style]
88
- : isDateTimeSkeleton(el.style)
92
+ : (0, icu_messageformat_parser_1.isDateTimeSkeleton)(el.style)
89
93
  ? el.style.parsedOptions
90
94
  : undefined;
91
95
  result.push({
@@ -96,10 +100,10 @@ originalMessage) {
96
100
  });
97
101
  continue;
98
102
  }
99
- if (isTimeElement(el)) {
103
+ if ((0, icu_messageformat_parser_1.isTimeElement)(el)) {
100
104
  var style = typeof el.style === 'string'
101
105
  ? formats.time[el.style]
102
- : isDateTimeSkeleton(el.style)
106
+ : (0, icu_messageformat_parser_1.isDateTimeSkeleton)(el.style)
103
107
  ? el.style.parsedOptions
104
108
  : formats.time.medium;
105
109
  result.push({
@@ -110,10 +114,10 @@ originalMessage) {
110
114
  });
111
115
  continue;
112
116
  }
113
- if (isNumberElement(el)) {
117
+ if ((0, icu_messageformat_parser_1.isNumberElement)(el)) {
114
118
  var style = typeof el.style === 'string'
115
119
  ? formats.number[el.style]
116
- : isNumberSkeleton(el.style)
120
+ : (0, icu_messageformat_parser_1.isNumberSkeleton)(el.style)
117
121
  ? el.style.parsedOptions
118
122
  : undefined;
119
123
  if (style && style.scale) {
@@ -129,11 +133,11 @@ originalMessage) {
129
133
  });
130
134
  continue;
131
135
  }
132
- if (isTagElement(el)) {
136
+ if ((0, icu_messageformat_parser_1.isTagElement)(el)) {
133
137
  var children = el.children, value_1 = el.value;
134
138
  var formatFn = values[value_1];
135
139
  if (!isFormatXMLElementFn(formatFn)) {
136
- throw new InvalidValueTypeError(value_1, 'function', originalMessage);
140
+ throw new error_1.InvalidValueTypeError(value_1, 'function', originalMessage);
137
141
  }
138
142
  var parts = formatToParts(children, locales, formatters, formats, values, currentPluralValue);
139
143
  var chunks = formatFn(parts.map(function (p) { return p.value; }));
@@ -147,19 +151,19 @@ originalMessage) {
147
151
  };
148
152
  }));
149
153
  }
150
- if (isSelectElement(el)) {
154
+ if ((0, icu_messageformat_parser_1.isSelectElement)(el)) {
151
155
  var opt = el.options[value] || el.options.other;
152
156
  if (!opt) {
153
- throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage);
157
+ throw new error_1.InvalidValueError(el.value, value, Object.keys(el.options), originalMessage);
154
158
  }
155
159
  result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values));
156
160
  continue;
157
161
  }
158
- if (isPluralElement(el)) {
162
+ if ((0, icu_messageformat_parser_1.isPluralElement)(el)) {
159
163
  var opt = el.options["=".concat(value)];
160
164
  if (!opt) {
161
165
  if (!Intl.PluralRules) {
162
- throw new FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", ErrorCode.MISSING_INTL_API, originalMessage);
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);
163
167
  }
164
168
  var rule = formatters
165
169
  .getPluralRules(locales, { type: el.pluralType })
@@ -167,7 +171,7 @@ originalMessage) {
167
171
  opt = el.options[rule] || el.options.other;
168
172
  }
169
173
  if (!opt) {
170
- throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage);
174
+ throw new error_1.InvalidValueError(el.value, value, Object.keys(el.options), originalMessage);
171
175
  }
172
176
  result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values, value - (el.offset || 0)));
173
177
  continue;
@@ -175,3 +179,4 @@ originalMessage) {
175
179
  }
176
180
  return mergeLiteral(result);
177
181
  }
182
+ exports.formatToParts = formatToParts;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "intl-messageformat",
3
- "version": "10.5.4",
3
+ "version": "10.5.5",
4
4
  "description": "Formats ICU Message strings with number, date, plural, and select placeholders to create localized messages.",
5
5
  "keywords": [
6
6
  "i18n",
@@ -32,9 +32,9 @@
32
32
  "types": "index.d.ts",
33
33
  "dependencies": {
34
34
  "tslib": "^2.4.0",
35
- "@formatjs/ecma402-abstract": "1.17.2",
36
- "@formatjs/icu-messageformat-parser": "2.7.0",
37
- "@formatjs/fast-memoize": "2.2.0"
35
+ "@formatjs/ecma402-abstract": "1.17.3",
36
+ "@formatjs/fast-memoize": "2.2.0",
37
+ "@formatjs/icu-messageformat-parser": "2.7.1"
38
38
  },
39
39
  "sideEffects": false,
40
40
  "homepage": "https://github.com/formatjs/formatjs",
package/src/core.js CHANGED
@@ -85,10 +85,10 @@ function createDefaultFormatters(cache) {
85
85
  }),
86
86
  };
87
87
  }
88
- var IntlMessageFormat = exports.IntlMessageFormat = /** @class */ (function () {
88
+ var IntlMessageFormat = /** @class */ (function () {
89
89
  function IntlMessageFormat(message, locales, overrideFormats, opts) {
90
- if (locales === void 0) { locales = IntlMessageFormat.defaultLocale; }
91
90
  var _this = this;
91
+ if (locales === void 0) { locales = IntlMessageFormat.defaultLocale; }
92
92
  this.formatterCache = {
93
93
  number: {},
94
94
  dateTime: {},
@@ -238,3 +238,4 @@ var IntlMessageFormat = exports.IntlMessageFormat = /** @class */ (function () {
238
238
  };
239
239
  return IntlMessageFormat;
240
240
  }());
241
+ exports.IntlMessageFormat = IntlMessageFormat;
package/src/error.js CHANGED
@@ -10,7 +10,7 @@ var ErrorCode;
10
10
  ErrorCode["INVALID_VALUE"] = "INVALID_VALUE";
11
11
  // When we need specific Intl API but it's not available
12
12
  ErrorCode["MISSING_INTL_API"] = "MISSING_INTL_API";
13
- })(ErrorCode = exports.ErrorCode || (exports.ErrorCode = {}));
13
+ })(ErrorCode || (exports.ErrorCode = ErrorCode = {}));
14
14
  var FormatError = /** @class */ (function (_super) {
15
15
  tslib_1.__extends(FormatError, _super);
16
16
  function FormatError(msg, code, originalMessage) {
package/src/formatters.js CHANGED
@@ -7,7 +7,7 @@ var PART_TYPE;
7
7
  (function (PART_TYPE) {
8
8
  PART_TYPE[PART_TYPE["literal"] = 0] = "literal";
9
9
  PART_TYPE[PART_TYPE["object"] = 1] = "object";
10
- })(PART_TYPE = exports.PART_TYPE || (exports.PART_TYPE = {}));
10
+ })(PART_TYPE || (exports.PART_TYPE = PART_TYPE = {}));
11
11
  function mergeLiteral(parts) {
12
12
  if (parts.length < 2) {
13
13
  return parts;