intl-messageformat 4.1.1 → 4.4.0

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +32 -44
  2. package/LICENSE +0 -0
  3. package/README.md +108 -59
  4. package/core.js +1 -0
  5. package/dist/compiler.d.ts +10 -4
  6. package/dist/compiler.js +11 -14
  7. package/dist/compiler.js.map +1 -1
  8. package/dist/core.d.ts +78 -0
  9. package/dist/core.js +248 -0
  10. package/dist/core.js.map +1 -0
  11. package/dist/index.d.ts +4 -16
  12. package/dist/index.js +7 -198
  13. package/dist/index.js.map +1 -1
  14. package/dist/umd/intl-messageformat.js +1548 -1373
  15. package/dist/umd/intl-messageformat.js.map +1 -1
  16. package/dist/umd/intl-messageformat.min.js +1 -1
  17. package/dist/umd/intl-messageformat.min.js.map +1 -1
  18. package/index.js +0 -0
  19. package/lib/compiler.d.ts +10 -4
  20. package/lib/compiler.js +11 -14
  21. package/lib/compiler.js.map +1 -1
  22. package/lib/core.d.ts +78 -0
  23. package/lib/core.js +245 -0
  24. package/lib/core.js.map +1 -0
  25. package/lib/index.d.ts +4 -16
  26. package/lib/index.js +4 -198
  27. package/lib/index.js.map +1 -1
  28. package/package.json +4 -4
  29. package/src/compiler.ts +39 -16
  30. package/src/core.ts +269 -0
  31. package/src/index.ts +6 -242
  32. package/.nyc_output/34ec6f1e-d2e9-445f-8813-bd6e8b5975bb.json +0 -1
  33. package/.nyc_output/9b57550b-ff23-4ed6-b289-f7ff8d2beb4f.json +0 -1
  34. package/.nyc_output/b696f16a-7b55-4692-a441-41aa11ca2fb0.json +0 -1
  35. package/.nyc_output/processinfo/34ec6f1e-d2e9-445f-8813-bd6e8b5975bb.json +0 -1
  36. package/.nyc_output/processinfo/9b57550b-ff23-4ed6-b289-f7ff8d2beb4f.json +0 -1
  37. package/.nyc_output/processinfo/b696f16a-7b55-4692-a441-41aa11ca2fb0.json +0 -1
  38. package/.nyc_output/processinfo/index.json +0 -1
package/lib/compiler.js CHANGED
@@ -17,7 +17,7 @@ var __extends = (this && this.__extends) || (function () {
17
17
  };
18
18
  })();
