intl-messageformat 8.2.1 → 8.3.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/lib/core.d.ts CHANGED
@@ -12,8 +12,8 @@ export declare class IntlMessageFormat {
12
12
  private readonly message;
13
13
  private readonly formatterCache;
14
14
  constructor(message: string | MessageFormatElement[], locales?: string | string[], overrideFormats?: Partial<Formats>, opts?: Options);
15
- format: <T = void>(values?: Record<string, string | number | boolean | Date | T | FormatXMLElementFn<T> | null | undefined> | undefined) => string | T | (string | T)[];
16
- formatToParts: <T>(values?: Record<string, string | number | boolean | Date | T | FormatXMLElementFn<T> | null | undefined> | undefined) => MessageFormatPart<T>[];
15
+ format: <T = void>(values?: Record<string, string | number | boolean | Date | T | FormatXMLElementFn<T, string | (string | T)[]> | null | undefined> | undefined) => string | T | (string | T)[];
16
+ formatToParts: <T>(values?: Record<string, string | number | boolean | Date | T | FormatXMLElementFn<T, string | (string | T)[]> | null | undefined> | undefined) => MessageFormatPart<T>[];
17
17
  resolvedOptions: () => {
18
18
  locale: string;
19
19
  };
package/lib/error.d.ts CHANGED
@@ -1,15 +1,26 @@
1
1
  export declare const enum ErrorCode {
2
- MISSING_VALUE = 0,
3
- INVALID_VALUE = 1,
4
- MISSING_INTL_API = 2
2
+ MISSING_VALUE = "MISSING_VALUE",
3
+ INVALID_VALUE = "INVALID_VALUE",
4
+ MISSING_INTL_API = "MISSING_INTL_API"
5
5
  }
6
6
  export declare class FormatError extends Error {
7
7
  readonly code: ErrorCode;
8
- constructor(msg: string, 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);
9
17
  toString(): string;
10
18
  }
11
19
  export declare class InvalidValueError extends FormatError {
12
- constructor(variableId: string, value: any, options: string[]);
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);
13
24
  }
14
25
  export declare class MissingValueError extends FormatError {
15
26
  constructor(variableId: string, originalMessage?: string);
package/lib/error.js CHANGED
@@ -11,11 +11,21 @@ var __extends = (this && this.__extends) || (function () {
11
11
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
12
12
  };
13
13
  })();
