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/dist/core.js ADDED
@@ -0,0 +1,248 @@
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
+ var __extends = (this && this.__extends) || (function () {
8
+ var extendStatics = function (d, b) {
9
+ extendStatics = Object.setPrototypeOf ||
10
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
12
+ return extendStatics(d, b);
13
+ };
14
+ return function (d, b) {
15
+ extendStatics(d, b);
16
+ function __() { this.constructor = d; }
17
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
18
+ };
19
+ })();
20
+ var __assign = (this && this.__assign) || function () {
21
+ __assign = Object.assign || function(t) {
22
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
23
+ s = arguments[i];
24
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
25
+ t[p] = s[p];
26
+ }
27
+ return t;
28
+ };
29
+ return __assign.apply(this, arguments);
30
+ };
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ /* jslint esnext: true */
33
+ var compiler_1 = require("./compiler");
34
+ // -- MessageFormat --------------------------------------------------------
35
+ function resolveLocale(locales) {
36
+ if (typeof locales === 'string') {
37
+ locales = [locales];
38
+ }
39
+ try {
40
+ return Intl.NumberFormat.supportedLocalesOf(locales, {
41
+ // IE11 localeMatcher `lookup` seems to convert `en` -> `en-US`
42
+ // but not other browsers,
43
+ localeMatcher: 'best fit'
44
+ })[0];
45
+ }
46
+ catch (e) {
47
+ return IntlMessageFormat.defaultLocale;
48
+ }
49
+ }
50
+ function formatPatterns(pattern, values) {
51
+ var result = '';
52
+ for (var _i = 0, pattern_1 = pattern; _i < pattern_1.length; _i++) {
53
+ var part = pattern_1[_i];
54
+ // Exist early for string parts.
55
+ if (typeof part === 'string') {
56
+ result += part;
57
+ continue;
58
+ }
59
+ var id = part.id;
60
+ // Enforce that all required values are provided by the caller.
61
+ if (!(values && id in values)) {
62
+ throw new FormatError("A value must be provided for: " + id, id);
63
+ }
64
+ var value = values[id];
65
+ // Recursively format plural and select parts' option — which can be a
66
+ // nested pattern structure. The choosing of the option to use is
67
+ // abstracted-by and delegated-to the part helper object.
68
+ if (compiler_1.isSelectOrPluralFormat(part)) {
69
+ result += formatPatterns(part.getOption(value), values);
70
+ }
71
+ else {
72
+ result += part.format(value);
73
+ }
74
+ }
75
+ return result;
76
+ }
77
+ function mergeConfig(c1, c2) {
78
+ if (!c2) {
79
+ return c1;
80
+ }
81
+ return __assign({}, (c1 || {}), (c2 || {}), Object.keys(c1).reduce(function (all, k) {
82
+ all[k] = __assign({}, c1[k], (c2[k] || {}));
83
+ return all;
84
+ }, {}));
85
+ }
86
+ function mergeConfigs(defaultConfig, configs) {
87
+ if (!configs) {
88
+ return defaultConfig;
89
+ }
90
+ return Object.keys(defaultConfig).reduce(function (all, k) {
91
+ all[k] = mergeConfig(defaultConfig[k], configs[k]);
92
+ return all;
93
+ }, __assign({}, defaultConfig));
94
+ }
95
+ var FormatError = /** @class */ (function (_super) {
96
+ __extends(FormatError, _super);
97
+ function FormatError(msg, variableId) {
98
+ var _this = _super.call(this, msg) || this;
99
+ _this.variableId = variableId;
100
+ return _this;
101
+ }
102
+ return FormatError;
103
+ }(Error));
104
+ function createDefaultFormatters() {
105
+ return {
106
+ getNumberFormat: function () {
107
+ var _a;
108
+ var args = [];
109
+ for (var _i = 0; _i < arguments.length; _i++) {
110
+ args[_i] = arguments[_i];
111
+ }
112
+ return new ((_a = Intl.NumberFormat).bind.apply(_a, [void 0].concat(args)))();
113
+ },
114
+ getDateTimeFormat: function () {
115
+ var _a;
116
+ var args = [];
117
+ for (var _i = 0; _i < arguments.length; _i++) {
118
+ args[_i] = arguments[_i];
119
+ }
120
+ return new ((_a = Intl.DateTimeFormat).bind.apply(_a, [void 0].concat(args)))();
121
+ },
122
+ getPluralRules: function () {
123
+ var _a;
124
+ var args = [];
125
+ for (var _i = 0; _i < arguments.length; _i++) {
126
+ args[_i] = arguments[_i];
127
+ }
128
+ return new ((_a = Intl.PluralRules).bind.apply(_a, [void 0].concat(args)))();
129
+ }
130
+ };
131
+ }
132
+ exports.createDefaultFormatters = createDefaultFormatters;
133
+ var IntlMessageFormat = /** @class */ (function () {
134
+ function IntlMessageFormat(message, locales, overrideFormats, opts) {
135
+ var _this = this;
136
+ if (locales === void 0) { locales = IntlMessageFormat.defaultLocale; }
137
+ this.format = function (values) {
138
+ try {
139
+ return formatPatterns(_this.pattern, values);
140
+ }
141
+ catch (e) {
142
+ if (e.variableId) {
143
+ throw new Error("The intl string context variable '" + e.variableId + "' was not provided to the string '" + _this.message + "'");
144
+ }
145
+ else {
146
+ throw e;
147
+ }
148
+ }
149
+ };
150
+ if (typeof message === 'string') {
151
+ if (!IntlMessageFormat.__parse) {
152
+ throw new TypeError('IntlMessageFormat.__parse must be set to process `message` of type `string`');
153
+ }
154
+ // Parse string messages into an AST.
155
+ this.ast = IntlMessageFormat.__parse(message);
156
+ }
157
+ else {
158
+ this.ast = message;
159
+ }
160
+ this.message = message;
161
+ if (!(this.ast && this.ast.type === 'messageFormatPattern')) {
162
+ throw new TypeError('A message must be provided as a String or AST.');
163
+ }
164
+ // Creates a new object with the specified `formats` merged with the default
165
+ // formats.
166
+ var formats = mergeConfigs(IntlMessageFormat.formats, overrideFormats);
167
+ // Defined first because it's used to build the format pattern.
168
+ this.locale = resolveLocale(locales || []);
169
+ var formatters = (opts && opts.formatters) || createDefaultFormatters();
170
+ // Compile the `ast` to a pattern that is highly optimized for repeated
171
+ // `format()` invocations. **Note:** This passes the `locales` set provided
172
+ // to the constructor instead of just the resolved locale.
173
+ this.pattern = new compiler_1.default(locales, formats, formatters).compile(this.ast);
174
+ // "Bind" `format()` method to `this` so it can be passed by reference like
175
+ // the other `Intl` APIs.
176
+ }
177
+ IntlMessageFormat.prototype.resolvedOptions = function () {
178
+ return { locale: this.locale };
179
+ };
180
+ IntlMessageFormat.prototype.getAst = function () {
181
+ return this.ast;
182
+ };
183
+ IntlMessageFormat.defaultLocale = 'en';
184
+ IntlMessageFormat.__parse = undefined;
185
+ // Default format options used as the prototype of the `formats` provided to the
186
+ // constructor. These are used when constructing the internal Intl.NumberFormat
187
+ // and Intl.DateTimeFormat instances.
188
+ IntlMessageFormat.formats = {
189
+ number: {
190
+ currency: {
191
+ style: 'currency'
192
+ },
193
+ percent: {
194
+ style: 'percent'
195
+ }
196
+ },
197
+ date: {
198
+ short: {
199
+ month: 'numeric',
200
+ day: 'numeric',
201
+ year: '2-digit'
202
+ },
203
+ medium: {
204
+ month: 'short',
205
+ day: 'numeric',
206
+ year: 'numeric'
207
+ },
208
+ long: {
209
+ month: 'long',
210
+ day: 'numeric',
211
+ year: 'numeric'
212
+ },
213
+ full: {
214
+ weekday: 'long',
215
+ month: 'long',
216
+ day: 'numeric',
217
+ year: 'numeric'
218
+ }
219
+ },
220
+ time: {
221
+ short: {
222
+ hour: 'numeric',
223
+ minute: 'numeric'
224
+ },
225
+ medium: {
226
+ hour: 'numeric',
227
+ minute: 'numeric',
228
+ second: 'numeric'
229
+ },
230
+ long: {
231
+ hour: 'numeric',
232
+ minute: 'numeric',
233
+ second: 'numeric',
234
+ timeZoneName: 'short'
235
+ },
236
+ full: {
237
+ hour: 'numeric',
238
+ minute: 'numeric',
239
+ second: 'numeric',
240
+ timeZoneName: 'short'
241
+ }
242
+ }
243
+ };
244
+ return IntlMessageFormat;
245
+ }());
246
+ exports.IntlMessageFormat = IntlMessageFormat;
247
+ exports.default = IntlMessageFormat;
248
+ //# 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,uCAKoB;AAGpB,4EAA4E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d,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;AA1IY,8CAAiB;AA6I9B,kBAAe,iBAAiB,CAAC"}
package/dist/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/dist/index.js CHANGED
@@ -4,204 +4,13 @@ Copyright (c) 2014, Yahoo! Inc. All rights reserved.
4
4
  Copyrights licensed under the New BSD License.