19
19
  var Compiler = /** @class */ (function () {
20
- function Compiler(locales, formats) {
20
+ function Compiler(locales, formats, formatters) {
21
21
  this.locales = [];
22
22
  this.formats = {
23
23
  number: {},
@@ -29,6 +29,7 @@ var Compiler = /** @class */ (function () {
29
29
  this.pluralStack = [];
30
30
  this.locales = locales;
31
31
  this.formats = formats;
32
+ this.formatters = formatters;
32
33
  }
33
34
  Compiler.prototype.compile = function (ast) {
34
35
  this.pluralStack = [];
@@ -73,6 +74,7 @@ var Compiler = /** @class */ (function () {
73
74
  };
74
75
  Compiler.prototype.compileArgument = function (element) {
75
76
  var format = element.format, id = element.id;
77
+ var formatters = this.formatters;
76
78
  if (!format) {
77
79
  return new StringFormat(id);
78
80
  }
@@ -81,23 +83,22 @@ var Compiler = /** @class */ (function () {
81
83
  case 'numberFormat':
82
84
  return {
83
85
  id: id,
84
- format: new Intl.NumberFormat(locales, formats.number[format.style])
85
- .format
86
+ format: formatters.getNumberFormat(locales, formats.number[format.style]).format
86
87
  };
87
88
  case 'dateFormat':
88
89
  return {
89
90
  id: id,
90
- format: new Intl.DateTimeFormat(locales, formats.date[format.style])
91
- .format
91
+ format: formatters.getDateTimeFormat(locales, formats.date[format.style]).format
92
92
  };
93
93
  case 'timeFormat':
94
94
  return {
95
95
  id: id,
96
- format: new Intl.DateTimeFormat(locales, formats.time[format.style])
97
- .format
96
+ format: formatters.getDateTimeFormat(locales, formats.time[format.style]).format
98
97
  };
99
98
  case 'pluralFormat':
100
- return new PluralFormat(id, format.ordinal, format.offset, this.compileOptions(element), locales);
99
+ return new PluralFormat(id, format.offset, this.compileOptions(element), formatters.getPluralRules(locales, {
100
+ type: format.ordinal ? 'ordinal' : 'cardinal'
101
+ }));
101
102
  case 'selectFormat':
102
103
  return new SelectFormat(id, this.compileOptions(element));
103
104
  default:
@@ -145,15 +146,12 @@ var StringFormat = /** @class */ (function (_super) {
145
146
  };
146
147
  return StringFormat;
147
148
  }(Formatter));
148
- export { StringFormat };
149
149
  var PluralFormat = /** @class */ (function () {
150
- function PluralFormat(id, useOrdinal, offset, options, locales) {
150
+ function PluralFormat(id, offset, options, pluralRules) {
151
151
  this.id = id;
152
152
  this.offset = offset;
153
153
  this.options = options;
154
- this.pluralRules = new Intl.PluralRules(locales, {
155
- type: useOrdinal ? 'ordinal' : 'cardinal'
156
- });
154
+ this.pluralRules = pluralRules;
157
155
  }
158
156
  PluralFormat.prototype.getOption = function (value) {
159
157
  var options = this.options;
@@ -163,7 +161,6 @@ var PluralFormat = /** @class */ (function () {
163
161
  };
164
162
  return PluralFormat;
165
163
  }());
166
- export { PluralFormat };
167
164
  var PluralOffsetString = /** @class */ (function (_super) {
168
165
  __extends(PluralOffsetString, _super);
169
166
  function PluralOffsetString(id, offset, numberFormat, string) {
@@ -1 +1 @@
1
- {"version":3,"file":"compiler.js","sourceRoot":"","sources":["../src/compiler.ts"],"names":[],"mappings":"AAAA;;;;EAIE;;;;;;;;;;;;;;AAuBF;IAWE,kBAAY,OAA0B,EAAE,OAAgB;QAVhD,YAAO,GAAsB,EAAE,CAAC;QAChC,YAAO,GAAY;YACzB,MAAM,EAAE,EAAE;YACV,IAAI,EAAE,EAAE;YACR,IAAI,EAAE,EAAE;SACT,CAAC;QACM,uBAAkB,GAA6B,IAAI,CAAC;QACpD,kBAAa,GAAuC,IAAI,CAAC;QACzD,gBAAW,GAA8C,EAAE,CAAC;QAGlE,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,0BAAO,GAAP,UAAQ,GAAyB;QAC/B,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC;QAC1B,IAAI,CAAC,kBAAkB,GAAG,IAAI,CAAC;QAE/B,OAAO,IAAI,CAAC,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CAAC,MAAM;aACf,OAAO,CAAC,aAAa,EAAE,IAAI,GAAG,MAAM,CAAC;aACrC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,CAAC;IACH,yBAAC;AAAD,CAAC,AAvBD,CAAwC,SAAS,GAuBhD;;AAED;IAGE,sBAAY,EAAU,EAAE,OAAkC;QACxD,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,gCAAS,GAAT,UAAU,KAAa;QACb,IAAA,sBAAO,CAAU;QACzB,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC,KAAK,CAAC;IACzC,CAAC;IACH,mBAAC;AAAD,CAAC,AAZD,IAYC;;AAED,MAAM,UAAU,sBAAsB,CACpC,CAAM;IAEN,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;AACrB,CAAC"}
package/lib/core.d.ts ADDED
@@ -0,0 +1,78 @@
1
+ import { Formats, Formatters } from './compiler';
2
+ import parser, { MessageFormatPattern } from 'intl-messageformat-parser';
3
+ export interface Options {
4
+ formatters?: Formatters;
5
+ }
6
+ export declare function createDefaultFormatters(): Formatters;
7
+ export declare class IntlMessageFormat {
8
+ private ast;
9
+ private locale;
10
+ private pattern;
11
+ private message;
12
+ constructor(message: string | MessageFormatPattern, locales?: string | string[], overrideFormats?: Partial<Formats>, opts?: Options);
13
+ format: (values?: Record<string, string | number | boolean | null | undefined> | undefined) => string;
14
+ resolvedOptions(): {
15
+ locale: string;
16
+ };
17
+ getAst(): MessageFormatPattern;
18
+ static defaultLocale: string;
19
+ static __parse: typeof parser['parse'] | undefined;
20
+ static formats: {
21
+ number: {
22
+ currency: {
23
+ style: string;
24
+ };
25
+ percent: {
26
+ style: string;
27
+ };
28
+ };
29
+ date: {
30
+ short: {
31
+ month: string;
32
+ day: string;
33
+ year: string;
34
+ };
35
+ medium: {
36
+ month: string;
37
+ day: string;
38
+ year: string;
39
+ };
40
+ long: {
41
+ month: string;
42
+ day: string;
43
+ year: string;
44
+ };
45
+ full: {
46
+ weekday: string;
47
+ month: string;
48
+ day: string;
49
+ year: string;
50
+ };
51
+ };
52
+ time: {
53
+ short: {
54
+ hour: string;
55
+ minute: string;
56
+ };
57
+ medium: {
58
+ hour: string;
59
+ minute: string;
60
+ second: string;
61
+ };
62
+ long: {
63
+ hour: string;
64
+ minute: string;
65
+ second: string;
66
+ timeZoneName: string;
67
+ };
68
+ full: {
69
+ hour: string;
70
+ minute: string;
71
+ second: string;
72
+ timeZoneName: string;
73
+ };
74
+ };
75
+ };
76
+ }
77
+ export { Formats, Pattern } from './compiler';
78
+ export default IntlMessageFormat;
package/lib/core.js ADDED
@@ -0,0 +1,245 @@
1
+ /*
2
+ Copyright (c) 2014, Yahoo! Inc. All rights reserved.
3
+ Copyrights licensed under the New BSD License.
4
+ See the accompanying LICENSE file for terms.
5
+ */
6
+ var __extends = (this && this.__extends) || (function () {
7
+ var extendStatics = function (d, b) {
8
+ extendStatics = Object.setPrototypeOf ||
9
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
10
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
11
+ return extendStatics(d, b);
12
+ };
13
+ return function (d, b) {
14
+ extendStatics(d, b);
15
+ function __() { this.constructor = d; }
16
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
17
+ };
18
+ })();
19
+ var __assign = (this && this.__assign) || function () {
20
+ __assign = Object.assign || function(t) {
21
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
22
+ s = arguments[i];
23
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
24
+ t[p] = s[p];
25
+ }
26
+ return t;
27
+ };
28
+ return __assign.apply(this, arguments);
29
+ };
30
+ /* jslint esnext: true */
31
+ import Compiler, { isSelectOrPluralFormat } from './compiler';
32
+ // -- MessageFormat --------------------------------------------------------
33
+ function resolveLocale(locales) {
34
+ if (typeof locales === 'string') {
35
+ locales = [locales];
36
+ }
37
+ try {
38
+ return Intl.NumberFormat.supportedLocalesOf(locales, {
39
+ // IE11 localeMatcher `lookup` seems to convert `en` -> `en-US`
40
+ // but not other browsers,
41
+ localeMatcher: 'best fit'
42
+ })[0];
43
+ }
44
+ catch (e) {
45
+ return IntlMessageFormat.defaultLocale;
46
+ }
47
+ }
48
+ function formatPatterns(pattern, values) {
49
+ var result = '';
50
+ for (var _i = 0, pattern_1 = pattern; _i < pattern_1.length; _i++) {
51
+ var part = pattern_1[_i];
52
+ // Exist early for string parts.
53
+ if (typeof part === 'string') {
54
+ result += part;
55
+ continue;
56
+ }
57
+ var id = part.id;
58
+ // Enforce that all required values are provided by the caller.
59
+ if (!(values && id in values)) {
60
+ throw new FormatError("A value must be provided for: " + id, id);
61
+ }
62
+ var value = values[id];
63
+ // Recursively format plural and select parts' option — which can be a
64
+ // nested pattern structure. The choosing of the option to use is
65
+ // abstracted-by and delegated-to the part helper object.
66
+ if (isSelectOrPluralFormat(part)) {
67
+ result += formatPatterns(part.getOption(value), values);
68
+ }
69
+ else {
70
+ result += part.format(value);
71
+ }
72
+ }
73
+ return result;
74
+ }
75
+ function mergeConfig(c1, c2) {
76
+ if (!c2) {
77
+ return c1;
78
+ }
79
+ return __assign({}, (c1 || {}), (c2 || {}), Object.keys(c1).reduce(function (all, k) {
80
+ all[k] = __assign({}, c1[k], (c2[k] || {}));
81
+ return all;
82
+ }, {}));
83
+ }
84
+ function mergeConfigs(defaultConfig, configs) {
85
+ if (!configs) {
86
+ return defaultConfig;
87
+ }
88
+ return Object.keys(defaultConfig).reduce(function (all, k) {
89
+ all[k] = mergeConfig(defaultConfig[k], configs[k]);
90
+ return all;
91
+ }, __assign({}, defaultConfig));
92
+ }
93
+ var FormatError = /** @class */ (function (_super) {
94
+ __extends(FormatError, _super);
95
+ function FormatError(msg, variableId) {
96
+ var _this = _super.call(this, msg) || this;
97
+ _this.variableId = variableId;
98
+ return _this;
99
+ }
100
+ return FormatError;
101
+ }(Error));
102
+ export function createDefaultFormatters() {
103
+ return {
104
+ getNumberFormat: function () {
105
+ var _a;
106
+ var args = [];
107
+ for (var _i = 0; _i < arguments.length; _i++) {
108
+ args[_i] = arguments[_i];
109
+ }
110
+ return new ((_a = Intl.NumberFormat).bind.apply(_a, [void 0].concat(args)))();
111
+ },
112
+ getDateTimeFormat: function () {
113
+ var _a;
114
+ var args = [];
115
+ for (var _i = 0; _i < arguments.length; _i++) {
116
+ args[_i] = arguments[_i];
117
+ }
118
+ return new ((_a = Intl.DateTimeFormat).bind.apply(_a, [void 0].concat(args)))();
119
+ },
120
+ getPluralRules: function () {
121
+ var _a;
122
+ var args = [];
123
+ for (var _i = 0; _i < arguments.length; _i++) {
124
+ args[_i] = arguments[_i];
125
+ }
126
+ return new ((_a = Intl.PluralRules).bind.apply(_a, [void 0].concat(args)))();
127
+ }
128
+ };
129
+ }
130
+ var IntlMessageFormat = /** @class */ (function () {
131
+ function IntlMessageFormat(message, locales, overrideFormats, opts) {
132
+ var _this = this;
133
+ if (locales === void 0) { locales = IntlMessageFormat.defaultLocale; }
134
+ this.format = function (values) {
135
+ try {
136
+ return formatPatterns(_this.pattern, values);
137
+ }
138
+ catch (e) {
139
+ if (e.variableId) {
140
+ throw new Error("The intl string context variable '" + e.variableId + "' was not provided to the string '" + _this.message + "'");
141
+ }
142
+ else {
143
+ throw e;
144
+ }
145
+ }
146
+ };
147
+ if (typeof message === 'string') {
148
+ if (!IntlMessageFormat.__parse) {
149
+ throw new TypeError('IntlMessageFormat.__parse must be set to process `message` of type `string`');
150
+ }
151
+ // Parse string messages into an AST.
152
+ this.ast = IntlMessageFormat.__parse(message);
153
+ }
154
+ else {
155
+ this.ast = message;
156
+ }
157
+ this.message = message;
158
+ if (!(this.ast && this.ast.type === 'messageFormatPattern')) {
159
+ throw new TypeError('A message must be provided as a String or AST.');
160
+ }
161
+ // Creates a new object with the specified `formats` merged with the default
162
+ // formats.
163
+ var formats = mergeConfigs(IntlMessageFormat.formats, overrideFormats);
164
+ // Defined first because it's used to build the format pattern.
165
+ this.locale = resolveLocale(locales || []);
166
+ var formatters = (opts && opts.formatters) || createDefaultFormatters();
167
+ // Compile the `ast` to a pattern that is highly optimized for repeated
168
+ // `format()` invocations. **Note:** This passes the `locales` set provided
169
+ // to the constructor instead of just the resolved locale.
170
+ this.pattern = new Compiler(locales, formats, formatters).compile(this.ast);
171
+ // "Bind" `format()` method to `this` so it can be passed by reference like
172
+ // the other `Intl` APIs.
173
+ }
174
+ IntlMessageFormat.prototype.resolvedOptions = function () {
175
+ return { locale: this.locale };
176
+ };
177
+ IntlMessageFormat.prototype.getAst = function () {
178
+ return this.ast;
179
+ };
180
+ IntlMessageFormat.defaultLocale = 'en';
181
+ IntlMessageFormat.__parse = undefined;
182
+ // Default format options used as the prototype of the `formats` provided to the
183
+ // constructor. These are used when constructing the internal Intl.NumberFormat
184
+ // and Intl.DateTimeFormat instances.
185
+ IntlMessageFormat.formats = {
186
+ number: {
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
+ export { IntlMessageFormat };
244
+ export default IntlMessageFormat;
245
+ //# sourceMappingURL=core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAAA;;;;EAIE;;;;;;;;;;;;;;;;;;;;;;;;;AAEF,yBAAyB;AAEzB,OAAO,QAAQ,EAAE,EAEf,sBAAsB,EAGvB,MAAM,YAAY,CAAC;AAGpB,4EAA4E;AAE5E,SAAS,aAAa,CAAC,OAA0B;IAC/C,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QAC/B,OAAO,GAAG,CAAC,OAAO,CAAC,CAAC;KACrB;IACD,IAAI;QACF,OAAO,IAAI,CAAC,YAAY,CAAC,kBAAkB,CAAC,OAAO,EAAE;YACnD,+DAA+D;YAC/D,0BAA0B;YAC1B,aAAa,EAAE,UAAU;SAC1B,CAAC,CAAC,CAAC,CAAC,CAAC;KACP;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,iBAAiB,CAAC,aAAa,CAAC;KACxC;AACH,CAAC;AAED,SAAS,cAAc,CACrB,OAAkB,EAClB,MAAqE;IAErE,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAmB,UAAO,EAAP,mBAAO,EAAP,qBAAO,EAAP,IAAO,EAAE;QAAvB,IAAM,IAAI,gBAAA;QACb,gCAAgC;QAChC,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;YAC5B,MAAM,IAAI,IAAI,CAAC;YACf,SAAS;SACV;QAEO,IAAA,YAAE,CAAU;QAEpB,+DAA+D;QAC/D,IAAI,CAAC,CAAC,MAAM,IAAI,EAAE,IAAI,MAAM,CAAC,EAAE;YAC7B,MAAM,IAAI,WAAW,CAAC,mCAAiC,EAAI,EAAE,EAAE,CAAC,CAAC;SAClE;QAED,IAAM,KAAK,GAAG,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,EAAE;gBACR,KAAK,EAAE,UAAU;aAClB;YAED,OAAO,EAAE;gBACP,KAAK,EAAE,SAAS;aACjB;SACF;QAED,IAAI,EAAE;YACJ,KAAK,EAAE;gBACL,KAAK,EAAE,SAAS;gBAChB,GAAG,EAAE,SAAS;gBACd,IAAI,EAAE,SAAS;aAChB;YAED,MAAM,EAAE;gBACN,KAAK,EAAE,OAAO;gBACd,GAAG,EAAE,SAAS;gBACd,IAAI,EAAE,SAAS;aAChB;YAED,IAAI,EAAE;gBACJ,KAAK,EAAE,MAAM;gBACb,GAAG,EAAE,SAAS;gBACd,IAAI,EAAE,SAAS;aAChB;YAED,IAAI,EAAE;gBACJ,OAAO,EAAE,MAAM;gBACf,KAAK,EAAE,MAAM;gBACb,GAAG,EAAE,SAAS;gBACd,IAAI,EAAE,SAAS;aAChB;SACF;QAED,IAAI,EAAE;YACJ,KAAK,EAAE;gBACL,IAAI,EAAE,SAAS;gBACf,MAAM,EAAE,SAAS;aAClB;YAED,MAAM,EAAE;gBACN,IAAI,EAAE,SAAS;gBACf,MAAM,EAAE,SAAS;gBACjB,MAAM,EAAE,SAAS;aAClB;YAED,IAAI,EAAE;gBACJ,IAAI,EAAE,SAAS;gBACf,MAAM,EAAE,SAAS;gBACjB,MAAM,EAAE,SAAS;gBACjB,YAAY,EAAE,OAAO;aACtB;YAED,IAAI,EAAE;gBACJ,IAAI,EAAE,SAAS;gBACf,MAAM,EAAE,SAAS;gBACjB,MAAM,EAAE,SAAS;gBACjB,YAAY,EAAE,OAAO;aACtB;SACF;KACF,CAAC;IACJ,wBAAC;CAAA,AA1ID,IA0IC;SA1IY,iBAAiB;AA6I9B,eAAe,iBAAiB,CAAC"}
package/lib/index.d.ts CHANGED
@@ -1,17 +1,5 @@
1
- import { Formats } from './compiler';
2
- import parser, { MessageFormatPattern } from 'intl-messageformat-parser';
3
- export interface IntlMessageFormat {
4
- new (message: string | MessageFormatPattern, locales?: string | string[], overrideFormats?: Partial<Formats>): IntlMessageFormat;
5
- (message: string | MessageFormatPattern, locales?: string | string[], overrideFormats?: Partial<Formats>): IntlMessageFormat;
6
- format(values?: Record<string, string | number | boolean | null | undefined>): string;
7
- resolvedOptions(): {
8
- locale: string;
9
- };
10
- getAst(): ReturnType<typeof parser['parse']>;
11
- defaultLocale: string;
12
- formats: Formats;
13
- __parse: typeof parser['parse'];
14
- }
15
- declare const MessageFormat: IntlMessageFormat;
1
+ import IntlMessageFormat from './core';
16
2
  export { Formats, Pattern } from './compiler';
17
- export default MessageFormat;
3
+ export * from './core';
4
+ export { Formatters } from './compiler';
5
+ export default IntlMessageFormat;
package/lib/index.js CHANGED
@@ -3,203 +3,9 @@ Copyright (c) 2014, Yahoo! Inc. All rights reserved.
3
3
  Copyrights licensed under the New BSD License.
4
4
  See the accompanying LICENSE file for terms.
5
5
  */
6
- var __extends = (this && this.__extends) || (function () {
7
- var extendStatics = function (d, b) {
8
- extendStatics = Object.setPrototypeOf ||
9
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
10
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
11
- return extendStatics(d, b);
12
- };
13
- return function (d, b) {
14
- extendStatics(d, b);
15
- function __() { this.constructor = d; }
16
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
17
- };
18
- })();
19
- var __assign = (this && this.__assign) || function () {
20
- __assign = Object.assign || function(t) {
21
- for (var s, i = 1, n = arguments.length; i < n; i++) {
22
- s = arguments[i];
23
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
24
- t[p] = s[p];
25
- }
26
- return t;
27
- };
28
- return __assign.apply(this, arguments);
29
- };
30
- /* jslint esnext: true */
31
- import Compiler, { isSelectOrPluralFormat } from './compiler';
32
6
  import parser from 'intl-messageformat-parser';
33
- // -- MessageFormat --------------------------------------------------------
34
- function resolveLocale(locales) {
35
- if (typeof locales === 'string') {
36
- locales = [locales];
37
- }
38
- try {
39
- return Intl.NumberFormat.supportedLocalesOf(locales, {
40
- // IE11 localeMatcher `lookup` seems to convert `en` -> `en-US`
41
- // but not other browsers,
42
- localeMatcher: 'best fit'
43
- })[0];
44
- }
45
- catch (e) {
46
- return MessageFormat.defaultLocale;
47
- }
48
- }
49
- function formatPatterns(pattern, values) {
50
- var result = '';
51
- for (var _i = 0, pattern_1 = pattern; _i < pattern_1.length; _i++) {
52
- var part = pattern_1[_i];
53
- // Exist early for string parts.
54
- if (typeof part === 'string') {
55
- result += part;
56
- continue;
57
- }
58
- var id = part.id;
59
- // Enforce that all required values are provided by the caller.
60
- if (!(values && id in values)) {
61
- throw new FormatError("A value must be provided for: " + id, id);
62
- }
63
- var value = values[id];
64
- // Recursively format plural and select parts' option — which can be a
65
- // nested pattern structure. The choosing of the option to use is
66
- // abstracted-by and delegated-to the part helper object.
67
- if (isSelectOrPluralFormat(part)) {
68
- result += formatPatterns(part.getOption(value), values);
69
- }
70
- else {
71
- result += part.format(value);
72
- }
73
- }
74
- return result;
75
- }
76
- function mergeConfig(c1, c2) {
77
- if (!c2) {
78
- return c1;
79
- }
80
- return __assign({}, (c1 || {}), (c2 || {}), Object.keys(c1).reduce(function (all, k) {
81
- all[k] = __assign({}, c1[k], (c2[k] || {}));
82
- return all;
83
- }, {}));
84
- }
85
- function mergeConfigs(defaultConfig, configs) {
86
- if (!configs) {
87
- return defaultConfig;
88
- }
89
- return Object.keys(defaultConfig).reduce(function (all, k) {
90
- all[k] = mergeConfig(defaultConfig[k], configs[k]);
91
- return all;
92
- }, __assign({}, defaultConfig));
93
- }
94
- var FormatError = /** @class */ (function (_super) {
95
- __extends(FormatError, _super);
96
- function FormatError(msg, variableId) {
97
- var _this = _super.call(this, msg) || this;
98
- _this.variableId = variableId;
99
- return _this;
100
- }
101
- return FormatError;
102
- }(Error));
103
- var MessageFormat = (function (message, locales, overrideFormats) {
104
- if (locales === void 0) { locales = MessageFormat.defaultLocale; }
105
- // Parse string messages into an AST.
106
- var ast = typeof message === 'string' ? MessageFormat.__parse(message) : message;
107
- if (!(ast && ast.type === 'messageFormatPattern')) {
108
- throw new TypeError('A message must be provided as a String or AST.');
109
- }
110
- // Creates a new object with the specified `formats` merged with the default
111
- // formats.
112
- var formats = mergeConfigs(MessageFormat.formats, overrideFormats);
113
- // Defined first because it's used to build the format pattern.
114
- var locale = resolveLocale(locales || []);
115
- // Compile the `ast` to a pattern that is highly optimized for repeated
116
- // `format()` invocations. **Note:** This passes the `locales` set provided
117
- // to the constructor instead of just the resolved locale.
118
- var pattern = new Compiler(locales, formats).compile(ast);
119
- // "Bind" `format()` method to `this` so it can be passed by reference like
120
- // the other `Intl` APIs.
121
- return {
122
- format: function (values) {
123
- try {
124
- return formatPatterns(pattern, values);
125
- }
126
- catch (e) {
127
- if (e.variableId) {
128
- throw new Error("The intl string context variable '" + e.variableId + "' was not provided to the string '" + message + "'");
129
- }
130
- else {
131
- throw e;
132
- }
133
- }
134
- },
135
- resolvedOptions: function () {
136
- return { locale: locale };
137
- },
138
- getAst: function () {
139
- return ast;
140
- }
141
- };
142
- });
143
- MessageFormat.defaultLocale = 'en';
144
- // Default format options used as the prototype of the `formats` provided to the
145
- // constructor. These are used when constructing the internal Intl.NumberFormat
146
- // and Intl.DateTimeFormat instances.
147
- MessageFormat.formats = {
148
- number: {
149
- currency: {
150
- style: 'currency'
151
- },
152
- percent: {
153
- style: 'percent'
154
- }
155
- },
156
- date: {
157
- short: {
158
- month: 'numeric',
159
- day: 'numeric',
160
- year: '2-digit'
161
- },
162
- medium: {
163
- month: 'short',
164
- day: 'numeric',
165
- year: 'numeric'
166
- },
167
- long: {
168
- month: 'long',
169
- day: 'numeric',
170
- year: 'numeric'
171
- },
172
- full: {
173
- weekday: 'long',
174
- month: 'long',
175
- day: 'numeric',
176
- year: 'numeric'
177
- }
178
- },
179
- time: {
180
- short: {
181
- hour: 'numeric',
182
- minute: 'numeric'
183
- },
184
- medium: {
185
- hour: 'numeric',
186
- minute: 'numeric',
187
- second: 'numeric'
188
- },
189
- long: {
190
- hour: 'numeric',
191
- minute: 'numeric',
192
- second: 'numeric',
193
- timeZoneName: 'short'
194
- },
195
- full: {
196
- hour: 'numeric',
197
- minute: 'numeric',
198
- second: 'numeric',
199
- timeZoneName: 'short'
200
- }
201
- }
202
- };
203
- MessageFormat.__parse = parser.parse;
204
- export default MessageFormat;
7
+ import IntlMessageFormat from './core';
8
+ IntlMessageFormat.__parse = parser.parse;
9
+ export * from './core';
10
+ export default IntlMessageFormat;
205
11
  //# sourceMappingURL=index.js.map