14
+ export var ErrorCode;
15
+ (function (ErrorCode) {
16
+ // When we have a placeholder but no value to format
17
+ ErrorCode["MISSING_VALUE"] = "MISSING_VALUE";
18
+ // When value supplied is invalid
19
+ ErrorCode["INVALID_VALUE"] = "INVALID_VALUE";
20
+ // When we need specific Intl API but it's not available
21
+ ErrorCode["MISSING_INTL_API"] = "MISSING_INTL_API";
22
+ })(ErrorCode || (ErrorCode = {}));
14
23
  var FormatError = /** @class */ (function (_super) {
15
24
  __extends(FormatError, _super);
16
- function FormatError(msg, code) {
25
+ function FormatError(msg, code, originalMessage) {
17
26
  var _this = _super.call(this, msg) || this;
18
27
  _this.code = code;
28
+ _this.originalMessage = originalMessage;
19
29
  return _this;
20
30
  }
21
31
  FormatError.prototype.toString = function () {
@@ -26,16 +36,24 @@ var FormatError = /** @class */ (function (_super) {
26
36
  export { FormatError };
27
37
  var InvalidValueError = /** @class */ (function (_super) {
28
38
  __extends(InvalidValueError, _super);
29
- function InvalidValueError(variableId, value, options) {
30
- return _super.call(this, "Invalid values for \"" + variableId + "\": \"" + value + "\". Options are \"" + Object.keys(options).join('", "') + "\"", 1 /* INVALID_VALUE */) || this;
39
+ function InvalidValueError(variableId, value, options, originalMessage) {
40
+ return _super.call(this, "Invalid values for \"" + variableId + "\": \"" + value + "\". Options are \"" + Object.keys(options).join('", "') + "\"", "INVALID_VALUE" /* INVALID_VALUE */, originalMessage) || this;
31
41
  }
32
42
  return InvalidValueError;
33
43
  }(FormatError));
34
44
  export { InvalidValueError };
45
+ var InvalidValueTypeError = /** @class */ (function (_super) {
46
+ __extends(InvalidValueTypeError, _super);
47
+ function InvalidValueTypeError(value, type, originalMessage) {
48
+ return _super.call(this, "Value for \"" + value + "\" must be of type " + type, "INVALID_VALUE" /* INVALID_VALUE */, originalMessage) || this;
49
+ }
50
+ return InvalidValueTypeError;
51
+ }(FormatError));
52
+ export { InvalidValueTypeError };
35
53
  var MissingValueError = /** @class */ (function (_super) {
36
54
  __extends(MissingValueError, _super);
37
55
  function MissingValueError(variableId, originalMessage) {
38
- return _super.call(this, "The intl string context variable \"" + variableId + "\" was not provided to the string \"" + originalMessage + "\"", 0 /* MISSING_VALUE */) || this;
56
+ return _super.call(this, "The intl string context variable \"" + variableId + "\" was not provided to the string \"" + originalMessage + "\"", "MISSING_VALUE" /* MISSING_VALUE */, originalMessage) || this;
39
57
  }
40
58
  return MissingValueError;
41
59
  }(FormatError));
@@ -29,4 +29,4 @@ export interface ObjectPart<T = any> {
29
29
  export declare type MessageFormatPart<T> = LiteralPart | ObjectPart<T>;
30
30
  export declare type PrimitiveType = string | number | boolean | null | undefined | Date;
31
31
  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>[];
32
- export declare type FormatXMLElementFn<T> = (...args: Array<string | T>) => string | Array<string | T>;
32
+ export declare type FormatXMLElementFn<T, R = string | Array<string | T>> = (...args: Array<string | T>) => R;
package/lib/formatters.js CHANGED
@@ -1,5 +1,10 @@
1
1
  import { convertNumberSkeletonToNumberFormatOptions, isArgumentElement, isDateElement, isDateTimeSkeleton, isLiteralElement, isNumberElement, isNumberSkeleton, isPluralElement, isPoundElement, isSelectElement, isTimeElement, parseDateTimeSkeleton, isTagElement, } from 'intl-messageformat-parser';
2
- import { MissingValueError, InvalidValueError, FormatError, } from './error';
2
+ import { MissingValueError, InvalidValueError, FormatError, InvalidValueTypeError, } from './error';
3
+ export var PART_TYPE;
4
+ (function (PART_TYPE) {
5
+ PART_TYPE[PART_TYPE["literal"] = 0] = "literal";
6
+ PART_TYPE[PART_TYPE["object"] = 1] = "object";
7
+ })(PART_TYPE || (PART_TYPE = {}));
3
8
  function mergeLiteral(parts) {
4
9
  if (parts.length < 2) {
5
10
  return parts;
@@ -119,7 +124,7 @@ originalMessage) {
119
124
  var children = el.children, value_1 = el.value;
120
125
  var formatFn = values[value_1];
121
126
  if (!isFormatXMLElementFn(formatFn)) {
122
- throw new TypeError("Value for \"" + value_1 + "\" must be a function");
127
+ throw new InvalidValueTypeError(value_1, 'function', originalMessage);
123
128
  }
124
129
  var parts = formatToParts(children, locales, formatters, formats, values);
125
130
  var chunks = formatFn.apply(void 0, parts.map(function (p) { return p.value; }));
@@ -136,7 +141,7 @@ originalMessage) {
136
141
  if (isSelectElement(el)) {
137
142
  var opt = el.options[value] || el.options.other;
138
143
  if (!opt) {
139
- throw new InvalidValueError(el.value, value, Object.keys(el.options));
144
+ throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage);
140
145
  }
141
146
  result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values));
142
147
  continue;
@@ -145,7 +150,7 @@ originalMessage) {
145
150
  var opt = el.options["=" + value];
146
151
  if (!opt) {
147
152
  if (!Intl.PluralRules) {
148
- throw new FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", 2 /* MISSING_INTL_API */);
153
+ throw new FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", "MISSING_INTL_API" /* MISSING_INTL_API */, originalMessage);
149
154
  }
150
155
  var rule = formatters
151
156
  .getPluralRules(locales, { type: el.pluralType })
@@ -153,7 +158,7 @@ originalMessage) {
153
158
  opt = el.options[rule] || el.options.other;
154
159
  }
155
160
  if (!opt) {
156
- throw new InvalidValueError(el.value, value, Object.keys(el.options));
161
+ throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage);
157
162
  }
158
163
  result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values, value - (el.offset || 0)));
159
164
  continue;
@@ -4,14 +4,22 @@ import { parse } from 'intl-messageformat-parser';
4
4
  export declare function createDefaultFormatters(cache?: FormatterCache): Formatters;
5
5
 
6
6
  export declare const enum ErrorCode {
7
- MISSING_VALUE = 0,
8
- INVALID_VALUE = 1,
9
- MISSING_INTL_API = 2
7
+ MISSING_VALUE = "MISSING_VALUE",
8
+ INVALID_VALUE = "INVALID_VALUE",
9
+ MISSING_INTL_API = "MISSING_INTL_API"
10
10
  }
11
11
 
12
12
  export declare class FormatError extends Error {
13
13
  readonly code: ErrorCode;
14
- constructor(msg: string, code: ErrorCode);
14
+ /**
15
+ * Original message we're trying to format
16
+ * `undefined` if we're only dealing w/ AST
17
+ *
18
+ * @type {(string | undefined)}
19
+ * @memberof FormatError
20
+ */
21
+ readonly originalMessage: string | undefined;
22
+ constructor(msg: string, code: ErrorCode, originalMessage?: string);
15
23
  toString(): string;
16
24
  }
17
25
 
@@ -35,7 +43,7 @@ export declare interface Formatters {
35
43
 
36
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>[];
37
45
 
38
- export declare type FormatXMLElementFn<T> = (...args: Array<string | T>) => string | Array<string | T>;
46
+ export declare type FormatXMLElementFn<T, R = string | Array<string | T>> = (...args: Array<string | T>) => R;
39
47
 
40
48
  declare class IntlMessageFormat {
41
49
  private readonly ast;
@@ -45,8 +53,8 @@ declare class IntlMessageFormat {
45
53
  private readonly message;
46
54
  private readonly formatterCache;
47
55
  constructor(message: string | MessageFormatElement[], locales?: string | string[], overrideFormats?: Partial<Formats>, opts?: Options);
48
- format: <T = void>(values?: Record<string, string | number | boolean | Date | T | FormatXMLElementFn<T> | null | undefined> | undefined) => string | T | (string | T)[];
49
- formatToParts: <T>(values?: Record<string, string | number | boolean | Date | T | FormatXMLElementFn<T> | null | undefined> | undefined) => MessageFormatPart<T>[];
56
+ format: <T = void>(values?: Record<string, string | number | boolean | Date | T | FormatXMLElementFn<T, string | (string | T)[]> | null | undefined> | undefined) => string | T | (string | T)[];
57
+ formatToParts: <T>(values?: Record<string, string | number | boolean | Date | T | FormatXMLElementFn<T, string | (string | T)[]> | null | undefined> | undefined) => MessageFormatPart<T>[];
50
58
  resolvedOptions: () => {
51
59
  locale: string;
52
60
  };
@@ -114,7 +122,11 @@ export { IntlMessageFormat }
114
122
  export default IntlMessageFormat;
115
123
 
116
124
  export declare class InvalidValueError extends FormatError {
117
- constructor(variableId: string, value: any, options: string[]);
125
+ constructor(variableId: string, value: any, options: string[], originalMessage?: string);
126
+ }
127
+
128
+ export declare class InvalidValueTypeError extends FormatError {
129
+ constructor(value: any, type: string, originalMessage?: string);
118
130
  }
119
131
 
120
132
  export declare interface LiteralPart {
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.7.0"
8
+ "packageVersion": "7.7.8"
9
9
  }
10
10
  ]
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "intl-messageformat",
3
- "version": "8.2.1",
3
+ "version": "8.3.2",
4
4
  "description": "Formats ICU Message strings with number, date, plural, and select placeholders to create localized messages.",
5
5
  "keywords": [
6
6
  "i18n",
@@ -31,8 +31,8 @@
31
31
  "module": "lib/index.js",
32
32
  "types": "lib/intl-messageformat.d.ts",
33
33
  "dependencies": {
34
- "intl-format-cache": "^4.2.21",
35
- "intl-messageformat-parser": "^4.1.0"
34
+ "intl-format-cache": "^4.2.22",
35
+ "intl-messageformat-parser": "^4.1.1"
36
36
  },
37
37
  "files": [
38
38
  "dist",
@@ -40,7 +40,7 @@
40
40
  "src"
41
41
  ],
42
42
  "devDependencies": {
43
- "@formatjs/intl-pluralrules": "^1.5.2"
43
+ "@formatjs/intl-pluralrules": "^1.5.3"
44
44
  },
45
45
  "sideEffects": false,
46
46
  "scripts": {
@@ -58,5 +58,5 @@
58
58
  "test": "tests"
59
59
  },
60
60
  "license": "BSD-3-Clause",
61
- "gitHead": "e0bb34c71e99fd4c68244f3bc668c748b2f18413"
61
+ "gitHead": "33528f6975bd231fdf8635726892c98499326109"
62
62
  }
package/src/error.ts CHANGED
@@ -1,17 +1,26 @@
1
1
  export const enum ErrorCode {
2
2
  // When we have a placeholder but no value to format
3
- MISSING_VALUE,
3
+ MISSING_VALUE = 'MISSING_VALUE',
4
4
  // When value supplied is invalid
5
- INVALID_VALUE,
5
+ INVALID_VALUE = 'INVALID_VALUE',
6
6
  // When we need specific Intl API but it's not available
7
- MISSING_INTL_API,
7
+ MISSING_INTL_API = 'MISSING_INTL_API',
8
8
  }
9
9
 
10
10
  export class FormatError extends Error {
11
11
  public readonly code: ErrorCode;
12
- constructor(msg: string, code: ErrorCode) {
12
+ /**
13
+ * Original message we're trying to format
14
+ * `undefined` if we're only dealing w/ AST
15
+ *
16
+ * @type {(string | undefined)}
17
+ * @memberof FormatError
18
+ */
19
+ public readonly originalMessage: string | undefined;
20
+ constructor(msg: string, code: ErrorCode, originalMessage?: string) {
13
21
  super(msg);
14
22
  this.code = code;
23
+ this.originalMessage = originalMessage;
15
24
  }
16
25
  public toString() {
17
26
  return `[formatjs Error: ${this.code}] ${this.message}`;
@@ -19,12 +28,28 @@ export class FormatError extends Error {
19
28
  }
20
29
 
21
30
  export class InvalidValueError extends FormatError {
22
- constructor(variableId: string, value: any, options: string[]) {
31
+ constructor(
32
+ variableId: string,
33
+ value: any,
34
+ options: string[],
35
+ originalMessage?: string
36
+ ) {
23
37
  super(
24
38
  `Invalid values for "${variableId}": "${value}". Options are "${Object.keys(
25
39
  options
26
40
  ).join('", "')}"`,
27
- ErrorCode.INVALID_VALUE
41
+ ErrorCode.INVALID_VALUE,
42
+ originalMessage
43
+ );
44
+ }
45
+ }
46
+
47
+ export class InvalidValueTypeError extends FormatError {
48
+ constructor(value: any, type: string, originalMessage?: string) {
49
+ super(
50
+ `Value for "${value}" must be of type ${type}`,
51
+ ErrorCode.INVALID_VALUE,
52
+ originalMessage
28
53
  );
29
54
  }
30
55
  }
@@ -33,7 +58,8 @@ export class MissingValueError extends FormatError {
33
58
  constructor(variableId: string, originalMessage?: string) {
34
59
  super(
35
60
  `The intl string context variable "${variableId}" was not provided to the string "${originalMessage}"`,
36
- ErrorCode.MISSING_VALUE
61
+ ErrorCode.MISSING_VALUE,
62
+ originalMessage
37
63
  );
38
64
  }
39
65
  }
package/src/formatters.ts CHANGED
@@ -19,6 +19,7 @@ import {
19
19
  InvalidValueError,
20
20
  ErrorCode,
21
21
  FormatError,
22
+ InvalidValueTypeError,
22
23
  } from './error';
23
24
 
24
25
  export interface Formats {
@@ -203,7 +204,7 @@ export function formatToParts<T>(
203
204
  const {children, value} = el;
204
205
  const formatFn = values[value];
205
206
  if (!isFormatXMLElementFn<T>(formatFn)) {
206
- throw new TypeError(`Value for "${value}" must be a function`);
207
+ throw new InvalidValueTypeError(value, 'function', originalMessage);
207
208
  }
208
209
  const parts = formatToParts<T>(
209
210
  children,
@@ -231,7 +232,12 @@ export function formatToParts<T>(
231
232
  if (isSelectElement(el)) {
232
233
  const opt = el.options[value as string] || el.options.other;
233
234
  if (!opt) {
234
- throw new InvalidValueError(el.value, value, Object.keys(el.options));
235
+ throw new InvalidValueError(
236
+ el.value,
237
+ value,
238
+ Object.keys(el.options),
239
+ originalMessage
240
+ );
235
241
  }
236
242
  result.push(
237
243
  ...formatToParts(opt.value, locales, formatters, formats, values)
@@ -246,7 +252,8 @@ export function formatToParts<T>(
246
252
  `Intl.PluralRules is not available in this environment.
247
253
  Try polyfilling it using "@formatjs/intl-pluralrules"
248
254
  `,
249
- ErrorCode.MISSING_INTL_API
255
+ ErrorCode.MISSING_INTL_API,
256
+ originalMessage
250
257
  );
251
258
  }
252
259
  const rule = formatters
@@ -255,7 +262,12 @@ Try polyfilling it using "@formatjs/intl-pluralrules"
255
262
  opt = el.options[rule] || el.options.other;
256
263
  }
257
264
  if (!opt) {
258
- throw new InvalidValueError(el.value, value, Object.keys(el.options));
265
+ throw new InvalidValueError(
266
+ el.value,
267
+ value,
268
+ Object.keys(el.options),
269
+ originalMessage
270
+ );
259
271
  }
260
272
  result.push(
261
273
  ...formatToParts(
@@ -273,6 +285,6 @@ Try polyfilling it using "@formatjs/intl-pluralrules"
273
285
  return mergeLiteral(result);
274
286
  }
275
287
 
276
- export type FormatXMLElementFn<T> = (
288
+ export type FormatXMLElementFn<T, R = string | Array<string | T>> = (
277
289
  ...args: Array<string | T>
278
- ) => string | Array<string | T>;
290
+ ) => R;