5
5
  See the accompanying LICENSE file for terms.
6
6
  */
7
- var __extends = (this && this.__extends) || (function () {
8
- var extendStatics = function (d, b) {
9
- extendStatics = Object.setPrototypeOf ||
10
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
12
- return extendStatics(d, b);
13
- };
14
- return function (d, b) {
15
- extendStatics(d, b);
16
- function __() { this.constructor = d; }
17
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
18
- };
19
- })();
20
- var __assign = (this && this.__assign) || function () {
21
- __assign = Object.assign || function(t) {
22
- for (var s, i = 1, n = arguments.length; i < n; i++) {
23
- s = arguments[i];
24
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
25
- t[p] = s[p];
26
- }
27
- return t;
28
- };
29
- return __assign.apply(this, arguments);
30
- };
7
+ function __export(m) {
8
+ for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
9
+ }
31
10
  Object.defineProperty(exports, "__esModule", { value: true });
32
- /* jslint esnext: true */
33
- var compiler_1 = require("./compiler");
34
11
  var intl_messageformat_parser_1 = require("intl-messageformat-parser");
35
- // -- MessageFormat --------------------------------------------------------
36
- function resolveLocale(locales) {
37
- if (typeof locales === 'string') {
38
- locales = [locales];
39
- }
40
- try {
41
- return Intl.NumberFormat.supportedLocalesOf(locales, {
42
- // IE11 localeMatcher `lookup` seems to convert `en` -> `en-US`
43
- // but not other browsers,
44
- localeMatcher: 'best fit'
45
- })[0];
46
- }
47
- catch (e) {
48
- return MessageFormat.defaultLocale;
49
- }
50
- }
51
- function formatPatterns(pattern, values) {
52
- var result = '';
53
- for (var _i = 0, pattern_1 = pattern; _i < pattern_1.length; _i++) {
54
- var part = pattern_1[_i];
55
- // Exist early for string parts.
56
- if (typeof part === 'string') {
57
- result += part;
58
- continue;
59
- }
60
- var id = part.id;
61
- // Enforce that all required values are provided by the caller.
62
- if (!(values && id in values)) {
63
- throw new FormatError("A value must be provided for: " + id, id);
64
- }
65
- var value = values[id];
66
- // Recursively format plural and select parts' option — which can be a
67
- // nested pattern structure. The choosing of the option to use is
68
- // abstracted-by and delegated-to the part helper object.
69
- if (compiler_1.isSelectOrPluralFormat(part)) {
70
- result += formatPatterns(part.getOption(value), values);
71
- }
72
- else {
73
- result += part.format(value);
74
- }
75
- }
76
- return result;
77
- }
78
- function mergeConfig(c1, c2) {
79
- if (!c2) {
80
- return c1;
81
- }
82
- return __assign({}, (c1 || {}), (c2 || {}), Object.keys(c1).reduce(function (all, k) {
83
- all[k] = __assign({}, c1[k], (c2[k] || {}));
84
- return all;
85
- }, {}));
86
- }
87
- function mergeConfigs(defaultConfig, configs) {
88
- if (!configs) {
89
- return defaultConfig;
90
- }
91
- return Object.keys(defaultConfig).reduce(function (all, k) {
92
- all[k] = mergeConfig(defaultConfig[k], configs[k]);
93
- return all;
94
- }, __assign({}, defaultConfig));
95
- }
96
- var FormatError = /** @class */ (function (_super) {
97
- __extends(FormatError, _super);
98
- function FormatError(msg, variableId) {
99
- var _this = _super.call(this, msg) || this;
100
- _this.variableId = variableId;
101
- return _this;
102
- }
103
- return FormatError;
104
- }(Error));
105
- var MessageFormat = (function (message, locales, overrideFormats) {
106
- if (locales === void 0) { locales = MessageFormat.defaultLocale; }
107
- // Parse string messages into an AST.
108
- var ast = typeof message === 'string' ? MessageFormat.__parse(message) : message;
109
- if (!(ast && ast.type === 'messageFormatPattern')) {
110
- throw new TypeError('A message must be provided as a String or AST.');
111
- }
112
- // Creates a new object with the specified `formats` merged with the default
113
- // formats.
114
- var formats = mergeConfigs(MessageFormat.formats, overrideFormats);
115
- // Defined first because it's used to build the format pattern.
116
- var locale = resolveLocale(locales || []);
117
- // Compile the `ast` to a pattern that is highly optimized for repeated
118
- // `format()` invocations. **Note:** This passes the `locales` set provided
119
- // to the constructor instead of just the resolved locale.
120
- var pattern = new compiler_1.default(locales, formats).compile(ast);
121
- // "Bind" `format()` method to `this` so it can be passed by reference like
122
- // the other `Intl` APIs.
123
- return {
124
- format: function (values) {
125
- try {
126
- return formatPatterns(pattern, values);
127
- }
128
- catch (e) {
129
- if (e.variableId) {
130
- throw new Error("The intl string context variable '" + e.variableId + "' was not provided to the string '" + message + "'");
131
- }
132
- else {
133
- throw e;
134
- }
135
- }
136
- },
137
- resolvedOptions: function () {
138
- return { locale: locale };
139
- },
140
- getAst: function () {
141
- return ast;
142
- }
143
- };
144
- });
145
- MessageFormat.defaultLocale = 'en';
146
- // Default format options used as the prototype of the `formats` provided to the
147
- // constructor. These are used when constructing the internal Intl.NumberFormat
148
- // and Intl.DateTimeFormat instances.
149
- MessageFormat.formats = {
150
- number: {
151
- currency: {
152
- style: 'currency'
153
- },
154
- percent: {
155
- style: 'percent'
156
- }
157
- },
158
- date: {
159
- short: {
160
- month: 'numeric',
161
- day: 'numeric',
162
- year: '2-digit'
163
- },
164
- medium: {
165
- month: 'short',
166
- day: 'numeric',
167
- year: 'numeric'
168
- },
169
- long: {
170
- month: 'long',
171
- day: 'numeric',
172
- year: 'numeric'
173
- },
174
- full: {
175
- weekday: 'long',
176
- month: 'long',
177
- day: 'numeric',
178
- year: 'numeric'
179
- }
180
- },
181
- time: {
182
- short: {
183
- hour: 'numeric',
184
- minute: 'numeric'
185
- },
186
- medium: {
187
- hour: 'numeric',
188
- minute: 'numeric',
189
- second: 'numeric'
190
- },
191
- long: {
192
- hour: 'numeric',
193
- minute: 'numeric',
194
- second: 'numeric',
195
- timeZoneName: 'short'
196
- },
197
- full: {
198
- hour: 'numeric',
199
- minute: 'numeric',
200
- second: 'numeric',
201
- timeZoneName: 'short'
202
- }
203
- }
204
- };
205
- MessageFormat.__parse = intl_messageformat_parser_1.default.parse;
206
- exports.default = MessageFormat;
12
+ var core_1 = require("./core");
13
+ core_1.default.__parse = intl_messageformat_parser_1.default.parse;
14
+ __export(require("./core"));
15
+ exports.default = core_1.default;
207
16
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;EAIE;;;;;;;;;;;;;;;;;;;;;;;;;;AAEF,yBAAyB;AAEzB,uCAAgF;AAChF,uEAAyE;AAEzE,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,aAAa,CAAC,aAAa,CAAC;KACpC;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,MAAM,CAAC,EAAE,CAAC,CAAC;QAEzB,sEAAsE;QACtE,iEAAiE;QACjE,yDAAyD;QACzD,IAAI,iCAAsB,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