UNPKG

172 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3 typeof define === 'function' && define.amd ? define(factory) :
4 (global.IntlPolyfill = factory());
5}(this, function () { 'use strict';
6
7 var babelHelpers = {};
8 babelHelpers.typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
9 return typeof obj;
10 } : function (obj) {
11 return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
12 };
13 babelHelpers;
14
15 var realDefineProp = function () {
16 var sentinel = {};
17 try {
18 Object.defineProperty(sentinel, 'a', {});
19 return 'a' in sentinel;
20 } catch (e) {
21 return false;
22 }
23 }();
24
25 // Need a workaround for getters in ES3
26 var es3 = !realDefineProp && !Object.prototype.__defineGetter__;
27
28 // We use this a lot (and need it for proto-less objects)
29 var hop = Object.prototype.hasOwnProperty;
30
31 // Naive defineProperty for compatibility
32 var defineProperty = realDefineProp ? Object.defineProperty : function (obj, name, desc) {
33 if ('get' in desc && obj.__defineGetter__) obj.__defineGetter__(name, desc.get);else if (!hop.call(obj, name) || 'value' in desc) obj[name] = desc.value;
34 };
35
36 // Array.prototype.indexOf, as good as we need it to be
37 var arrIndexOf = Array.prototype.indexOf || function (search) {
38 /*jshint validthis:true */
39 var t = this;
40 if (!t.length) return -1;
41
42 for (var i = arguments[1] || 0, max = t.length; i < max; i++) {
43 if (t[i] === search) return i;
44 }
45
46 return -1;
47 };
48
49 // Create an object with the specified prototype (2nd arg required for Record)
50 var objCreate = Object.create || function (proto, props) {
51 var obj = void 0;
52
53 function F() {}
54 F.prototype = proto;
55 obj = new F();
56
57 for (var k in props) {
58 if (hop.call(props, k)) defineProperty(obj, k, props[k]);
59 }
60
61 return obj;
62 };
63
64 // Snapshot some (hopefully still) native built-ins
65 var arrSlice = Array.prototype.slice;
66 var arrConcat = Array.prototype.concat;
67 var arrPush = Array.prototype.push;
68 var arrJoin = Array.prototype.join;
69 var arrShift = Array.prototype.shift;
70
71 // Naive Function.prototype.bind for compatibility
72 var fnBind = Function.prototype.bind || function (thisObj) {
73 var fn = this,
74 args = arrSlice.call(arguments, 1);
75
76 // All our (presently) bound functions have either 1 or 0 arguments. By returning
77 // different function signatures, we can pass some tests in ES3 environments
78 if (fn.length === 1) {
79 return function () {
80 return fn.apply(thisObj, arrConcat.call(args, arrSlice.call(arguments)));
81 };
82 }
83 return function () {
84 return fn.apply(thisObj, arrConcat.call(args, arrSlice.call(arguments)));
85 };
86 };
87
88 // Object housing internal properties for constructors
89 var internals = objCreate(null);
90
91 // Keep internal properties internal
92 var secret = Math.random();
93
94 // Helper functions
95 // ================
96
97 /**
98 * A function to deal with the inaccuracy of calculating log10 in pre-ES6
99 * JavaScript environments. Math.log(num) / Math.LN10 was responsible for
100 * causing issue #62.
101 */
102 function log10Floor(n) {
103 // ES6 provides the more accurate Math.log10
104 if (typeof Math.log10 === 'function') return Math.floor(Math.log10(n));
105
106 var x = Math.round(Math.log(n) * Math.LOG10E);
107 return x - (Number('1e' + x) > n);
108 }
109
110 /**
111 * A map that doesn't contain Object in its prototype chain
112 */
113 function Record(obj) {
114 // Copy only own properties over unless this object is already a Record instance
115 for (var k in obj) {
116 if (obj instanceof Record || hop.call(obj, k)) defineProperty(this, k, { value: obj[k], enumerable: true, writable: true, configurable: true });
117 }
118 }
119 Record.prototype = objCreate(null);
120
121 /**
122 * An ordered list
123 */
124 function List() {
125 defineProperty(this, 'length', { writable: true, value: 0 });
126
127 if (arguments.length) arrPush.apply(this, arrSlice.call(arguments));
128 }
129 List.prototype = objCreate(null);
130
131 /**
132 * Constructs a regular expression to restore tainted RegExp properties
133 */
134 function createRegExpRestore() {
135 var esc = /[.?*+^$[\]\\(){}|-]/g,
136 lm = RegExp.lastMatch || '',
137 ml = RegExp.multiline ? 'm' : '',
138 ret = { input: RegExp.input },
139 reg = new List(),
140 has = false,
141 cap = {};
142
143 // Create a snapshot of all the 'captured' properties
144 for (var i = 1; i <= 9; i++) {
145 has = (cap['$' + i] = RegExp['$' + i]) || has;
146 } // Now we've snapshotted some properties, escape the lastMatch string
147 lm = lm.replace(esc, '\\$&');
148
149 // If any of the captured strings were non-empty, iterate over them all
150 if (has) {
151 for (var _i = 1; _i <= 9; _i++) {
152 var m = cap['$' + _i];
153
154 // If it's empty, add an empty capturing group
155 if (!m) lm = '()' + lm;
156
157 // Else find the string in lm and escape & wrap it to capture it
158 else {
159 m = m.replace(esc, '\\$&');
160 lm = lm.replace(m, '(' + m + ')');
161 }
162
163 // Push it to the reg and chop lm to make sure further groups come after
164 arrPush.call(reg, lm.slice(0, lm.indexOf('(') + 1));
165 lm = lm.slice(lm.indexOf('(') + 1);
166 }
167 }
168
169 // Create the regular expression that will reconstruct the RegExp properties
170 ret.exp = new RegExp(arrJoin.call(reg, '') + lm, ml);
171
172 return ret;
173 }
174
175 /**
176 * Mimics ES5's abstract ToObject() function
177 */
178 function toObject(arg) {
179 if (arg === null) throw new TypeError('Cannot convert null or undefined to object');
180
181 return Object(arg);
182 }
183
184 /**
185 * Returns "internal" properties for an object
186 */
187 function getInternalProperties(obj) {
188 if (hop.call(obj, '__getInternalProperties')) return obj.__getInternalProperties(secret);
189
190 return objCreate(null);
191 }
192
193 /**
194 * Defines regular expressions for various operations related to the BCP 47 syntax,
195 * as defined at http://tools.ietf.org/html/bcp47#section-2.1
196 */
197
198 // extlang = 3ALPHA ; selected ISO 639 codes
199 // *2("-" 3ALPHA) ; permanently reserved
200 var extlang = '[a-z]{3}(?:-[a-z]{3}){0,2}';
201
202 // language = 2*3ALPHA ; shortest ISO 639 code
203 // ["-" extlang] ; sometimes followed by
204 // ; extended language subtags
205 // / 4ALPHA ; or reserved for future use
206 // / 5*8ALPHA ; or registered language subtag
207 var language = '(?:[a-z]{2,3}(?:-' + extlang + ')?|[a-z]{4}|[a-z]{5,8})';
208
209 // script = 4ALPHA ; ISO 15924 code
210 var script = '[a-z]{4}';
211
212 // region = 2ALPHA ; ISO 3166-1 code
213 // / 3DIGIT ; UN M.49 code
214 var region = '(?:[a-z]{2}|\\d{3})';
215
216 // variant = 5*8alphanum ; registered variants
217 // / (DIGIT 3alphanum)
218 var variant = '(?:[a-z0-9]{5,8}|\\d[a-z0-9]{3})';
219
220 // ; Single alphanumerics
221 // ; "x" reserved for private use
222 // singleton = DIGIT ; 0 - 9
223 // / %x41-57 ; A - W
224 // / %x59-5A ; Y - Z
225 // / %x61-77 ; a - w
226 // / %x79-7A ; y - z
227 var singleton = '[0-9a-wy-z]';
228
229 // extension = singleton 1*("-" (2*8alphanum))
230 var extension = singleton + '(?:-[a-z0-9]{2,8})+';
231
232 // privateuse = "x" 1*("-" (1*8alphanum))
233 var privateuse = 'x(?:-[a-z0-9]{1,8})+';
234
235 // irregular = "en-GB-oed" ; irregular tags do not match
236 // / "i-ami" ; the 'langtag' production and
237 // / "i-bnn" ; would not otherwise be
238 // / "i-default" ; considered 'well-formed'
239 // / "i-enochian" ; These tags are all valid,
240 // / "i-hak" ; but most are deprecated
241 // / "i-klingon" ; in favor of more modern
242 // / "i-lux" ; subtags or subtag
243 // / "i-mingo" ; combination
244 // / "i-navajo"
245 // / "i-pwn"
246 // / "i-tao"
247 // / "i-tay"
248 // / "i-tsu"
249 // / "sgn-BE-FR"
250 // / "sgn-BE-NL"
251 // / "sgn-CH-DE"
252 var irregular = '(?:en-GB-oed' + '|i-(?:ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|tao|tay|tsu)' + '|sgn-(?:BE-FR|BE-NL|CH-DE))';
253
254 // regular = "art-lojban" ; these tags match the 'langtag'
255 // / "cel-gaulish" ; production, but their subtags
256 // / "no-bok" ; are not extended language
257 // / "no-nyn" ; or variant subtags: their meaning
258 // / "zh-guoyu" ; is defined by their registration
259 // / "zh-hakka" ; and all of these are deprecated
260 // / "zh-min" ; in favor of a more modern
261 // / "zh-min-nan" ; subtag or sequence of subtags
262 // / "zh-xiang"
263 var regular = '(?:art-lojban|cel-gaulish|no-bok|no-nyn' + '|zh-(?:guoyu|hakka|min|min-nan|xiang))';
264
265 // grandfathered = irregular ; non-redundant tags registered
266 // / regular ; during the RFC 3066 era
267 var grandfathered = '(?:' + irregular + '|' + regular + ')';
268
269 // langtag = language
270 // ["-" script]
271 // ["-" region]
272 // *("-" variant)
273 // *("-" extension)
274 // ["-" privateuse]
275 var langtag = language + '(?:-' + script + ')?(?:-' + region + ')?(?:-' + variant + ')*(?:-' + extension + ')*(?:-' + privateuse + ')?';
276
277 // Language-Tag = langtag ; normal language tags
278 // / privateuse ; private use tag
279 // / grandfathered ; grandfathered tags
280 var expBCP47Syntax = RegExp('^(?:' + langtag + '|' + privateuse + '|' + grandfathered + ')$', 'i');
281
282 // Match duplicate variants in a language tag
283 var expVariantDupes = RegExp('^(?!x).*?-(' + variant + ')-(?:\\w{4,8}-(?!x-))*\\1\\b', 'i');
284
285 // Match duplicate singletons in a language tag (except in private use)
286 var expSingletonDupes = RegExp('^(?!x).*?-(' + singleton + ')-(?:\\w+-(?!x-))*\\1\\b', 'i');
287
288 // Match all extension sequences
289 var expExtSequences = RegExp('-' + extension, 'ig');
290
291 // Default locale is the first-added locale data for us
292 var defaultLocale = void 0;
293 function setDefaultLocale(locale) {
294 defaultLocale = locale;
295 }
296
297 // IANA Subtag Registry redundant tag and subtag maps
298 var redundantTags = {
299 tags: {
300 "art-lojban": "jbo",
301 "i-ami": "ami",
302 "i-bnn": "bnn",
303 "i-hak": "hak",
304 "i-klingon": "tlh",
305 "i-lux": "lb",
306 "i-navajo": "nv",
307 "i-pwn": "pwn",
308 "i-tao": "tao",
309 "i-tay": "tay",
310 "i-tsu": "tsu",
311 "no-bok": "nb",
312 "no-nyn": "nn",
313 "sgn-BE-FR": "sfb",
314 "sgn-BE-NL": "vgt",
315 "sgn-CH-DE": "sgg",
316 "zh-guoyu": "cmn",
317 "zh-hakka": "hak",
318 "zh-min-nan": "nan",
319 "zh-xiang": "hsn",
320 "sgn-BR": "bzs",
321 "sgn-CO": "csn",
322 "sgn-DE": "gsg",
323 "sgn-DK": "dsl",
324 "sgn-ES": "ssp",
325 "sgn-FR": "fsl",
326 "sgn-GB": "bfi",
327 "sgn-GR": "gss",
328 "sgn-IE": "isg",
329 "sgn-IT": "ise",
330 "sgn-JP": "jsl",
331 "sgn-MX": "mfs",
332 "sgn-NI": "ncs",
333 "sgn-NL": "dse",
334 "sgn-NO": "nsl",
335 "sgn-PT": "psr",
336 "sgn-SE": "swl",
337 "sgn-US": "ase",
338 "sgn-ZA": "sfs",
339 "zh-cmn": "cmn",
340 "zh-cmn-Hans": "cmn-Hans",
341 "zh-cmn-Hant": "cmn-Hant",
342 "zh-gan": "gan",
343 "zh-wuu": "wuu",
344 "zh-yue": "yue"
345 },
346 subtags: {
347 BU: "MM",
348 DD: "DE",
349 FX: "FR",
350 TP: "TL",
351 YD: "YE",
352 ZR: "CD",
353 heploc: "alalc97",
354 'in': "id",
355 iw: "he",
356 ji: "yi",
357 jw: "jv",
358 mo: "ro",
359 ayx: "nun",
360 bjd: "drl",
361 ccq: "rki",
362 cjr: "mom",
363 cka: "cmr",
364 cmk: "xch",
365 drh: "khk",
366 drw: "prs",
367 gav: "dev",
368 hrr: "jal",
369 ibi: "opa",
370 kgh: "kml",
371 lcq: "ppr",
372 mst: "mry",
373 myt: "mry",
374 sca: "hle",
375 tie: "ras",
376 tkk: "twm",
377 tlw: "weo",
378 tnf: "prs",
379 ybd: "rki",
380 yma: "lrr"
381 },
382 extLang: {
383 aao: ["aao", "ar"],
384 abh: ["abh", "ar"],
385 abv: ["abv", "ar"],
386 acm: ["acm", "ar"],
387 acq: ["acq", "ar"],
388 acw: ["acw", "ar"],
389 acx: ["acx", "ar"],
390 acy: ["acy", "ar"],
391 adf: ["adf", "ar"],
392 ads: ["ads", "sgn"],
393 aeb: ["aeb", "ar"],
394 aec: ["aec", "ar"],
395 aed: ["aed", "sgn"],
396 aen: ["aen", "sgn"],
397 afb: ["afb", "ar"],
398 afg: ["afg", "sgn"],
399 ajp: ["ajp", "ar"],
400 apc: ["apc", "ar"],
401 apd: ["apd", "ar"],
402 arb: ["arb", "ar"],
403 arq: ["arq", "ar"],
404 ars: ["ars", "ar"],
405 ary: ["ary", "ar"],
406 arz: ["arz", "ar"],
407 ase: ["ase", "sgn"],
408 asf: ["asf", "sgn"],
409 asp: ["asp", "sgn"],
410 asq: ["asq", "sgn"],
411 asw: ["asw", "sgn"],
412 auz: ["auz", "ar"],
413 avl: ["avl", "ar"],
414 ayh: ["ayh", "ar"],
415 ayl: ["ayl", "ar"],
416 ayn: ["ayn", "ar"],
417 ayp: ["ayp", "ar"],
418 bbz: ["bbz", "ar"],
419 bfi: ["bfi", "sgn"],
420 bfk: ["bfk", "sgn"],
421 bjn: ["bjn", "ms"],
422 bog: ["bog", "sgn"],
423 bqn: ["bqn", "sgn"],
424 bqy: ["bqy", "sgn"],
425 btj: ["btj", "ms"],
426 bve: ["bve", "ms"],
427 bvl: ["bvl", "sgn"],
428 bvu: ["bvu", "ms"],
429 bzs: ["bzs", "sgn"],
430 cdo: ["cdo", "zh"],
431 cds: ["cds", "sgn"],
432 cjy: ["cjy", "zh"],
433 cmn: ["cmn", "zh"],
434 coa: ["coa", "ms"],
435 cpx: ["cpx", "zh"],
436 csc: ["csc", "sgn"],
437 csd: ["csd", "sgn"],
438 cse: ["cse", "sgn"],
439 csf: ["csf", "sgn"],
440 csg: ["csg", "sgn"],
441 csl: ["csl", "sgn"],
442 csn: ["csn", "sgn"],
443 csq: ["csq", "sgn"],
444 csr: ["csr", "sgn"],
445 czh: ["czh", "zh"],
446 czo: ["czo", "zh"],
447 doq: ["doq", "sgn"],
448 dse: ["dse", "sgn"],
449 dsl: ["dsl", "sgn"],
450 dup: ["dup", "ms"],
451 ecs: ["ecs", "sgn"],
452 esl: ["esl", "sgn"],
453 esn: ["esn", "sgn"],
454 eso: ["eso", "sgn"],
455 eth: ["eth", "sgn"],
456 fcs: ["fcs", "sgn"],
457 fse: ["fse", "sgn"],
458 fsl: ["fsl", "sgn"],
459 fss: ["fss", "sgn"],
460 gan: ["gan", "zh"],
461 gds: ["gds", "sgn"],
462 gom: ["gom", "kok"],
463 gse: ["gse", "sgn"],
464 gsg: ["gsg", "sgn"],
465 gsm: ["gsm", "sgn"],
466 gss: ["gss", "sgn"],
467 gus: ["gus", "sgn"],
468 hab: ["hab", "sgn"],
469 haf: ["haf", "sgn"],
470 hak: ["hak", "zh"],
471 hds: ["hds", "sgn"],
472 hji: ["hji", "ms"],
473 hks: ["hks", "sgn"],
474 hos: ["hos", "sgn"],
475 hps: ["hps", "sgn"],
476 hsh: ["hsh", "sgn"],
477 hsl: ["hsl", "sgn"],
478 hsn: ["hsn", "zh"],
479 icl: ["icl", "sgn"],
480 ils: ["ils", "sgn"],
481 inl: ["inl", "sgn"],
482 ins: ["ins", "sgn"],
483 ise: ["ise", "sgn"],
484 isg: ["isg", "sgn"],
485 isr: ["isr", "sgn"],
486 jak: ["jak", "ms"],
487 jax: ["jax", "ms"],
488 jcs: ["jcs", "sgn"],
489 jhs: ["jhs", "sgn"],
490 jls: ["jls", "sgn"],
491 jos: ["jos", "sgn"],
492 jsl: ["jsl", "sgn"],
493 jus: ["jus", "sgn"],
494 kgi: ["kgi", "sgn"],
495 knn: ["knn", "kok"],
496 kvb: ["kvb", "ms"],
497 kvk: ["kvk", "sgn"],
498 kvr: ["kvr", "ms"],
499 kxd: ["kxd", "ms"],
500 lbs: ["lbs", "sgn"],
501 lce: ["lce", "ms"],
502 lcf: ["lcf", "ms"],
503 liw: ["liw", "ms"],
504 lls: ["lls", "sgn"],
505 lsg: ["lsg", "sgn"],
506 lsl: ["lsl", "sgn"],
507 lso: ["lso", "sgn"],
508 lsp: ["lsp", "sgn"],
509 lst: ["lst", "sgn"],
510 lsy: ["lsy", "sgn"],
511 ltg: ["ltg", "lv"],
512 lvs: ["lvs", "lv"],
513 lzh: ["lzh", "zh"],
514 max: ["max", "ms"],
515 mdl: ["mdl", "sgn"],
516 meo: ["meo", "ms"],
517 mfa: ["mfa", "ms"],
518 mfb: ["mfb", "ms"],
519 mfs: ["mfs", "sgn"],
520 min: ["min", "ms"],
521 mnp: ["mnp", "zh"],
522 mqg: ["mqg", "ms"],
523 mre: ["mre", "sgn"],
524 msd: ["msd", "sgn"],
525 msi: ["msi", "ms"],
526 msr: ["msr", "sgn"],
527 mui: ["mui", "ms"],
528 mzc: ["mzc", "sgn"],
529 mzg: ["mzg", "sgn"],
530 mzy: ["mzy", "sgn"],
531 nan: ["nan", "zh"],
532 nbs: ["nbs", "sgn"],
533 ncs: ["ncs", "sgn"],
534 nsi: ["nsi", "sgn"],
535 nsl: ["nsl", "sgn"],
536 nsp: ["nsp", "sgn"],
537 nsr: ["nsr", "sgn"],
538 nzs: ["nzs", "sgn"],
539 okl: ["okl", "sgn"],
540 orn: ["orn", "ms"],
541 ors: ["ors", "ms"],
542 pel: ["pel", "ms"],
543 pga: ["pga", "ar"],
544 pks: ["pks", "sgn"],
545 prl: ["prl", "sgn"],
546 prz: ["prz", "sgn"],
547 psc: ["psc", "sgn"],
548 psd: ["psd", "sgn"],
549 pse: ["pse", "ms"],
550 psg: ["psg", "sgn"],
551 psl: ["psl", "sgn"],
552 pso: ["pso", "sgn"],
553 psp: ["psp", "sgn"],
554 psr: ["psr", "sgn"],
555 pys: ["pys", "sgn"],
556 rms: ["rms", "sgn"],
557 rsi: ["rsi", "sgn"],
558 rsl: ["rsl", "sgn"],
559 sdl: ["sdl", "sgn"],
560 sfb: ["sfb", "sgn"],
561 sfs: ["sfs", "sgn"],
562 sgg: ["sgg", "sgn"],
563 sgx: ["sgx", "sgn"],
564 shu: ["shu", "ar"],
565 slf: ["slf", "sgn"],
566 sls: ["sls", "sgn"],
567 sqk: ["sqk", "sgn"],
568 sqs: ["sqs", "sgn"],
569 ssh: ["ssh", "ar"],
570 ssp: ["ssp", "sgn"],
571 ssr: ["ssr", "sgn"],
572 svk: ["svk", "sgn"],
573 swc: ["swc", "sw"],
574 swh: ["swh", "sw"],
575 swl: ["swl", "sgn"],
576 syy: ["syy", "sgn"],
577 tmw: ["tmw", "ms"],
578 tse: ["tse", "sgn"],
579 tsm: ["tsm", "sgn"],
580 tsq: ["tsq", "sgn"],
581 tss: ["tss", "sgn"],
582 tsy: ["tsy", "sgn"],
583 tza: ["tza", "sgn"],
584 ugn: ["ugn", "sgn"],
585 ugy: ["ugy", "sgn"],
586 ukl: ["ukl", "sgn"],
587 uks: ["uks", "sgn"],
588 urk: ["urk", "ms"],
589 uzn: ["uzn", "uz"],
590 uzs: ["uzs", "uz"],
591 vgt: ["vgt", "sgn"],
592 vkk: ["vkk", "ms"],
593 vkt: ["vkt", "ms"],
594 vsi: ["vsi", "sgn"],
595 vsl: ["vsl", "sgn"],
596 vsv: ["vsv", "sgn"],
597 wuu: ["wuu", "zh"],
598 xki: ["xki", "sgn"],
599 xml: ["xml", "sgn"],
600 xmm: ["xmm", "ms"],
601 xms: ["xms", "sgn"],
602 yds: ["yds", "sgn"],
603 ysl: ["ysl", "sgn"],
604 yue: ["yue", "zh"],
605 zib: ["zib", "sgn"],
606 zlm: ["zlm", "ms"],
607 zmi: ["zmi", "ms"],
608 zsl: ["zsl", "sgn"],
609 zsm: ["zsm", "ms"]
610 }
611 };
612
613 /**
614 * Convert only a-z to uppercase as per section 6.1 of the spec
615 */
616 function toLatinUpperCase(str) {
617 var i = str.length;
618
619 while (i--) {
620 var ch = str.charAt(i);
621
622 if (ch >= "a" && ch <= "z") str = str.slice(0, i) + ch.toUpperCase() + str.slice(i + 1);
623 }
624
625 return str;
626 }
627
628 /**
629 * The IsStructurallyValidLanguageTag abstract operation verifies that the locale
630 * argument (which must be a String value)
631 *
632 * - represents a well-formed BCP 47 language tag as specified in RFC 5646 section
633 * 2.1, or successor,
634 * - does not include duplicate variant subtags, and
635 * - does not include duplicate singleton subtags.
636 *
637 * The abstract operation returns true if locale can be generated from the ABNF
638 * grammar in section 2.1 of the RFC, starting with Language-Tag, and does not
639 * contain duplicate variant or singleton subtags (other than as a private use
640 * subtag). It returns false otherwise. Terminal value characters in the grammar are
641 * interpreted as the Unicode equivalents of the ASCII octet values given.
642 */
643 function /* 6.2.2 */IsStructurallyValidLanguageTag(locale) {
644 // represents a well-formed BCP 47 language tag as specified in RFC 5646
645 if (!expBCP47Syntax.test(locale)) return false;
646
647 // does not include duplicate variant subtags, and
648 if (expVariantDupes.test(locale)) return false;
649
650 // does not include duplicate singleton subtags.
651 if (expSingletonDupes.test(locale)) return false;
652
653 return true;
654 }
655
656 /**
657 * The CanonicalizeLanguageTag abstract operation returns the canonical and case-
658 * regularized form of the locale argument (which must be a String value that is
659 * a structurally valid BCP 47 language tag as verified by the
660 * IsStructurallyValidLanguageTag abstract operation). It takes the steps
661 * specified in RFC 5646 section 4.5, or successor, to bring the language tag
662 * into canonical form, and to regularize the case of the subtags, but does not
663 * take the steps to bring a language tag into “extlang form” and to reorder
664 * variant subtags.
665
666 * The specifications for extensions to BCP 47 language tags, such as RFC 6067,
667 * may include canonicalization rules for the extension subtag sequences they
668 * define that go beyond the canonicalization rules of RFC 5646 section 4.5.
669 * Implementations are allowed, but not required, to apply these additional rules.
670 */
671 function /* 6.2.3 */CanonicalizeLanguageTag(locale) {
672 var match = void 0,
673 parts = void 0;
674
675 // A language tag is in 'canonical form' when the tag is well-formed
676 // according to the rules in Sections 2.1 and 2.2
677
678 // Section 2.1 says all subtags use lowercase...
679 locale = locale.toLowerCase();
680
681 // ...with 2 exceptions: 'two-letter and four-letter subtags that neither
682 // appear at the start of the tag nor occur after singletons. Such two-letter
683 // subtags are all uppercase (as in the tags "en-CA-x-ca" or "sgn-BE-FR") and
684 // four-letter subtags are titlecase (as in the tag "az-Latn-x-latn").
685 parts = locale.split('-');
686 for (var i = 1, max = parts.length; i < max; i++) {
687 // Two-letter subtags are all uppercase
688 if (parts[i].length === 2) parts[i] = parts[i].toUpperCase();
689
690 // Four-letter subtags are titlecase
691 else if (parts[i].length === 4) parts[i] = parts[i].charAt(0).toUpperCase() + parts[i].slice(1);
692
693 // Is it a singleton?
694 else if (parts[i].length === 1 && parts[i] !== 'x') break;
695 }
696 locale = arrJoin.call(parts, '-');
697
698 // The steps laid out in RFC 5646 section 4.5 are as follows:
699
700 // 1. Extension sequences are ordered into case-insensitive ASCII order
701 // by singleton subtag.
702 if ((match = locale.match(expExtSequences)) && match.length > 1) {
703 // The built-in sort() sorts by ASCII order, so use that
704 match.sort();
705
706 // Replace all extensions with the joined, sorted array
707 locale = locale.replace(RegExp('(?:' + expExtSequences.source + ')+', 'i'), arrJoin.call(match, ''));
708 }
709
710 // 2. Redundant or grandfathered tags are replaced by their 'Preferred-
711 // Value', if there is one.
712 if (hop.call(redundantTags.tags, locale)) locale = redundantTags.tags[locale];
713
714 // 3. Subtags are replaced by their 'Preferred-Value', if there is one.
715 // For extlangs, the original primary language subtag is also
716 // replaced if there is a primary language subtag in the 'Preferred-
717 // Value'.
718 parts = locale.split('-');
719
720 for (var _i = 1, _max = parts.length; _i < _max; _i++) {
721 if (hop.call(redundantTags.subtags, parts[_i])) parts[_i] = redundantTags.subtags[parts[_i]];else if (hop.call(redundantTags.extLang, parts[_i])) {
722 parts[_i] = redundantTags.extLang[parts[_i]][0];
723
724 // For extlang tags, the prefix needs to be removed if it is redundant
725 if (_i === 1 && redundantTags.extLang[parts[1]][1] === parts[0]) {
726 parts = arrSlice.call(parts, _i++);
727 _max -= 1;
728 }
729 }
730 }
731
732 return arrJoin.call(parts, '-');
733 }
734
735 /**
736 * The DefaultLocale abstract operation returns a String value representing the
737 * structurally valid (6.2.2) and canonicalized (6.2.3) BCP 47 language tag for the
738 * host environment’s current locale.
739 */
740 function /* 6.2.4 */DefaultLocale() {
741 return defaultLocale;
742 }
743
744 // Sect 6.3 Currency Codes
745 // =======================
746
747 var expCurrencyCode = /^[A-Z]{3}$/;
748
749 /**
750 * The IsWellFormedCurrencyCode abstract operation verifies that the currency argument
751 * (after conversion to a String value) represents a well-formed 3-letter ISO currency
752 * code. The following steps are taken:
753 */
754 function /* 6.3.1 */IsWellFormedCurrencyCode(currency) {
755 // 1. Let `c` be ToString(currency)
756 var c = String(currency);
757
758 // 2. Let `normalized` be the result of mapping c to upper case as described
759 // in 6.1.
760 var normalized = toLatinUpperCase(c);
761
762 // 3. If the string length of normalized is not 3, return false.
763 // 4. If normalized contains any character that is not in the range "A" to "Z"
764 // (U+0041 to U+005A), return false.
765 if (expCurrencyCode.test(normalized) === false) return false;
766
767 // 5. Return true
768 return true;
769 }
770
771 var expUnicodeExSeq = /-u(?:-[0-9a-z]{2,8})+/gi; // See `extension` below
772
773 function /* 9.2.1 */CanonicalizeLocaleList(locales) {
774 // The abstract operation CanonicalizeLocaleList takes the following steps:
775
776 // 1. If locales is undefined, then a. Return a new empty List
777 if (locales === undefined) return new List();
778
779 // 2. Let seen be a new empty List.
780 var seen = new List();
781
782 // 3. If locales is a String value, then
783 // a. Let locales be a new array created as if by the expression new
784 // Array(locales) where Array is the standard built-in constructor with
785 // that name and locales is the value of locales.
786 locales = typeof locales === 'string' ? [locales] : locales;
787
788 // 4. Let O be ToObject(locales).
789 var O = toObject(locales);
790
791 // 5. Let lenValue be the result of calling the [[Get]] internal method of
792 // O with the argument "length".
793 // 6. Let len be ToUint32(lenValue).
794 var len = O.length;
795
796 // 7. Let k be 0.
797 var k = 0;
798
799 // 8. Repeat, while k < len
800 while (k < len) {
801 // a. Let Pk be ToString(k).
802 var Pk = String(k);
803
804 // b. Let kPresent be the result of calling the [[HasProperty]] internal
805 // method of O with argument Pk.
806 var kPresent = Pk in O;
807
808 // c. If kPresent is true, then
809 if (kPresent) {
810 // i. Let kValue be the result of calling the [[Get]] internal
811 // method of O with argument Pk.
812 var kValue = O[Pk];
813
814 // ii. If the type of kValue is not String or Object, then throw a
815 // TypeError exception.
816 if (kValue === null || typeof kValue !== 'string' && (typeof kValue === "undefined" ? "undefined" : babelHelpers["typeof"](kValue)) !== 'object') throw new TypeError('String or Object type expected');
817
818 // iii. Let tag be ToString(kValue).
819 var tag = String(kValue);
820
821 // iv. If the result of calling the abstract operation
822 // IsStructurallyValidLanguageTag (defined in 6.2.2), passing tag as
823 // the argument, is false, then throw a RangeError exception.
824 if (!IsStructurallyValidLanguageTag(tag)) throw new RangeError("'" + tag + "' is not a structurally valid language tag");
825
826 // v. Let tag be the result of calling the abstract operation
827 // CanonicalizeLanguageTag (defined in 6.2.3), passing tag as the
828 // argument.
829 tag = CanonicalizeLanguageTag(tag);
830
831 // vi. If tag is not an element of seen, then append tag as the last
832 // element of seen.
833 if (arrIndexOf.call(seen, tag) === -1) arrPush.call(seen, tag);
834 }
835
836 // d. Increase k by 1.
837 k++;
838 }
839
840 // 9. Return seen.
841 return seen;
842 }
843
844 /**
845 * The BestAvailableLocale abstract operation compares the provided argument
846 * locale, which must be a String value with a structurally valid and
847 * canonicalized BCP 47 language tag, against the locales in availableLocales and
848 * returns either the longest non-empty prefix of locale that is an element of
849 * availableLocales, or undefined if there is no such element. It uses the
850 * fallback mechanism of RFC 4647, section 3.4. The following steps are taken:
851 */
852 function /* 9.2.2 */BestAvailableLocale(availableLocales, locale) {
853 // 1. Let candidate be locale
854 var candidate = locale;
855
856 // 2. Repeat
857 while (candidate) {
858 // a. If availableLocales contains an element equal to candidate, then return
859 // candidate.
860 if (arrIndexOf.call(availableLocales, candidate) > -1) return candidate;
861
862 // b. Let pos be the character index of the last occurrence of "-"
863 // (U+002D) within candidate. If that character does not occur, return
864 // undefined.
865 var pos = candidate.lastIndexOf('-');
866
867 if (pos < 0) return;
868
869 // c. If pos ≥ 2 and the character "-" occurs at index pos-2 of candidate,
870 // then decrease pos by 2.
871 if (pos >= 2 && candidate.charAt(pos - 2) === '-') pos -= 2;
872
873 // d. Let candidate be the substring of candidate from position 0, inclusive,
874 // to position pos, exclusive.
875 candidate = candidate.substring(0, pos);
876 }
877 }
878
879 /**
880 * The LookupMatcher abstract operation compares requestedLocales, which must be
881 * a List as returned by CanonicalizeLocaleList, against the locales in
882 * availableLocales and determines the best available language to meet the
883 * request. The following steps are taken:
884 */
885 function /* 9.2.3 */LookupMatcher(availableLocales, requestedLocales) {
886 // 1. Let i be 0.
887 var i = 0;
888
889 // 2. Let len be the number of elements in requestedLocales.
890 var len = requestedLocales.length;
891
892 // 3. Let availableLocale be undefined.
893 var availableLocale = void 0;
894
895 var locale = void 0,
896 noExtensionsLocale = void 0;
897
898 // 4. Repeat while i < len and availableLocale is undefined:
899 while (i < len && !availableLocale) {
900 // a. Let locale be the element of requestedLocales at 0-origined list
901 // position i.
902 locale = requestedLocales[i];
903
904 // b. Let noExtensionsLocale be the String value that is locale with all
905 // Unicode locale extension sequences removed.
906 noExtensionsLocale = String(locale).replace(expUnicodeExSeq, '');
907
908 // c. Let availableLocale be the result of calling the
909 // BestAvailableLocale abstract operation (defined in 9.2.2) with
910 // arguments availableLocales and noExtensionsLocale.
911 availableLocale = BestAvailableLocale(availableLocales, noExtensionsLocale);
912
913 // d. Increase i by 1.
914 i++;
915 }
916
917 // 5. Let result be a new Record.
918 var result = new Record();
919
920 // 6. If availableLocale is not undefined, then
921 if (availableLocale !== undefined) {
922 // a. Set result.[[locale]] to availableLocale.
923 result['[[locale]]'] = availableLocale;
924
925 // b. If locale and noExtensionsLocale are not the same String value, then
926 if (String(locale) !== String(noExtensionsLocale)) {
927 // i. Let extension be the String value consisting of the first
928 // substring of locale that is a Unicode locale extension sequence.
929 var extension = locale.match(expUnicodeExSeq)[0];
930
931 // ii. Let extensionIndex be the character position of the initial
932 // "-" of the first Unicode locale extension sequence within locale.
933 var extensionIndex = locale.indexOf('-u-');
934
935 // iii. Set result.[[extension]] to extension.
936 result['[[extension]]'] = extension;
937
938 // iv. Set result.[[extensionIndex]] to extensionIndex.
939 result['[[extensionIndex]]'] = extensionIndex;
940 }
941 }
942 // 7. Else
943 else
944 // a. Set result.[[locale]] to the value returned by the DefaultLocale abstract
945 // operation (defined in 6.2.4).
946 result['[[locale]]'] = DefaultLocale();
947
948 // 8. Return result
949 return result;
950 }
951
952 /**
953 * The BestFitMatcher abstract operation compares requestedLocales, which must be
954 * a List as returned by CanonicalizeLocaleList, against the locales in
955 * availableLocales and determines the best available language to meet the
956 * request. The algorithm is implementation dependent, but should produce results
957 * that a typical user of the requested locales would perceive as at least as
958 * good as those produced by the LookupMatcher abstract operation. Options
959 * specified through Unicode locale extension sequences must be ignored by the
960 * algorithm. Information about such subsequences is returned separately.
961 * The abstract operation returns a record with a [[locale]] field, whose value
962 * is the language tag of the selected locale, which must be an element of
963 * availableLocales. If the language tag of the request locale that led to the
964 * selected locale contained a Unicode locale extension sequence, then the
965 * returned record also contains an [[extension]] field whose value is the first
966 * Unicode locale extension sequence, and an [[extensionIndex]] field whose value
967 * is the index of the first Unicode locale extension sequence within the request
968 * locale language tag.
969 */
970 function /* 9.2.4 */BestFitMatcher(availableLocales, requestedLocales) {
971 return LookupMatcher(availableLocales, requestedLocales);
972 }
973
974 /**
975 * The ResolveLocale abstract operation compares a BCP 47 language priority list
976 * requestedLocales against the locales in availableLocales and determines the
977 * best available language to meet the request. availableLocales and
978 * requestedLocales must be provided as List values, options as a Record.
979 */
980 function /* 9.2.5 */ResolveLocale(availableLocales, requestedLocales, options, relevantExtensionKeys, localeData) {
981 if (availableLocales.length === 0) {
982 throw new ReferenceError('No locale data has been provided for this object yet.');
983 }
984
985 // The following steps are taken:
986 // 1. Let matcher be the value of options.[[localeMatcher]].
987 var matcher = options['[[localeMatcher]]'];
988
989 var r = void 0;
990
991 // 2. If matcher is "lookup", then
992 if (matcher === 'lookup')
993 // a. Let r be the result of calling the LookupMatcher abstract operation
994 // (defined in 9.2.3) with arguments availableLocales and
995 // requestedLocales.
996 r = LookupMatcher(availableLocales, requestedLocales);
997
998 // 3. Else
999 else
1000 // a. Let r be the result of calling the BestFitMatcher abstract
1001 // operation (defined in 9.2.4) with arguments availableLocales and
1002 // requestedLocales.
1003 r = BestFitMatcher(availableLocales, requestedLocales);
1004
1005 // 4. Let foundLocale be the value of r.[[locale]].
1006 var foundLocale = r['[[locale]]'];
1007
1008 var extensionSubtags = void 0,
1009 extensionSubtagsLength = void 0;
1010
1011 // 5. If r has an [[extension]] field, then
1012 if (hop.call(r, '[[extension]]')) {
1013 // a. Let extension be the value of r.[[extension]].
1014 var extension = r['[[extension]]'];
1015 // b. Let split be the standard built-in function object defined in ES5,
1016 // 15.5.4.14.
1017 var split = String.prototype.split;
1018 // c. Let extensionSubtags be the result of calling the [[Call]] internal
1019 // method of split with extension as the this value and an argument
1020 // list containing the single item "-".
1021 extensionSubtags = split.call(extension, '-');
1022 // d. Let extensionSubtagsLength be the result of calling the [[Get]]
1023 // internal method of extensionSubtags with argument "length".
1024 extensionSubtagsLength = extensionSubtags.length;
1025 }
1026
1027 // 6. Let result be a new Record.
1028 var result = new Record();
1029
1030 // 7. Set result.[[dataLocale]] to foundLocale.
1031 result['[[dataLocale]]'] = foundLocale;
1032
1033 // 8. Let supportedExtension be "-u".
1034 var supportedExtension = '-u';
1035 // 9. Let i be 0.
1036 var i = 0;
1037 // 10. Let len be the result of calling the [[Get]] internal method of
1038 // relevantExtensionKeys with argument "length".
1039 var len = relevantExtensionKeys.length;
1040
1041 // 11 Repeat while i < len:
1042 while (i < len) {
1043 // a. Let key be the result of calling the [[Get]] internal method of
1044 // relevantExtensionKeys with argument ToString(i).
1045 var key = relevantExtensionKeys[i];
1046 // b. Let foundLocaleData be the result of calling the [[Get]] internal
1047 // method of localeData with the argument foundLocale.
1048 var foundLocaleData = localeData[foundLocale];
1049 // c. Let keyLocaleData be the result of calling the [[Get]] internal
1050 // method of foundLocaleData with the argument key.
1051 var keyLocaleData = foundLocaleData[key];
1052 // d. Let value be the result of calling the [[Get]] internal method of
1053 // keyLocaleData with argument "0".
1054 var value = keyLocaleData['0'];
1055 // e. Let supportedExtensionAddition be "".
1056 var supportedExtensionAddition = '';
1057 // f. Let indexOf be the standard built-in function object defined in
1058 // ES5, 15.4.4.14.
1059 var indexOf = arrIndexOf;
1060
1061 // g. If extensionSubtags is not undefined, then
1062 if (extensionSubtags !== undefined) {
1063 // i. Let keyPos be the result of calling the [[Call]] internal
1064 // method of indexOf with extensionSubtags as the this value and
1065 // an argument list containing the single item key.
1066 var keyPos = indexOf.call(extensionSubtags, key);
1067
1068 // ii. If keyPos ≠ -1, then
1069 if (keyPos !== -1) {
1070 // 1. If keyPos + 1 < extensionSubtagsLength and the length of the
1071 // result of calling the [[Get]] internal method of
1072 // extensionSubtags with argument ToString(keyPos +1) is greater
1073 // than 2, then
1074 if (keyPos + 1 < extensionSubtagsLength && extensionSubtags[keyPos + 1].length > 2) {
1075 // a. Let requestedValue be the result of calling the [[Get]]
1076 // internal method of extensionSubtags with argument
1077 // ToString(keyPos + 1).
1078 var requestedValue = extensionSubtags[keyPos + 1];
1079 // b. Let valuePos be the result of calling the [[Call]]
1080 // internal method of indexOf with keyLocaleData as the
1081 // this value and an argument list containing the single
1082 // item requestedValue.
1083 var valuePos = indexOf.call(keyLocaleData, requestedValue);
1084
1085 // c. If valuePos ≠ -1, then
1086 if (valuePos !== -1) {
1087 // i. Let value be requestedValue.
1088 value = requestedValue,
1089 // ii. Let supportedExtensionAddition be the
1090 // concatenation of "-", key, "-", and value.
1091 supportedExtensionAddition = '-' + key + '-' + value;
1092 }
1093 }
1094 // 2. Else
1095 else {
1096 // a. Let valuePos be the result of calling the [[Call]]
1097 // internal method of indexOf with keyLocaleData as the this
1098 // value and an argument list containing the single item
1099 // "true".
1100 var _valuePos = indexOf(keyLocaleData, 'true');
1101
1102 // b. If valuePos ≠ -1, then
1103 if (_valuePos !== -1)
1104 // i. Let value be "true".
1105 value = 'true';
1106 }
1107 }
1108 }
1109 // h. If options has a field [[<key>]], then
1110 if (hop.call(options, '[[' + key + ']]')) {
1111 // i. Let optionsValue be the value of options.[[<key>]].
1112 var optionsValue = options['[[' + key + ']]'];
1113
1114 // ii. If the result of calling the [[Call]] internal method of indexOf
1115 // with keyLocaleData as the this value and an argument list
1116 // containing the single item optionsValue is not -1, then
1117 if (indexOf.call(keyLocaleData, optionsValue) !== -1) {
1118 // 1. If optionsValue is not equal to value, then
1119 if (optionsValue !== value) {
1120 // a. Let value be optionsValue.
1121 value = optionsValue;
1122 // b. Let supportedExtensionAddition be "".
1123 supportedExtensionAddition = '';
1124 }
1125 }
1126 }
1127 // i. Set result.[[<key>]] to value.
1128 result['[[' + key + ']]'] = value;
1129
1130 // j. Append supportedExtensionAddition to supportedExtension.
1131 supportedExtension += supportedExtensionAddition;
1132
1133 // k. Increase i by 1.
1134 i++;
1135 }
1136 // 12. If the length of supportedExtension is greater than 2, then
1137 if (supportedExtension.length > 2) {
1138 // a.
1139 var privateIndex = foundLocale.indexOf("-x-");
1140 // b.
1141 if (privateIndex === -1) {
1142 // i.
1143 foundLocale = foundLocale + supportedExtension;
1144 }
1145 // c.
1146 else {
1147 // i.
1148 var preExtension = foundLocale.substring(0, privateIndex);
1149 // ii.
1150 var postExtension = foundLocale.substring(privateIndex);
1151 // iii.
1152 foundLocale = preExtension + supportedExtension + postExtension;
1153 }
1154 // d. asserting - skipping
1155 // e.
1156 foundLocale = CanonicalizeLanguageTag(foundLocale);
1157 }
1158 // 13. Set result.[[locale]] to foundLocale.
1159 result['[[locale]]'] = foundLocale;
1160
1161 // 14. Return result.
1162 return result;
1163 }
1164
1165 /**
1166 * The LookupSupportedLocales abstract operation returns the subset of the
1167 * provided BCP 47 language priority list requestedLocales for which
1168 * availableLocales has a matching locale when using the BCP 47 Lookup algorithm.
1169 * Locales appear in the same order in the returned list as in requestedLocales.
1170 * The following steps are taken:
1171 */
1172 function /* 9.2.6 */LookupSupportedLocales(availableLocales, requestedLocales) {
1173 // 1. Let len be the number of elements in requestedLocales.
1174 var len = requestedLocales.length;
1175 // 2. Let subset be a new empty List.
1176 var subset = new List();
1177 // 3. Let k be 0.
1178 var k = 0;
1179
1180 // 4. Repeat while k < len
1181 while (k < len) {
1182 // a. Let locale be the element of requestedLocales at 0-origined list
1183 // position k.
1184 var locale = requestedLocales[k];
1185 // b. Let noExtensionsLocale be the String value that is locale with all
1186 // Unicode locale extension sequences removed.
1187 var noExtensionsLocale = String(locale).replace(expUnicodeExSeq, '');
1188 // c. Let availableLocale be the result of calling the
1189 // BestAvailableLocale abstract operation (defined in 9.2.2) with
1190 // arguments availableLocales and noExtensionsLocale.
1191 var availableLocale = BestAvailableLocale(availableLocales, noExtensionsLocale);
1192
1193 // d. If availableLocale is not undefined, then append locale to the end of
1194 // subset.
1195 if (availableLocale !== undefined) arrPush.call(subset, locale);
1196
1197 // e. Increment k by 1.
1198 k++;
1199 }
1200
1201 // 5. Let subsetArray be a new Array object whose elements are the same
1202 // values in the same order as the elements of subset.
1203 var subsetArray = arrSlice.call(subset);
1204
1205 // 6. Return subsetArray.
1206 return subsetArray;
1207 }
1208
1209 /**
1210 * The BestFitSupportedLocales abstract operation returns the subset of the
1211 * provided BCP 47 language priority list requestedLocales for which
1212 * availableLocales has a matching locale when using the Best Fit Matcher
1213 * algorithm. Locales appear in the same order in the returned list as in
1214 * requestedLocales. The steps taken are implementation dependent.
1215 */
1216 function /*9.2.7 */BestFitSupportedLocales(availableLocales, requestedLocales) {
1217 // ###TODO: implement this function as described by the specification###
1218 return LookupSupportedLocales(availableLocales, requestedLocales);
1219 }
1220
1221 /**
1222 * The SupportedLocales abstract operation returns the subset of the provided BCP
1223 * 47 language priority list requestedLocales for which availableLocales has a
1224 * matching locale. Two algorithms are available to match the locales: the Lookup
1225 * algorithm described in RFC 4647 section 3.4, and an implementation dependent
1226 * best-fit algorithm. Locales appear in the same order in the returned list as
1227 * in requestedLocales. The following steps are taken:
1228 */
1229 function /*9.2.8 */SupportedLocales(availableLocales, requestedLocales, options) {
1230 var matcher = void 0,
1231 subset = void 0;
1232
1233 // 1. If options is not undefined, then
1234 if (options !== undefined) {
1235 // a. Let options be ToObject(options).
1236 options = new Record(toObject(options));
1237 // b. Let matcher be the result of calling the [[Get]] internal method of
1238 // options with argument "localeMatcher".
1239 matcher = options.localeMatcher;
1240
1241 // c. If matcher is not undefined, then
1242 if (matcher !== undefined) {
1243 // i. Let matcher be ToString(matcher).
1244 matcher = String(matcher);
1245
1246 // ii. If matcher is not "lookup" or "best fit", then throw a RangeError
1247 // exception.
1248 if (matcher !== 'lookup' && matcher !== 'best fit') throw new RangeError('matcher should be "lookup" or "best fit"');
1249 }
1250 }
1251 // 2. If matcher is undefined or "best fit", then
1252 if (matcher === undefined || matcher === 'best fit')
1253 // a. Let subset be the result of calling the BestFitSupportedLocales
1254 // abstract operation (defined in 9.2.7) with arguments
1255 // availableLocales and requestedLocales.
1256 subset = BestFitSupportedLocales(availableLocales, requestedLocales);
1257 // 3. Else
1258 else
1259 // a. Let subset be the result of calling the LookupSupportedLocales
1260 // abstract operation (defined in 9.2.6) with arguments
1261 // availableLocales and requestedLocales.
1262 subset = LookupSupportedLocales(availableLocales, requestedLocales);
1263
1264 // 4. For each named own property name P of subset,
1265 for (var P in subset) {
1266 if (!hop.call(subset, P)) continue;
1267
1268 // a. Let desc be the result of calling the [[GetOwnProperty]] internal
1269 // method of subset with P.
1270 // b. Set desc.[[Writable]] to false.
1271 // c. Set desc.[[Configurable]] to false.
1272 // d. Call the [[DefineOwnProperty]] internal method of subset with P, desc,
1273 // and true as arguments.
1274 defineProperty(subset, P, {
1275 writable: false, configurable: false, value: subset[P]
1276 });
1277 }
1278 // "Freeze" the array so no new elements can be added
1279 defineProperty(subset, 'length', { writable: false });
1280
1281 // 5. Return subset
1282 return subset;
1283 }
1284
1285 /**
1286 * The GetOption abstract operation extracts the value of the property named
1287 * property from the provided options object, converts it to the required type,
1288 * checks whether it is one of a List of allowed values, and fills in a fallback
1289 * value if necessary.
1290 */
1291 function /*9.2.9 */GetOption(options, property, type, values, fallback) {
1292 // 1. Let value be the result of calling the [[Get]] internal method of
1293 // options with argument property.
1294 var value = options[property];
1295
1296 // 2. If value is not undefined, then
1297 if (value !== undefined) {
1298 // a. Assert: type is "boolean" or "string".
1299 // b. If type is "boolean", then let value be ToBoolean(value).
1300 // c. If type is "string", then let value be ToString(value).
1301 value = type === 'boolean' ? Boolean(value) : type === 'string' ? String(value) : value;
1302
1303 // d. If values is not undefined, then
1304 if (values !== undefined) {
1305 // i. If values does not contain an element equal to value, then throw a
1306 // RangeError exception.
1307 if (arrIndexOf.call(values, value) === -1) throw new RangeError("'" + value + "' is not an allowed value for `" + property + '`');
1308 }
1309
1310 // e. Return value.
1311 return value;
1312 }
1313 // Else return fallback.
1314 return fallback;
1315 }
1316
1317 /**
1318 * The GetNumberOption abstract operation extracts a property value from the
1319 * provided options object, converts it to a Number value, checks whether it is
1320 * in the allowed range, and fills in a fallback value if necessary.
1321 */
1322 function /* 9.2.10 */GetNumberOption(options, property, minimum, maximum, fallback) {
1323 // 1. Let value be the result of calling the [[Get]] internal method of
1324 // options with argument property.
1325 var value = options[property];
1326
1327 // 2. If value is not undefined, then
1328 if (value !== undefined) {
1329 // a. Let value be ToNumber(value).
1330 value = Number(value);
1331
1332 // b. If value is NaN or less than minimum or greater than maximum, throw a
1333 // RangeError exception.
1334 if (isNaN(value) || value < minimum || value > maximum) throw new RangeError('Value is not a number or outside accepted range');
1335
1336 // c. Return floor(value).
1337 return Math.floor(value);
1338 }
1339 // 3. Else return fallback.
1340 return fallback;
1341 }
1342
1343 // 8 The Intl Object
1344 var Intl$1 = {};
1345
1346 // 8.2 Function Properties of the Intl Object
1347
1348 // 8.2.1
1349 // @spec[tc39/ecma402/master/spec/intl.html]
1350 // @clause[sec-intl.getcanonicallocales]
1351 Intl$1.getCanonicalLocales = function (locales) {
1352 // 1. Let ll be ? CanonicalizeLocaleList(locales).
1353 var ll = CanonicalizeLocaleList(locales);
1354 // 2. Return CreateArrayFromList(ll).
1355 {
1356 var result = [];
1357 for (var code in ll) {
1358 result.push(ll[code]);
1359 }
1360 return result;
1361 }
1362 };
1363
1364 // Currency minor units output from get-4217 grunt task, formatted
1365 var currencyMinorUnits = {
1366 BHD: 3, BYR: 0, XOF: 0, BIF: 0, XAF: 0, CLF: 4, CLP: 0, KMF: 0, DJF: 0,
1367 XPF: 0, GNF: 0, ISK: 0, IQD: 3, JPY: 0, JOD: 3, KRW: 0, KWD: 3, LYD: 3,
1368 OMR: 3, PYG: 0, RWF: 0, TND: 3, UGX: 0, UYI: 0, VUV: 0, VND: 0
1369 };
1370
1371 // Define the NumberFormat constructor internally so it cannot be tainted
1372 function NumberFormatConstructor() {
1373 var locales = arguments[0];
1374 var options = arguments[1];
1375
1376 if (!this || this === Intl$1) {
1377 return new Intl$1.NumberFormat(locales, options);
1378 }
1379
1380 return InitializeNumberFormat(toObject(this), locales, options);
1381 }
1382
1383 defineProperty(Intl$1, 'NumberFormat', {
1384 configurable: true,
1385 writable: true,
1386 value: NumberFormatConstructor
1387 });
1388
1389 // Must explicitly set prototypes as unwritable
1390 defineProperty(Intl$1.NumberFormat, 'prototype', {
1391 writable: false
1392 });
1393
1394 /**
1395 * The abstract operation InitializeNumberFormat accepts the arguments
1396 * numberFormat (which must be an object), locales, and options. It initializes
1397 * numberFormat as a NumberFormat object.
1398 */
1399 function /*11.1.1.1 */InitializeNumberFormat(numberFormat, locales, options) {
1400 // This will be a internal properties object if we're not already initialized
1401 var internal = getInternalProperties(numberFormat);
1402
1403 // Create an object whose props can be used to restore the values of RegExp props
1404 var regexpState = createRegExpRestore();
1405
1406 // 1. If numberFormat has an [[initializedIntlObject]] internal property with
1407 // value true, throw a TypeError exception.
1408 if (internal['[[initializedIntlObject]]'] === true) throw new TypeError('`this` object has already been initialized as an Intl object');
1409
1410 // Need this to access the `internal` object
1411 defineProperty(numberFormat, '__getInternalProperties', {
1412 value: function value() {
1413 // NOTE: Non-standard, for internal use only
1414 if (arguments[0] === secret) return internal;
1415 }
1416 });
1417
1418 // 2. Set the [[initializedIntlObject]] internal property of numberFormat to true.
1419 internal['[[initializedIntlObject]]'] = true;
1420
1421 // 3. Let requestedLocales be the result of calling the CanonicalizeLocaleList
1422 // abstract operation (defined in 9.2.1) with argument locales.
1423 var requestedLocales = CanonicalizeLocaleList(locales);
1424
1425 // 4. If options is undefined, then
1426 if (options === undefined)
1427 // a. Let options be the result of creating a new object as if by the
1428 // expression new Object() where Object is the standard built-in constructor
1429 // with that name.
1430 options = {};
1431
1432 // 5. Else
1433 else
1434 // a. Let options be ToObject(options).
1435 options = toObject(options);
1436
1437 // 6. Let opt be a new Record.
1438 var opt = new Record(),
1439
1440
1441 // 7. Let matcher be the result of calling the GetOption abstract operation
1442 // (defined in 9.2.9) with the arguments options, "localeMatcher", "string",
1443 // a List containing the two String values "lookup" and "best fit", and
1444 // "best fit".
1445 matcher = GetOption(options, 'localeMatcher', 'string', new List('lookup', 'best fit'), 'best fit');
1446
1447 // 8. Set opt.[[localeMatcher]] to matcher.
1448 opt['[[localeMatcher]]'] = matcher;
1449
1450 // 9. Let NumberFormat be the standard built-in object that is the initial value
1451 // of Intl.NumberFormat.
1452 // 10. Let localeData be the value of the [[localeData]] internal property of
1453 // NumberFormat.
1454 var localeData = internals.NumberFormat['[[localeData]]'];
1455
1456 // 11. Let r be the result of calling the ResolveLocale abstract operation
1457 // (defined in 9.2.5) with the [[availableLocales]] internal property of
1458 // NumberFormat, requestedLocales, opt, the [[relevantExtensionKeys]]
1459 // internal property of NumberFormat, and localeData.
1460 var r = ResolveLocale(internals.NumberFormat['[[availableLocales]]'], requestedLocales, opt, internals.NumberFormat['[[relevantExtensionKeys]]'], localeData);
1461
1462 // 12. Set the [[locale]] internal property of numberFormat to the value of
1463 // r.[[locale]].
1464 internal['[[locale]]'] = r['[[locale]]'];
1465
1466 // 13. Set the [[numberingSystem]] internal property of numberFormat to the value
1467 // of r.[[nu]].
1468 internal['[[numberingSystem]]'] = r['[[nu]]'];
1469
1470 // The specification doesn't tell us to do this, but it's helpful later on
1471 internal['[[dataLocale]]'] = r['[[dataLocale]]'];
1472
1473 // 14. Let dataLocale be the value of r.[[dataLocale]].
1474 var dataLocale = r['[[dataLocale]]'];
1475
1476 // 15. Let s be the result of calling the GetOption abstract operation with the
1477 // arguments options, "style", "string", a List containing the three String
1478 // values "decimal", "percent", and "currency", and "decimal".
1479 var s = GetOption(options, 'style', 'string', new List('decimal', 'percent', 'currency'), 'decimal');
1480
1481 // 16. Set the [[style]] internal property of numberFormat to s.
1482 internal['[[style]]'] = s;
1483
1484 // 17. Let c be the result of calling the GetOption abstract operation with the
1485 // arguments options, "currency", "string", undefined, and undefined.
1486 var c = GetOption(options, 'currency', 'string');
1487
1488 // 18. If c is not undefined and the result of calling the
1489 // IsWellFormedCurrencyCode abstract operation (defined in 6.3.1) with
1490 // argument c is false, then throw a RangeError exception.
1491 if (c !== undefined && !IsWellFormedCurrencyCode(c)) throw new RangeError("'" + c + "' is not a valid currency code");
1492
1493 // 19. If s is "currency" and c is undefined, throw a TypeError exception.
1494 if (s === 'currency' && c === undefined) throw new TypeError('Currency code is required when style is currency');
1495
1496 var cDigits = void 0;
1497
1498 // 20. If s is "currency", then
1499 if (s === 'currency') {
1500 // a. Let c be the result of converting c to upper case as specified in 6.1.
1501 c = c.toUpperCase();
1502
1503 // b. Set the [[currency]] internal property of numberFormat to c.
1504 internal['[[currency]]'] = c;
1505
1506 // c. Let cDigits be the result of calling the CurrencyDigits abstract
1507 // operation (defined below) with argument c.
1508 cDigits = CurrencyDigits(c);
1509 }
1510
1511 // 21. Let cd be the result of calling the GetOption abstract operation with the
1512 // arguments options, "currencyDisplay", "string", a List containing the
1513 // three String values "code", "symbol", and "name", and "symbol".
1514 var cd = GetOption(options, 'currencyDisplay', 'string', new List('code', 'symbol', 'name'), 'symbol');
1515
1516 // 22. If s is "currency", then set the [[currencyDisplay]] internal property of
1517 // numberFormat to cd.
1518 if (s === 'currency') internal['[[currencyDisplay]]'] = cd;
1519
1520 // 23. Let mnid be the result of calling the GetNumberOption abstract operation
1521 // (defined in 9.2.10) with arguments options, "minimumIntegerDigits", 1, 21,
1522 // and 1.
1523 var mnid = GetNumberOption(options, 'minimumIntegerDigits', 1, 21, 1);
1524
1525 // 24. Set the [[minimumIntegerDigits]] internal property of numberFormat to mnid.
1526 internal['[[minimumIntegerDigits]]'] = mnid;
1527
1528 // 25. If s is "currency", then let mnfdDefault be cDigits; else let mnfdDefault
1529 // be 0.
1530 var mnfdDefault = s === 'currency' ? cDigits : 0;
1531
1532 // 26. Let mnfd be the result of calling the GetNumberOption abstract operation
1533 // with arguments options, "minimumFractionDigits", 0, 20, and mnfdDefault.
1534 var mnfd = GetNumberOption(options, 'minimumFractionDigits', 0, 20, mnfdDefault);
1535
1536 // 27. Set the [[minimumFractionDigits]] internal property of numberFormat to mnfd.
1537 internal['[[minimumFractionDigits]]'] = mnfd;
1538
1539 // 28. If s is "currency", then let mxfdDefault be max(mnfd, cDigits); else if s
1540 // is "percent", then let mxfdDefault be max(mnfd, 0); else let mxfdDefault
1541 // be max(mnfd, 3).
1542 var mxfdDefault = s === 'currency' ? Math.max(mnfd, cDigits) : s === 'percent' ? Math.max(mnfd, 0) : Math.max(mnfd, 3);
1543
1544 // 29. Let mxfd be the result of calling the GetNumberOption abstract operation
1545 // with arguments options, "maximumFractionDigits", mnfd, 20, and mxfdDefault.
1546 var mxfd = GetNumberOption(options, 'maximumFractionDigits', mnfd, 20, mxfdDefault);
1547
1548 // 30. Set the [[maximumFractionDigits]] internal property of numberFormat to mxfd.
1549 internal['[[maximumFractionDigits]]'] = mxfd;
1550
1551 // 31. Let mnsd be the result of calling the [[Get]] internal method of options
1552 // with argument "minimumSignificantDigits".
1553 var mnsd = options.minimumSignificantDigits;
1554
1555 // 32. Let mxsd be the result of calling the [[Get]] internal method of options
1556 // with argument "maximumSignificantDigits".
1557 var mxsd = options.maximumSignificantDigits;
1558
1559 // 33. If mnsd is not undefined or mxsd is not undefined, then:
1560 if (mnsd !== undefined || mxsd !== undefined) {
1561 // a. Let mnsd be the result of calling the GetNumberOption abstract
1562 // operation with arguments options, "minimumSignificantDigits", 1, 21,
1563 // and 1.
1564 mnsd = GetNumberOption(options, 'minimumSignificantDigits', 1, 21, 1);
1565
1566 // b. Let mxsd be the result of calling the GetNumberOption abstract
1567 // operation with arguments options, "maximumSignificantDigits", mnsd,
1568 // 21, and 21.
1569 mxsd = GetNumberOption(options, 'maximumSignificantDigits', mnsd, 21, 21);
1570
1571 // c. Set the [[minimumSignificantDigits]] internal property of numberFormat
1572 // to mnsd, and the [[maximumSignificantDigits]] internal property of
1573 // numberFormat to mxsd.
1574 internal['[[minimumSignificantDigits]]'] = mnsd;
1575 internal['[[maximumSignificantDigits]]'] = mxsd;
1576 }
1577 // 34. Let g be the result of calling the GetOption abstract operation with the
1578 // arguments options, "useGrouping", "boolean", undefined, and true.
1579 var g = GetOption(options, 'useGrouping', 'boolean', undefined, true);
1580
1581 // 35. Set the [[useGrouping]] internal property of numberFormat to g.
1582 internal['[[useGrouping]]'] = g;
1583
1584 // 36. Let dataLocaleData be the result of calling the [[Get]] internal method of
1585 // localeData with argument dataLocale.
1586 var dataLocaleData = localeData[dataLocale];
1587
1588 // 37. Let patterns be the result of calling the [[Get]] internal method of
1589 // dataLocaleData with argument "patterns".
1590 var patterns = dataLocaleData.patterns;
1591
1592 // 38. Assert: patterns is an object (see 11.2.3)
1593
1594 // 39. Let stylePatterns be the result of calling the [[Get]] internal method of
1595 // patterns with argument s.
1596 var stylePatterns = patterns[s];
1597
1598 // 40. Set the [[positivePattern]] internal property of numberFormat to the
1599 // result of calling the [[Get]] internal method of stylePatterns with the
1600 // argument "positivePattern".
1601 internal['[[positivePattern]]'] = stylePatterns.positivePattern;
1602
1603 // 41. Set the [[negativePattern]] internal property of numberFormat to the
1604 // result of calling the [[Get]] internal method of stylePatterns with the
1605 // argument "negativePattern".
1606 internal['[[negativePattern]]'] = stylePatterns.negativePattern;
1607
1608 // 42. Set the [[boundFormat]] internal property of numberFormat to undefined.
1609 internal['[[boundFormat]]'] = undefined;
1610
1611 // 43. Set the [[initializedNumberFormat]] internal property of numberFormat to
1612 // true.
1613 internal['[[initializedNumberFormat]]'] = true;
1614
1615 // In ES3, we need to pre-bind the format() function
1616 if (es3) numberFormat.format = GetFormatNumber.call(numberFormat);
1617
1618 // Restore the RegExp properties
1619 regexpState.exp.test(regexpState.input);
1620
1621 // Return the newly initialised object
1622 return numberFormat;
1623 }
1624
1625 function CurrencyDigits(currency) {
1626 // When the CurrencyDigits abstract operation is called with an argument currency
1627 // (which must be an upper case String value), the following steps are taken:
1628
1629 // 1. If the ISO 4217 currency and funds code list contains currency as an
1630 // alphabetic code, then return the minor unit value corresponding to the
1631 // currency from the list; else return 2.
1632 return currencyMinorUnits[currency] !== undefined ? currencyMinorUnits[currency] : 2;
1633 }
1634
1635 /* 11.2.3 */internals.NumberFormat = {
1636 '[[availableLocales]]': [],
1637 '[[relevantExtensionKeys]]': ['nu'],
1638 '[[localeData]]': {}
1639 };
1640
1641 /**
1642 * When the supportedLocalesOf method of Intl.NumberFormat is called, the
1643 * following steps are taken:
1644 */
1645 /* 11.2.2 */
1646 defineProperty(Intl$1.NumberFormat, 'supportedLocalesOf', {
1647 configurable: true,
1648 writable: true,
1649 value: fnBind.call(function (locales) {
1650 // Bound functions only have the `this` value altered if being used as a constructor,
1651 // this lets us imitate a native function that has no constructor
1652 if (!hop.call(this, '[[availableLocales]]')) throw new TypeError('supportedLocalesOf() is not a constructor');
1653
1654 // Create an object whose props can be used to restore the values of RegExp props
1655 var regexpState = createRegExpRestore(),
1656
1657
1658 // 1. If options is not provided, then let options be undefined.
1659 options = arguments[1],
1660
1661
1662 // 2. Let availableLocales be the value of the [[availableLocales]] internal
1663 // property of the standard built-in object that is the initial value of
1664 // Intl.NumberFormat.
1665
1666 availableLocales = this['[[availableLocales]]'],
1667
1668
1669 // 3. Let requestedLocales be the result of calling the CanonicalizeLocaleList
1670 // abstract operation (defined in 9.2.1) with argument locales.
1671 requestedLocales = CanonicalizeLocaleList(locales);
1672
1673 // Restore the RegExp properties
1674 regexpState.exp.test(regexpState.input);
1675
1676 // 4. Return the result of calling the SupportedLocales abstract operation
1677 // (defined in 9.2.8) with arguments availableLocales, requestedLocales,
1678 // and options.
1679 return SupportedLocales(availableLocales, requestedLocales, options);
1680 }, internals.NumberFormat)
1681 });
1682
1683 /**
1684 * This named accessor property returns a function that formats a number
1685 * according to the effective locale and the formatting options of this
1686 * NumberFormat object.
1687 */
1688 /* 11.3.2 */defineProperty(Intl$1.NumberFormat.prototype, 'format', {
1689 configurable: true,
1690 get: GetFormatNumber
1691 });
1692
1693 function GetFormatNumber() {
1694 var internal = this !== null && babelHelpers["typeof"](this) === 'object' && getInternalProperties(this);
1695
1696 // Satisfy test 11.3_b
1697 if (!internal || !internal['[[initializedNumberFormat]]']) throw new TypeError('`this` value for format() is not an initialized Intl.NumberFormat object.');
1698
1699 // The value of the [[Get]] attribute is a function that takes the following
1700 // steps:
1701
1702 // 1. If the [[boundFormat]] internal property of this NumberFormat object
1703 // is undefined, then:
1704 if (internal['[[boundFormat]]'] === undefined) {
1705 // a. Let F be a Function object, with internal properties set as
1706 // specified for built-in functions in ES5, 15, or successor, and the
1707 // length property set to 1, that takes the argument value and
1708 // performs the following steps:
1709 var F = function F(value) {
1710 // i. If value is not provided, then let value be undefined.
1711 // ii. Let x be ToNumber(value).
1712 // iii. Return the result of calling the FormatNumber abstract
1713 // operation (defined below) with arguments this and x.
1714 return FormatNumber(this, /* x = */Number(value));
1715 };
1716
1717 // b. Let bind be the standard built-in function object defined in ES5,
1718 // 15.3.4.5.
1719 // c. Let bf be the result of calling the [[Call]] internal method of
1720 // bind with F as the this value and an argument list containing
1721 // the single item this.
1722 var bf = fnBind.call(F, this);
1723
1724 // d. Set the [[boundFormat]] internal property of this NumberFormat
1725 // object to bf.
1726 internal['[[boundFormat]]'] = bf;
1727 }
1728 // Return the value of the [[boundFormat]] internal property of this
1729 // NumberFormat object.
1730 return internal['[[boundFormat]]'];
1731 }
1732
1733 Intl$1.NumberFormat.prototype.formatToParts = function (value) {
1734 var internal = this !== null && babelHelpers["typeof"](this) === 'object' && getInternalProperties(this);
1735 if (!internal || !internal['[[initializedNumberFormat]]']) throw new TypeError('`this` value for formatToParts() is not an initialized Intl.NumberFormat object.');
1736
1737 var x = Number(value);
1738 return FormatNumberToParts(this, x);
1739 };
1740
1741 /*
1742 * @spec[stasm/ecma402/number-format-to-parts/spec/numberformat.html]
1743 * @clause[sec-formatnumbertoparts]
1744 */
1745 function FormatNumberToParts(numberFormat, x) {
1746 // 1. Let parts be ? PartitionNumberPattern(numberFormat, x).
1747 var parts = PartitionNumberPattern(numberFormat, x);
1748 // 2. Let result be ArrayCreate(0).
1749 var result = [];
1750 // 3. Let n be 0.
1751 var n = 0;
1752 // 4. For each part in parts, do:
1753 for (var i = 0; parts.length > i; i++) {
1754 var part = parts[i];
1755 // a. Let O be ObjectCreate(%ObjectPrototype%).
1756 var O = {};
1757 // a. Perform ? CreateDataPropertyOrThrow(O, "type", part.[[type]]).
1758 O.type = part['[[type]]'];
1759 // a. Perform ? CreateDataPropertyOrThrow(O, "value", part.[[value]]).
1760 O.value = part['[[value]]'];
1761 // a. Perform ? CreateDataPropertyOrThrow(result, ? ToString(n), O).
1762 result[n] = O;
1763 // a. Increment n by 1.
1764 n += 1;
1765 }
1766 // 5. Return result.
1767 return result;
1768 }
1769
1770 /*
1771 * @spec[stasm/ecma402/number-format-to-parts/spec/numberformat.html]
1772 * @clause[sec-partitionnumberpattern]
1773 */
1774 function PartitionNumberPattern(numberFormat, x) {
1775
1776 var internal = getInternalProperties(numberFormat),
1777 locale = internal['[[dataLocale]]'],
1778 nums = internal['[[numberingSystem]]'],
1779 data = internals.NumberFormat['[[localeData]]'][locale],
1780 ild = data.symbols[nums] || data.symbols.latn,
1781 pattern = void 0;
1782
1783 // 1. If x is not NaN and x < 0, then:
1784 if (!isNaN(x) && x < 0) {
1785 // a. Let x be -x.
1786 x = -x;
1787 // a. Let pattern be the value of numberFormat.[[negativePattern]].
1788 pattern = internal['[[negativePattern]]'];
1789 }
1790 // 2. Else,
1791 else {
1792 // a. Let pattern be the value of numberFormat.[[positivePattern]].
1793 pattern = internal['[[positivePattern]]'];
1794 }
1795 // 3. Let result be a new empty List.
1796 var result = new List();
1797 // 4. Let beginIndex be Call(%StringProto_indexOf%, pattern, "{", 0).
1798 var beginIndex = pattern.indexOf('{', 0);
1799 // 5. Let endIndex be 0.
1800 var endIndex = 0;
1801 // 6. Let nextIndex be 0.
1802 var nextIndex = 0;
1803 // 7. Let length be the number of code units in pattern.
1804 var length = pattern.length;
1805 // 8. Repeat while beginIndex is an integer index into pattern:
1806 while (beginIndex > -1 && beginIndex < length) {
1807 // a. Set endIndex to Call(%StringProto_indexOf%, pattern, "}", beginIndex)
1808 endIndex = pattern.indexOf('}', beginIndex);
1809 // a. If endIndex = -1, throw new Error exception.
1810 if (endIndex === -1) throw new Error();
1811 // a. If beginIndex is greater than nextIndex, then:
1812 if (beginIndex > nextIndex) {
1813 // i. Let literal be a substring of pattern from position nextIndex, inclusive, to position beginIndex, exclusive.
1814 var literal = pattern.substring(nextIndex, beginIndex);
1815 // ii. Add new part record { [[type]]: "literal", [[value]]: literal } as a new element of the list result.
1816 arrPush.call(result, { '[[type]]': 'literal', '[[value]]': literal });
1817 }
1818 // a. Let p be the substring of pattern from position beginIndex, exclusive, to position endIndex, exclusive.
1819 var p = pattern.substring(beginIndex + 1, endIndex);
1820 // a. If p is equal "number", then:
1821 if (p === "number") {
1822 // i. If x is NaN,
1823 if (isNaN(x)) {
1824 // 1. Let n be an ILD String value indicating the NaN value.
1825 var n = ild.nan;
1826 // 2. Add new part record { [[type]]: "nan", [[value]]: n } as a new element of the list result.
1827 arrPush.call(result, { '[[type]]': 'nan', '[[value]]': n });
1828 }
1829 // ii. Else if isFinite(x) is false,
1830 else if (!isFinite(x)) {
1831 // 1. Let n be an ILD String value indicating infinity.
1832 var _n = ild.infinity;
1833 // 2. Add new part record { [[type]]: "infinity", [[value]]: n } as a new element of the list result.
1834 arrPush.call(result, { '[[type]]': 'infinity', '[[value]]': _n });
1835 }
1836 // iii. Else,
1837 else {
1838 // 1. If the value of numberFormat.[[style]] is "percent" and isFinite(x), let x be 100 × x.
1839 if (internal['[[style]]'] === 'percent' && isFinite(x)) x *= 100;
1840
1841 var _n2 = void 0;
1842 // 2. If the numberFormat.[[minimumSignificantDigits]] and numberFormat.[[maximumSignificantDigits]] are present, then
1843 if (hop.call(internal, '[[minimumSignificantDigits]]') && hop.call(internal, '[[maximumSignificantDigits]]')) {
1844 // a. Let n be ToRawPrecision(x, numberFormat.[[minimumSignificantDigits]], numberFormat.[[maximumSignificantDigits]]).
1845 _n2 = ToRawPrecision(x, internal['[[minimumSignificantDigits]]'], internal['[[maximumSignificantDigits]]']);
1846 }
1847 // 3. Else,
1848 else {
1849 // a. Let n be ToRawFixed(x, numberFormat.[[minimumIntegerDigits]], numberFormat.[[minimumFractionDigits]], numberFormat.[[maximumFractionDigits]]).
1850 _n2 = ToRawFixed(x, internal['[[minimumIntegerDigits]]'], internal['[[minimumFractionDigits]]'], internal['[[maximumFractionDigits]]']);
1851 }
1852 // 4. If the value of the numberFormat.[[numberingSystem]] matches one of the values in the "Numbering System" column of Table 2 below, then
1853 if (numSys[nums]) {
1854 (function () {
1855 // a. Let digits be an array whose 10 String valued elements are the UTF-16 string representations of the 10 digits specified in the "Digits" column of the matching row in Table 2.
1856 var digits = numSys[nums];
1857 // a. Replace each digit in n with the value of digits[digit].
1858 _n2 = String(_n2).replace(/\d/g, function (digit) {
1859 return digits[digit];
1860 });
1861 })();
1862 }
1863 // 5. Else use an implementation dependent algorithm to map n to the appropriate representation of n in the given numbering system.
1864 else _n2 = String(_n2); // ###TODO###
1865
1866 var integer = void 0;
1867 var fraction = void 0;
1868 // 6. Let decimalSepIndex be Call(%StringProto_indexOf%, n, ".", 0).
1869 var decimalSepIndex = _n2.indexOf('.', 0);
1870 // 7. If decimalSepIndex > 0, then:
1871 if (decimalSepIndex > 0) {
1872 // a. Let integer be the substring of n from position 0, inclusive, to position decimalSepIndex, exclusive.
1873 integer = _n2.substring(0, decimalSepIndex);
1874 // a. Let fraction be the substring of n from position decimalSepIndex, exclusive, to the end of n.
1875 fraction = _n2.substring(decimalSepIndex + 1, decimalSepIndex.length);
1876 }
1877 // 8. Else:
1878 else {
1879 // a. Let integer be n.
1880 integer = _n2;
1881 // a. Let fraction be undefined.
1882 fraction = undefined;
1883 }
1884 // 9. If the value of the numberFormat.[[useGrouping]] is true,
1885 if (internal['[[useGrouping]]'] === true) {
1886 // a. Let groupSepSymbol be the ILND String representing the grouping separator.
1887 var groupSepSymbol = ild.group;
1888 // a. Let groups be a List whose elements are, in left to right order, the substrings defined by ILND set of locations within the integer.
1889 var groups = [];
1890 // ----> implementation:
1891 // Primary group represents the group closest to the decimal
1892 var pgSize = data.patterns.primaryGroupSize || 3;
1893 // Secondary group is every other group
1894 var sgSize = data.patterns.secondaryGroupSize || pgSize;
1895 // Group only if necessary
1896 if (integer.length > pgSize) {
1897 // Index of the primary grouping separator
1898 var end = integer.length - pgSize;
1899 // Starting index for our loop
1900 var idx = end % sgSize;
1901 var start = integer.slice(0, idx);
1902 if (start.length) arrPush.call(groups, start);
1903 // Loop to separate into secondary grouping digits
1904 while (idx < end) {
1905 arrPush.call(groups, integer.slice(idx, idx + sgSize));
1906 idx += sgSize;
1907 }
1908 // Add the primary grouping digits
1909 arrPush.call(groups, integer.slice(end));
1910 } else {
1911 arrPush.call(groups, integer);
1912 }
1913 // a. Assert: The number of elements in groups List is greater than 0.
1914 if (groups.length === 0) throw new Error();
1915 // a. Repeat, while groups List is not empty:
1916 while (groups.length) {
1917 // i. Remove the first element from groups and let integerGroup be the value of that element.
1918 var integerGroup = arrShift.call(groups);
1919 // ii. Add new part record { [[type]]: "integer", [[value]]: integerGroup } as a new element of the list result.
1920 arrPush.call(result, { '[[type]]': 'integer', '[[value]]': integerGroup });
1921 // iii. If groups List is not empty, then:
1922 if (groups.length) {
1923 // 1. Add new part record { [[type]]: "group", [[value]]: groupSepSymbol } as a new element of the list result.
1924 arrPush.call(result, { '[[type]]': 'group', '[[value]]': groupSepSymbol });
1925 }
1926 }
1927 }
1928 // 10. Else,
1929 else {
1930 // a. Add new part record { [[type]]: "integer", [[value]]: integer } as a new element of the list result.
1931 arrPush.call(result, { '[[type]]': 'integer', '[[value]]': integer });
1932 }
1933 // 11. If fraction is not undefined, then:
1934 if (fraction !== undefined) {
1935 // a. Let decimalSepSymbol be the ILND String representing the decimal separator.
1936 var decimalSepSymbol = ild.decimal;
1937 // a. Add new part record { [[type]]: "decimal", [[value]]: decimalSepSymbol } as a new element of the list result.
1938 arrPush.call(result, { '[[type]]': 'decimal', '[[value]]': decimalSepSymbol });
1939 // a. Add new part record { [[type]]: "fraction", [[value]]: fraction } as a new element of the list result.
1940 arrPush.call(result, { '[[type]]': 'fraction', '[[value]]': fraction });
1941 }
1942 }
1943 }
1944 // a. Else if p is equal "plusSign", then:
1945 else if (p === "plusSign") {
1946 // i. Let plusSignSymbol be the ILND String representing the plus sign.
1947 var plusSignSymbol = ild.plusSign;
1948 // ii. Add new part record { [[type]]: "plusSign", [[value]]: plusSignSymbol } as a new element of the list result.
1949 arrPush.call(result, { '[[type]]': 'plusSign', '[[value]]': plusSignSymbol });
1950 }
1951 // a. Else if p is equal "minusSign", then:
1952 else if (p === "minusSign") {
1953 // i. Let minusSignSymbol be the ILND String representing the minus sign.
1954 var minusSignSymbol = ild.minusSign;
1955 // ii. Add new part record { [[type]]: "minusSign", [[value]]: minusSignSymbol } as a new element of the list result.
1956 arrPush.call(result, { '[[type]]': 'minusSign', '[[value]]': minusSignSymbol });
1957 }
1958 // a. Else if p is equal "percentSign" and numberFormat.[[style]] is "percent", then:
1959 else if (p === "percentSign" && internal['[[style]]'] === "percent") {
1960 // i. Let percentSignSymbol be the ILND String representing the percent sign.
1961 var percentSignSymbol = ild.percentSign;
1962 // ii. Add new part record { [[type]]: "percentSign", [[value]]: percentSignSymbol } as a new element of the list result.
1963 arrPush.call(result, { '[[type]]': 'literal', '[[value]]': percentSignSymbol });
1964 }
1965 // a. Else if p is equal "currency" and numberFormat.[[style]] is "currency", then:
1966 else if (p === "currency" && internal['[[style]]'] === "currency") {
1967 // i. Let currency be the value of numberFormat.[[currency]].
1968 var currency = internal['[[currency]]'];
1969
1970 var cd = void 0;
1971
1972 // ii. If numberFormat.[[currencyDisplay]] is "code", then
1973 if (internal['[[currencyDisplay]]'] === "code") {
1974 // 1. Let cd be currency.
1975 cd = currency;
1976 }
1977 // iii. Else if numberFormat.[[currencyDisplay]] is "symbol", then
1978 else if (internal['[[currencyDisplay]]'] === "symbol") {
1979 // 1. Let cd be an ILD string representing currency in short form. If the implementation does not have such a representation of currency, use currency itself.
1980 cd = data.currencies[currency] || currency;
1981 }
1982 // iv. Else if numberFormat.[[currencyDisplay]] is "name", then
1983 else if (internal['[[currencyDisplay]]'] === "name") {
1984 // 1. Let cd be an ILD string representing currency in long form. If the implementation does not have such a representation of currency, then use currency itself.
1985 cd = currency;
1986 }
1987 // v. Add new part record { [[type]]: "currency", [[value]]: cd } as a new element of the list result.
1988 arrPush.call(result, { '[[type]]': 'currency', '[[value]]': cd });
1989 }
1990 // a. Else,
1991 else {
1992 // i. Let literal be the substring of pattern from position beginIndex, inclusive, to position endIndex, inclusive.
1993 var _literal = pattern.substring(beginIndex, endIndex);
1994 // ii. Add new part record { [[type]]: "literal", [[value]]: literal } as a new element of the list result.
1995 arrPush.call(result, { '[[type]]': 'literal', '[[value]]': _literal });
1996 }
1997 // a. Set nextIndex to endIndex + 1.
1998 nextIndex = endIndex + 1;
1999 // a. Set beginIndex to Call(%StringProto_indexOf%, pattern, "{", nextIndex)
2000 beginIndex = pattern.indexOf('{', nextIndex);
2001 }
2002 // 9. If nextIndex is less than length, then:
2003 if (nextIndex < length) {
2004 // a. Let literal be the substring of pattern from position nextIndex, inclusive, to position length, exclusive.
2005 var _literal2 = pattern.substring(nextIndex, length);
2006 // a. Add new part record { [[type]]: "literal", [[value]]: literal } as a new element of the list result.
2007 arrPush.call(result, { '[[type]]': 'literal', '[[value]]': _literal2 });
2008 }
2009 // 10. Return result.
2010 return result;
2011 }
2012
2013 /*
2014 * @spec[stasm/ecma402/number-format-to-parts/spec/numberformat.html]
2015 * @clause[sec-formatnumber]
2016 */
2017 function FormatNumber(numberFormat, x) {
2018 // 1. Let parts be ? PartitionNumberPattern(numberFormat, x).
2019 var parts = PartitionNumberPattern(numberFormat, x);
2020 // 2. Let result be an empty String.
2021 var result = '';
2022 // 3. For each part in parts, do:
2023 for (var i = 0; parts.length > i; i++) {
2024 var part = parts[i];
2025 // a. Set result to a String value produced by concatenating result and part.[[value]].
2026 result += part['[[value]]'];
2027 }
2028 // 4. Return result.
2029 return result;
2030 }
2031
2032 /**
2033 * When the ToRawPrecision abstract operation is called with arguments x (which
2034 * must be a finite non-negative number), minPrecision, and maxPrecision (both
2035 * must be integers between 1 and 21) the following steps are taken:
2036 */
2037 function ToRawPrecision(x, minPrecision, maxPrecision) {
2038 // 1. Let p be maxPrecision.
2039 var p = maxPrecision;
2040
2041 var m = void 0,
2042 e = void 0;
2043
2044 // 2. If x = 0, then
2045 if (x === 0) {
2046 // a. Let m be the String consisting of p occurrences of the character "0".
2047 m = arrJoin.call(Array(p + 1), '0');
2048 // b. Let e be 0.
2049 e = 0;
2050 }
2051 // 3. Else
2052 else {
2053 // a. Let e and n be integers such that 10ᵖ⁻¹ ≤ n < 10ᵖ and for which the
2054 // exact mathematical value of n × 10ᵉ⁻ᵖ⁺¹ – x is as close to zero as
2055 // possible. If there are two such sets of e and n, pick the e and n for
2056 // which n × 10ᵉ⁻ᵖ⁺¹ is larger.
2057 e = log10Floor(Math.abs(x));
2058
2059 // Easier to get to m from here
2060 var f = Math.round(Math.exp(Math.abs(e - p + 1) * Math.LN10));
2061
2062 // b. Let m be the String consisting of the digits of the decimal
2063 // representation of n (in order, with no leading zeroes)
2064 m = String(Math.round(e - p + 1 < 0 ? x * f : x / f));
2065 }
2066
2067 // 4. If e ≥ p, then
2068 if (e >= p)
2069 // a. Return the concatenation of m and e-p+1 occurrences of the character "0".
2070 return m + arrJoin.call(Array(e - p + 1 + 1), '0');
2071
2072 // 5. If e = p-1, then
2073 else if (e === p - 1)
2074 // a. Return m.
2075 return m;
2076
2077 // 6. If e ≥ 0, then
2078 else if (e >= 0)
2079 // a. Let m be the concatenation of the first e+1 characters of m, the character
2080 // ".", and the remaining p–(e+1) characters of m.
2081 m = m.slice(0, e + 1) + '.' + m.slice(e + 1);
2082
2083 // 7. If e < 0, then
2084 else if (e < 0)
2085 // a. Let m be the concatenation of the String "0.", –(e+1) occurrences of the
2086 // character "0", and the string m.
2087 m = '0.' + arrJoin.call(Array(-(e + 1) + 1), '0') + m;
2088
2089 // 8. If m contains the character ".", and maxPrecision > minPrecision, then
2090 if (m.indexOf(".") >= 0 && maxPrecision > minPrecision) {
2091 // a. Let cut be maxPrecision – minPrecision.
2092 var cut = maxPrecision - minPrecision;
2093
2094 // b. Repeat while cut > 0 and the last character of m is "0":
2095 while (cut > 0 && m.charAt(m.length - 1) === '0') {
2096 // i. Remove the last character from m.
2097 m = m.slice(0, -1);
2098
2099 // ii. Decrease cut by 1.
2100 cut--;
2101 }
2102
2103 // c. If the last character of m is ".", then
2104 if (m.charAt(m.length - 1) === '.')
2105 // i. Remove the last character from m.
2106 m = m.slice(0, -1);
2107 }
2108 // 9. Return m.
2109 return m;
2110 }
2111
2112 /**
2113 * @spec[tc39/ecma402/master/spec/numberformat.html]
2114 * @clause[sec-torawfixed]
2115 * When the ToRawFixed abstract operation is called with arguments x (which must
2116 * be a finite non-negative number), minInteger (which must be an integer between
2117 * 1 and 21), minFraction, and maxFraction (which must be integers between 0 and
2118 * 20) the following steps are taken:
2119 */
2120 function ToRawFixed(x, minInteger, minFraction, maxFraction) {
2121 // 1. Let f be maxFraction.
2122 var f = maxFraction;
2123 // 2. Let n be an integer for which the exact mathematical value of n ÷ 10f – x is as close to zero as possible. If there are two such n, pick the larger n.
2124 var n = Math.pow(10, f) * x; // diverging...
2125 // 3. If n = 0, let m be the String "0". Otherwise, let m be the String consisting of the digits of the decimal representation of n (in order, with no leading zeroes).
2126 var m = n === 0 ? "0" : n.toFixed(0); // divering...
2127
2128 {
2129 // this diversion is needed to take into consideration big numbers, e.g.:
2130 // 1.2344501e+37 -> 12344501000000000000000000000000000000
2131 var idx = void 0;
2132 var exp = (idx = m.indexOf('e')) > -1 ? m.slice(idx + 1) : 0;
2133 if (exp) {
2134 m = m.slice(0, idx).replace('.', '');
2135 m += arrJoin.call(Array(exp - (m.length - 1) + 1), '0');
2136 }
2137 }
2138
2139 var int = void 0;
2140 // 4. If f ≠ 0, then
2141 if (f !== 0) {
2142 // a. Let k be the number of characters in m.
2143 var k = m.length;
2144 // a. If k ≤ f, then
2145 if (k <= f) {
2146 // i. Let z be the String consisting of f+1–k occurrences of the character "0".
2147 var z = arrJoin.call(Array(f + 1 - k + 1), '0');
2148 // ii. Let m be the concatenation of Strings z and m.
2149 m = z + m;
2150 // iii. Let k be f+1.
2151 k = f + 1;
2152 }
2153 // a. Let a be the first k–f characters of m, and let b be the remaining f characters of m.
2154 var a = m.substring(0, k - f),
2155 b = m.substring(k - f, m.length);
2156 // a. Let m be the concatenation of the three Strings a, ".", and b.
2157 m = a + "." + b;
2158 // a. Let int be the number of characters in a.
2159 int = a.length;
2160 }
2161 // 5. Else, let int be the number of characters in m.
2162 else int = m.length;
2163 // 6. Let cut be maxFraction – minFraction.
2164 var cut = maxFraction - minFraction;
2165 // 7. Repeat while cut > 0 and the last character of m is "0":
2166 while (cut > 0 && m.slice(-1) === "0") {
2167 // a. Remove the last character from m.
2168 m = m.slice(0, -1);
2169 // a. Decrease cut by 1.
2170 cut--;
2171 }
2172 // 8. If the last character of m is ".", then
2173 if (m.slice(-1) === ".") {
2174 // a. Remove the last character from m.
2175 m = m.slice(0, -1);
2176 }
2177 // 9. If int < minInteger, then
2178 if (int < minInteger) {
2179 // a. Let z be the String consisting of minInteger–int occurrences of the character "0".
2180 var _z = arrJoin.call(Array(minInteger - int + 1), '0');
2181 // a. Let m be the concatenation of Strings z and m.
2182 m = _z + m;
2183 }
2184 // 10. Return m.
2185 return m;
2186 }
2187
2188 // Sect 11.3.2 Table 2, Numbering systems
2189 // ======================================
2190 var numSys = {
2191 arab: ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"],
2192 arabext: ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"],
2193 bali: ["᭐", "᭑", "᭒", "᭓", "᭔", "᭕", "᭖", "᭗", "᭘", "᭙"],
2194 beng: ["০", "১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯"],
2195 deva: ["०", "१", "२", "३", "४", "५", "६", "७", "८", "९"],
2196 fullwide: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
2197 gujr: ["૦", "૧", "૨", "૩", "૪", "૫", "૬", "૭", "૮", "૯"],
2198 guru: ["੦", "੧", "੨", "੩", "੪", "੫", "੬", "੭", "੮", "੯"],
2199 hanidec: ["〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"],
2200 khmr: ["០", "១", "២", "៣", "៤", "៥", "៦", "៧", "៨", "៩"],
2201 knda: ["೦", "೧", "೨", "೩", "೪", "೫", "೬", "೭", "೮", "೯"],
2202 laoo: ["໐", "໑", "໒", "໓", "໔", "໕", "໖", "໗", "໘", "໙"],
2203 latn: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
2204 limb: ["᥆", "᥇", "᥈", "᥉", "᥊", "᥋", "᥌", "᥍", "᥎", "᥏"],
2205 mlym: ["൦", "൧", "൨", "൩", "൪", "൫", "൬", "൭", "൮", "൯"],
2206 mong: ["᠐", "᠑", "᠒", "᠓", "᠔", "᠕", "᠖", "᠗", "᠘", "᠙"],
2207 mymr: ["၀", "၁", "၂", "၃", "၄", "၅", "၆", "၇", "၈", "၉"],
2208 orya: ["୦", "୧", "୨", "୩", "୪", "୫", "୬", "୭", "୮", "୯"],
2209 tamldec: ["௦", "௧", "௨", "௩", "௪", "௫", "௬", "௭", "௮", "௯"],
2210 telu: ["౦", "౧", "౨", "౩", "౪", "౫", "౬", "౭", "౮", "౯"],
2211 thai: ["๐", "๑", "๒", "๓", "๔", "๕", "๖", "๗", "๘", "๙"],
2212 tibt: ["༠", "༡", "༢", "༣", "༤", "༥", "༦", "༧", "༨", "༩"]
2213 };
2214
2215 /**
2216 * This function provides access to the locale and formatting options computed
2217 * during initialization of the object.
2218 *
2219 * The function returns a new object whose properties and attributes are set as
2220 * if constructed by an object literal assigning to each of the following
2221 * properties the value of the corresponding internal property of this
2222 * NumberFormat object (see 11.4): locale, numberingSystem, style, currency,
2223 * currencyDisplay, minimumIntegerDigits, minimumFractionDigits,
2224 * maximumFractionDigits, minimumSignificantDigits, maximumSignificantDigits, and
2225 * useGrouping. Properties whose corresponding internal properties are not present
2226 * are not assigned.
2227 */
2228 /* 11.3.3 */defineProperty(Intl$1.NumberFormat.prototype, 'resolvedOptions', {
2229 configurable: true,
2230 writable: true,
2231 value: function value() {
2232 var prop = void 0,
2233 descs = new Record(),
2234 props = ['locale', 'numberingSystem', 'style', 'currency', 'currencyDisplay', 'minimumIntegerDigits', 'minimumFractionDigits', 'maximumFractionDigits', 'minimumSignificantDigits', 'maximumSignificantDigits', 'useGrouping'],
2235 internal = this !== null && babelHelpers["typeof"](this) === 'object' && getInternalProperties(this);
2236
2237 // Satisfy test 11.3_b
2238 if (!internal || !internal['[[initializedNumberFormat]]']) throw new TypeError('`this` value for resolvedOptions() is not an initialized Intl.NumberFormat object.');
2239
2240 for (var i = 0, max = props.length; i < max; i++) {
2241 if (hop.call(internal, prop = '[[' + props[i] + ']]')) descs[props[i]] = { value: internal[prop], writable: true, configurable: true, enumerable: true };
2242 }
2243
2244 return objCreate({}, descs);
2245 }
2246 });
2247
2248 /* jslint esnext: true */
2249
2250 // Match these datetime components in a CLDR pattern, except those in single quotes
2251 var expDTComponents = /(?:[Eec]{1,6}|G{1,5}|[Qq]{1,5}|(?:[yYur]+|U{1,5})|[ML]{1,5}|d{1,2}|D{1,3}|F{1}|[abB]{1,5}|[hkHK]{1,2}|w{1,2}|W{1}|m{1,2}|s{1,2}|[zZOvVxX]{1,4})(?=([^']*'[^']*')*[^']*$)/g;
2252 // trim patterns after transformations
2253 var expPatternTrimmer = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
2254 // Skip over patterns with these datetime components because we don't have data
2255 // to back them up:
2256 // timezone, weekday, amoung others
2257 var unwantedDTCs = /[rqQASjJgwWIQq]/; // xXVO were removed from this list in favor of computing matches with timeZoneName values but printing as empty string
2258
2259 var dtKeys = ["weekday", "era", "year", "month", "day", "weekday", "quarter"];
2260 var tmKeys = ["hour", "minute", "second", "hour12", "timeZoneName"];
2261
2262 function isDateFormatOnly(obj) {
2263 for (var i = 0; i < tmKeys.length; i += 1) {
2264 if (obj.hasOwnProperty(tmKeys[i])) {
2265 return false;
2266 }
2267 }
2268 return true;
2269 }
2270
2271 function isTimeFormatOnly(obj) {
2272 for (var i = 0; i < dtKeys.length; i += 1) {
2273 if (obj.hasOwnProperty(dtKeys[i])) {
2274 return false;
2275 }
2276 }
2277 return true;
2278 }
2279
2280 function joinDateAndTimeFormats(dateFormatObj, timeFormatObj) {
2281 var o = { _: {} };
2282 for (var i = 0; i < dtKeys.length; i += 1) {
2283 if (dateFormatObj[dtKeys[i]]) {
2284 o[dtKeys[i]] = dateFormatObj[dtKeys[i]];
2285 }
2286 if (dateFormatObj._[dtKeys[i]]) {
2287 o._[dtKeys[i]] = dateFormatObj._[dtKeys[i]];
2288 }
2289 }
2290 for (var j = 0; j < tmKeys.length; j += 1) {
2291 if (timeFormatObj[tmKeys[j]]) {
2292 o[tmKeys[j]] = timeFormatObj[tmKeys[j]];
2293 }
2294 if (timeFormatObj._[tmKeys[j]]) {
2295 o._[tmKeys[j]] = timeFormatObj._[tmKeys[j]];
2296 }
2297 }
2298 return o;
2299 }
2300
2301 function computeFinalPatterns(formatObj) {
2302 // From http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns:
2303 // 'In patterns, two single quotes represents a literal single quote, either
2304 // inside or outside single quotes. Text within single quotes is not
2305 // interpreted in any way (except for two adjacent single quotes).'
2306 formatObj.pattern12 = formatObj.extendedPattern.replace(/'([^']*)'/g, function ($0, literal) {
2307 return literal ? literal : "'";
2308 });
2309
2310 // pattern 12 is always the default. we can produce the 24 by removing {ampm}
2311 formatObj.pattern = formatObj.pattern12.replace('{ampm}', '').replace(expPatternTrimmer, '');
2312 return formatObj;
2313 }
2314
2315 function expDTComponentsMeta($0, formatObj) {
2316 switch ($0.charAt(0)) {
2317 // --- Era
2318 case 'G':
2319 formatObj.era = ['short', 'short', 'short', 'long', 'narrow'][$0.length - 1];
2320 return '{era}';
2321
2322 // --- Year
2323 case 'y':
2324 case 'Y':
2325 case 'u':
2326 case 'U':
2327 case 'r':
2328 formatObj.year = $0.length === 2 ? '2-digit' : 'numeric';
2329 return '{year}';
2330
2331 // --- Quarter (not supported in this polyfill)
2332 case 'Q':
2333 case 'q':
2334 formatObj.quarter = ['numeric', '2-digit', 'short', 'long', 'narrow'][$0.length - 1];
2335 return '{quarter}';
2336
2337 // --- Month
2338 case 'M':
2339 case 'L':
2340 formatObj.month = ['numeric', '2-digit', 'short', 'long', 'narrow'][$0.length - 1];
2341 return '{month}';
2342
2343 // --- Week (not supported in this polyfill)
2344 case 'w':
2345 // week of the year
2346 formatObj.week = $0.length === 2 ? '2-digit' : 'numeric';
2347 return '{weekday}';
2348 case 'W':
2349 // week of the month
2350 formatObj.week = 'numeric';
2351 return '{weekday}';
2352
2353 // --- Day
2354 case 'd':
2355 // day of the month
2356 formatObj.day = $0.length === 2 ? '2-digit' : 'numeric';
2357 return '{day}';
2358 case 'D': // day of the year
2359 case 'F': // day of the week
2360 case 'g':
2361 // 1..n: Modified Julian day
2362 formatObj.day = 'numeric';
2363 return '{day}';
2364
2365 // --- Week Day
2366 case 'E':
2367 // day of the week
2368 formatObj.weekday = ['short', 'short', 'short', 'long', 'narrow', 'short'][$0.length - 1];
2369 return '{weekday}';
2370 case 'e':
2371 // local day of the week
2372 formatObj.weekday = ['numeric', '2-digit', 'short', 'long', 'narrow', 'short'][$0.length - 1];
2373 return '{weekday}';
2374 case 'c':
2375 // stand alone local day of the week
2376 formatObj.weekday = ['numeric', undefined, 'short', 'long', 'narrow', 'short'][$0.length - 1];
2377 return '{weekday}';
2378
2379 // --- Period
2380 case 'a': // AM, PM
2381 case 'b': // am, pm, noon, midnight
2382 case 'B':
2383 // flexible day periods
2384 formatObj.hour12 = true;
2385 return '{ampm}';
2386
2387 // --- Hour
2388 case 'h':
2389 case 'H':
2390 formatObj.hour = $0.length === 2 ? '2-digit' : 'numeric';
2391 return '{hour}';
2392 case 'k':
2393 case 'K':
2394 formatObj.hour12 = true; // 12-hour-cycle time formats (using h or K)
2395 formatObj.hour = $0.length === 2 ? '2-digit' : 'numeric';
2396 return '{hour}';
2397
2398 // --- Minute
2399 case 'm':
2400 formatObj.minute = $0.length === 2 ? '2-digit' : 'numeric';
2401 return '{minute}';
2402
2403 // --- Second
2404 case 's':
2405 formatObj.second = $0.length === 2 ? '2-digit' : 'numeric';
2406 return '{second}';
2407 case 'S':
2408 case 'A':
2409 formatObj.second = 'numeric';
2410 return '{second}';
2411
2412 // --- Timezone
2413 case 'z': // 1..3, 4: specific non-location format
2414 case 'Z': // 1..3, 4, 5: The ISO8601 varios formats
2415 case 'O': // 1, 4: miliseconds in day short, long
2416 case 'v': // 1, 4: generic non-location format
2417 case 'V': // 1, 2, 3, 4: time zone ID or city
2418 case 'X': // 1, 2, 3, 4: The ISO8601 varios formats
2419 case 'x':
2420 // 1, 2, 3, 4: The ISO8601 varios formats
2421 // this polyfill only supports much, for now, we are just doing something dummy
2422 formatObj.timeZoneName = $0.length < 4 ? 'short' : 'long';
2423 return '{timeZoneName}';
2424 }
2425 }
2426
2427 /**
2428 * Converts the CLDR availableFormats into the objects and patterns required by
2429 * the ECMAScript Internationalization API specification.
2430 */
2431 function createDateTimeFormat(skeleton, pattern) {
2432 // we ignore certain patterns that are unsupported to avoid this expensive op.
2433 if (unwantedDTCs.test(pattern)) return undefined;
2434
2435 var formatObj = {
2436 originalPattern: pattern,
2437 _: {}
2438 };
2439
2440 // Replace the pattern string with the one required by the specification, whilst
2441 // at the same time evaluating it for the subsets and formats
2442 formatObj.extendedPattern = pattern.replace(expDTComponents, function ($0) {
2443 // See which symbol we're dealing with
2444 return expDTComponentsMeta($0, formatObj._);
2445 });
2446
2447 // Match the skeleton string with the one required by the specification
2448 // this implementation is based on the Date Field Symbol Table:
2449 // http://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
2450 // Note: we are adding extra data to the formatObject even though this polyfill
2451 // might not support it.
2452 skeleton.replace(expDTComponents, function ($0) {
2453 // See which symbol we're dealing with
2454 return expDTComponentsMeta($0, formatObj);
2455 });
2456
2457 return computeFinalPatterns(formatObj);
2458 }
2459
2460 /**
2461 * Processes DateTime formats from CLDR to an easier-to-parse format.
2462 * the result of this operation should be cached the first time a particular
2463 * calendar is analyzed.
2464 *
2465 * The specification requires we support at least the following subsets of
2466 * date/time components:
2467 *
2468 * - 'weekday', 'year', 'month', 'day', 'hour', 'minute', 'second'
2469 * - 'weekday', 'year', 'month', 'day'
2470 * - 'year', 'month', 'day'
2471 * - 'year', 'month'
2472 * - 'month', 'day'
2473 * - 'hour', 'minute', 'second'
2474 * - 'hour', 'minute'
2475 *
2476 * We need to cherry pick at least these subsets from the CLDR data and convert
2477 * them into the pattern objects used in the ECMA-402 API.
2478 */
2479 function createDateTimeFormats(formats) {
2480 var availableFormats = formats.availableFormats;
2481 var timeFormats = formats.timeFormats;
2482 var dateFormats = formats.dateFormats;
2483 var result = [];
2484 var skeleton = void 0,
2485 pattern = void 0,
2486 computed = void 0,
2487 i = void 0,
2488 j = void 0;
2489 var timeRelatedFormats = [];
2490 var dateRelatedFormats = [];
2491
2492 // Map available (custom) formats into a pattern for createDateTimeFormats
2493 for (skeleton in availableFormats) {
2494 if (availableFormats.hasOwnProperty(skeleton)) {
2495 pattern = availableFormats[skeleton];
2496 computed = createDateTimeFormat(skeleton, pattern);
2497 if (computed) {
2498 result.push(computed);
2499 // in some cases, the format is only displaying date specific props
2500 // or time specific props, in which case we need to also produce the
2501 // combined formats.
2502 if (isDateFormatOnly(computed)) {
2503 dateRelatedFormats.push(computed);
2504 } else if (isTimeFormatOnly(computed)) {
2505 timeRelatedFormats.push(computed);
2506 }
2507 }
2508 }
2509 }
2510
2511 // Map time formats into a pattern for createDateTimeFormats
2512 for (skeleton in timeFormats) {
2513 if (timeFormats.hasOwnProperty(skeleton)) {
2514 pattern = timeFormats[skeleton];
2515 computed = createDateTimeFormat(skeleton, pattern);
2516 if (computed) {
2517 result.push(computed);
2518 timeRelatedFormats.push(computed);
2519 }
2520 }
2521 }
2522
2523 // Map date formats into a pattern for createDateTimeFormats
2524 for (skeleton in dateFormats) {
2525 if (dateFormats.hasOwnProperty(skeleton)) {
2526 pattern = dateFormats[skeleton];
2527 computed = createDateTimeFormat(skeleton, pattern);
2528 if (computed) {
2529 result.push(computed);
2530 dateRelatedFormats.push(computed);
2531 }
2532 }
2533 }
2534
2535 // combine custom time and custom date formats when they are orthogonals to complete the
2536 // formats supported by CLDR.
2537 // This Algo is based on section "Missing Skeleton Fields" from:
2538 // http://unicode.org/reports/tr35/tr35-dates.html#availableFormats_appendItems
2539 for (i = 0; i < timeRelatedFormats.length; i += 1) {
2540 for (j = 0; j < dateRelatedFormats.length; j += 1) {
2541 if (dateRelatedFormats[j].month === 'long') {
2542 pattern = dateRelatedFormats[j].weekday ? formats.full : formats.long;
2543 } else if (dateRelatedFormats[j].month === 'short') {
2544 pattern = formats.medium;
2545 } else {
2546 pattern = formats.short;
2547 }
2548 computed = joinDateAndTimeFormats(dateRelatedFormats[j], timeRelatedFormats[i]);
2549 computed.originalPattern = pattern;
2550 computed.extendedPattern = pattern.replace('{0}', timeRelatedFormats[i].extendedPattern).replace('{1}', dateRelatedFormats[j].extendedPattern).replace(/^[,\s]+|[,\s]+$/gi, '');
2551 result.push(computeFinalPatterns(computed));
2552 }
2553 }
2554
2555 return result;
2556 }
2557
2558 // An object map of date component keys, saves using a regex later
2559 var dateWidths = objCreate(null, { narrow: {}, short: {}, long: {} });
2560
2561 /**
2562 * Returns a string for a date component, resolved using multiple inheritance as specified
2563 * as specified in the Unicode Technical Standard 35.
2564 */
2565 function resolveDateString(data, ca, component, width, key) {
2566 // From http://www.unicode.org/reports/tr35/tr35.html#Multiple_Inheritance:
2567 // 'In clearly specified instances, resources may inherit from within the same locale.
2568 // For example, ... the Buddhist calendar inherits from the Gregorian calendar.'
2569 var obj = data[ca] && data[ca][component] ? data[ca][component] : data.gregory[component],
2570
2571
2572 // "sideways" inheritance resolves strings when a key doesn't exist
2573 alts = {
2574 narrow: ['short', 'long'],
2575 short: ['long', 'narrow'],
2576 long: ['short', 'narrow']
2577 },
2578
2579
2580 //
2581 resolved = hop.call(obj, width) ? obj[width] : hop.call(obj, alts[width][0]) ? obj[alts[width][0]] : obj[alts[width][1]];
2582
2583 // `key` wouldn't be specified for components 'dayPeriods'
2584 return key !== null ? resolved[key] : resolved;
2585 }
2586
2587 // Define the DateTimeFormat constructor internally so it cannot be tainted
2588 function DateTimeFormatConstructor() {
2589 var locales = arguments[0];
2590 var options = arguments[1];
2591
2592 if (!this || this === Intl$1) {
2593 return new Intl$1.DateTimeFormat(locales, options);
2594 }
2595 return InitializeDateTimeFormat(toObject(this), locales, options);
2596 }
2597
2598 defineProperty(Intl$1, 'DateTimeFormat', {
2599 configurable: true,
2600 writable: true,
2601 value: DateTimeFormatConstructor
2602 });
2603
2604 // Must explicitly set prototypes as unwritable
2605 defineProperty(DateTimeFormatConstructor, 'prototype', {
2606 writable: false
2607 });
2608
2609 /**
2610 * The abstract operation InitializeDateTimeFormat accepts the arguments dateTimeFormat
2611 * (which must be an object), locales, and options. It initializes dateTimeFormat as a
2612 * DateTimeFormat object.
2613 */
2614 function /* 12.1.1.1 */InitializeDateTimeFormat(dateTimeFormat, locales, options) {
2615 // This will be a internal properties object if we're not already initialized
2616 var internal = getInternalProperties(dateTimeFormat);
2617
2618 // Create an object whose props can be used to restore the values of RegExp props
2619 var regexpState = createRegExpRestore();
2620
2621 // 1. If dateTimeFormat has an [[initializedIntlObject]] internal property with
2622 // value true, throw a TypeError exception.
2623 if (internal['[[initializedIntlObject]]'] === true) throw new TypeError('`this` object has already been initialized as an Intl object');
2624
2625 // Need this to access the `internal` object
2626 defineProperty(dateTimeFormat, '__getInternalProperties', {
2627 value: function value() {
2628 // NOTE: Non-standard, for internal use only
2629 if (arguments[0] === secret) return internal;
2630 }
2631 });
2632
2633 // 2. Set the [[initializedIntlObject]] internal property of numberFormat to true.
2634 internal['[[initializedIntlObject]]'] = true;
2635
2636 // 3. Let requestedLocales be the result of calling the CanonicalizeLocaleList
2637 // abstract operation (defined in 9.2.1) with argument locales.
2638 var requestedLocales = CanonicalizeLocaleList(locales);
2639
2640 // 4. Let options be the result of calling the ToDateTimeOptions abstract
2641 // operation (defined below) with arguments options, "any", and "date".
2642 options = ToDateTimeOptions(options, 'any', 'date');
2643
2644 // 5. Let opt be a new Record.
2645 var opt = new Record();
2646
2647 // 6. Let matcher be the result of calling the GetOption abstract operation
2648 // (defined in 9.2.9) with arguments options, "localeMatcher", "string", a List
2649 // containing the two String values "lookup" and "best fit", and "best fit".
2650 var matcher = GetOption(options, 'localeMatcher', 'string', new List('lookup', 'best fit'), 'best fit');
2651
2652 // 7. Set opt.[[localeMatcher]] to matcher.
2653 opt['[[localeMatcher]]'] = matcher;
2654
2655 // 8. Let DateTimeFormat be the standard built-in object that is the initial
2656 // value of Intl.DateTimeFormat.
2657 var DateTimeFormat = internals.DateTimeFormat; // This is what we *really* need
2658
2659 // 9. Let localeData be the value of the [[localeData]] internal property of
2660 // DateTimeFormat.
2661 var localeData = DateTimeFormat['[[localeData]]'];
2662
2663 // 10. Let r be the result of calling the ResolveLocale abstract operation
2664 // (defined in 9.2.5) with the [[availableLocales]] internal property of
2665 // DateTimeFormat, requestedLocales, opt, the [[relevantExtensionKeys]]
2666 // internal property of DateTimeFormat, and localeData.
2667 var r = ResolveLocale(DateTimeFormat['[[availableLocales]]'], requestedLocales, opt, DateTimeFormat['[[relevantExtensionKeys]]'], localeData);
2668
2669 // 11. Set the [[locale]] internal property of dateTimeFormat to the value of
2670 // r.[[locale]].
2671 internal['[[locale]]'] = r['[[locale]]'];
2672
2673 // 12. Set the [[calendar]] internal property of dateTimeFormat to the value of
2674 // r.[[ca]].
2675 internal['[[calendar]]'] = r['[[ca]]'];
2676
2677 // 13. Set the [[numberingSystem]] internal property of dateTimeFormat to the value of
2678 // r.[[nu]].
2679 internal['[[numberingSystem]]'] = r['[[nu]]'];
2680
2681 // The specification doesn't tell us to do this, but it's helpful later on
2682 internal['[[dataLocale]]'] = r['[[dataLocale]]'];
2683
2684 // 14. Let dataLocale be the value of r.[[dataLocale]].
2685 var dataLocale = r['[[dataLocale]]'];
2686
2687 // 15. Let tz be the result of calling the [[Get]] internal method of options with
2688 // argument "timeZone".
2689 var tz = options.timeZone;
2690
2691 // 16. If tz is not undefined, then
2692 if (tz !== undefined) {
2693 // a. Let tz be ToString(tz).
2694 // b. Convert tz to upper case as described in 6.1.
2695 // NOTE: If an implementation accepts additional time zone values, as permitted
2696 // under certain conditions by the Conformance clause, different casing
2697 // rules apply.
2698 tz = toLatinUpperCase(tz);
2699
2700 // c. If tz is not "UTC", then throw a RangeError exception.
2701 // ###TODO: accept more time zones###
2702 if (tz !== 'UTC') throw new RangeError('timeZone is not supported.');
2703 }
2704
2705 // 17. Set the [[timeZone]] internal property of dateTimeFormat to tz.
2706 internal['[[timeZone]]'] = tz;
2707
2708 // 18. Let opt be a new Record.
2709 opt = new Record();
2710
2711 // 19. For each row of Table 3, except the header row, do:
2712 for (var prop in dateTimeComponents) {
2713 if (!hop.call(dateTimeComponents, prop)) continue;
2714
2715 // 20. Let prop be the name given in the Property column of the row.
2716 // 21. Let value be the result of calling the GetOption abstract operation,
2717 // passing as argument options, the name given in the Property column of the
2718 // row, "string", a List containing the strings given in the Values column of
2719 // the row, and undefined.
2720 var value = GetOption(options, prop, 'string', dateTimeComponents[prop]);
2721
2722 // 22. Set opt.[[<prop>]] to value.
2723 opt['[[' + prop + ']]'] = value;
2724 }
2725
2726 // Assigned a value below
2727 var bestFormat = void 0;
2728
2729 // 23. Let dataLocaleData be the result of calling the [[Get]] internal method of
2730 // localeData with argument dataLocale.
2731 var dataLocaleData = localeData[dataLocale];
2732
2733 // 24. Let formats be the result of calling the [[Get]] internal method of
2734 // dataLocaleData with argument "formats".
2735 // Note: we process the CLDR formats into the spec'd structure
2736 var formats = ToDateTimeFormats(dataLocaleData.formats);
2737
2738 // 25. Let matcher be the result of calling the GetOption abstract operation with
2739 // arguments options, "formatMatcher", "string", a List containing the two String
2740 // values "basic" and "best fit", and "best fit".
2741 matcher = GetOption(options, 'formatMatcher', 'string', new List('basic', 'best fit'), 'best fit');
2742
2743 // Optimization: caching the processed formats as a one time operation by
2744 // replacing the initial structure from localeData
2745 dataLocaleData.formats = formats;
2746
2747 // 26. If matcher is "basic", then
2748 if (matcher === 'basic') {
2749 // 27. Let bestFormat be the result of calling the BasicFormatMatcher abstract
2750 // operation (defined below) with opt and formats.
2751 bestFormat = BasicFormatMatcher(opt, formats);
2752
2753 // 28. Else
2754 } else {
2755 {
2756 // diverging
2757 var _hr = GetOption(options, 'hour12', 'boolean' /*, undefined, undefined*/);
2758 opt.hour12 = _hr === undefined ? dataLocaleData.hour12 : _hr;
2759 }
2760 // 29. Let bestFormat be the result of calling the BestFitFormatMatcher
2761 // abstract operation (defined below) with opt and formats.
2762 bestFormat = BestFitFormatMatcher(opt, formats);
2763 }
2764
2765 // 30. For each row in Table 3, except the header row, do
2766 for (var _prop in dateTimeComponents) {
2767 if (!hop.call(dateTimeComponents, _prop)) continue;
2768
2769 // a. Let prop be the name given in the Property column of the row.
2770 // b. Let pDesc be the result of calling the [[GetOwnProperty]] internal method of
2771 // bestFormat with argument prop.
2772 // c. If pDesc is not undefined, then
2773 if (hop.call(bestFormat, _prop)) {
2774 // i. Let p be the result of calling the [[Get]] internal method of bestFormat
2775 // with argument prop.
2776 var p = bestFormat[_prop];
2777 {
2778 // diverging
2779 p = bestFormat._ && hop.call(bestFormat._, _prop) ? bestFormat._[_prop] : p;
2780 }
2781
2782 // ii. Set the [[<prop>]] internal property of dateTimeFormat to p.
2783 internal['[[' + _prop + ']]'] = p;
2784 }
2785 }
2786
2787 var pattern = void 0; // Assigned a value below
2788
2789 // 31. Let hr12 be the result of calling the GetOption abstract operation with
2790 // arguments options, "hour12", "boolean", undefined, and undefined.
2791 var hr12 = GetOption(options, 'hour12', 'boolean' /*, undefined, undefined*/);
2792
2793 // 32. If dateTimeFormat has an internal property [[hour]], then
2794 if (internal['[[hour]]']) {
2795 // a. If hr12 is undefined, then let hr12 be the result of calling the [[Get]]
2796 // internal method of dataLocaleData with argument "hour12".
2797 hr12 = hr12 === undefined ? dataLocaleData.hour12 : hr12;
2798
2799 // b. Set the [[hour12]] internal property of dateTimeFormat to hr12.
2800 internal['[[hour12]]'] = hr12;
2801
2802 // c. If hr12 is true, then
2803 if (hr12 === true) {
2804 // i. Let hourNo0 be the result of calling the [[Get]] internal method of
2805 // dataLocaleData with argument "hourNo0".
2806 var hourNo0 = dataLocaleData.hourNo0;
2807
2808 // ii. Set the [[hourNo0]] internal property of dateTimeFormat to hourNo0.
2809 internal['[[hourNo0]]'] = hourNo0;
2810
2811 // iii. Let pattern be the result of calling the [[Get]] internal method of
2812 // bestFormat with argument "pattern12".
2813 pattern = bestFormat.pattern12;
2814 }
2815
2816 // d. Else
2817 else
2818 // i. Let pattern be the result of calling the [[Get]] internal method of
2819 // bestFormat with argument "pattern".
2820 pattern = bestFormat.pattern;
2821 }
2822
2823 // 33. Else
2824 else
2825 // a. Let pattern be the result of calling the [[Get]] internal method of
2826 // bestFormat with argument "pattern".
2827 pattern = bestFormat.pattern;
2828
2829 // 34. Set the [[pattern]] internal property of dateTimeFormat to pattern.
2830 internal['[[pattern]]'] = pattern;
2831
2832 // 35. Set the [[boundFormat]] internal property of dateTimeFormat to undefined.
2833 internal['[[boundFormat]]'] = undefined;
2834
2835 // 36. Set the [[initializedDateTimeFormat]] internal property of dateTimeFormat to
2836 // true.
2837 internal['[[initializedDateTimeFormat]]'] = true;
2838
2839 // In ES3, we need to pre-bind the format() function
2840 if (es3) dateTimeFormat.format = GetFormatDateTime.call(dateTimeFormat);
2841
2842 // Restore the RegExp properties
2843 regexpState.exp.test(regexpState.input);
2844
2845 // Return the newly initialised object
2846 return dateTimeFormat;
2847 }
2848
2849 /**
2850 * Several DateTimeFormat algorithms use values from the following table, which provides
2851 * property names and allowable values for the components of date and time formats:
2852 */
2853 var dateTimeComponents = {
2854 weekday: ["narrow", "short", "long"],
2855 era: ["narrow", "short", "long"],
2856 year: ["2-digit", "numeric"],
2857 month: ["2-digit", "numeric", "narrow", "short", "long"],
2858 day: ["2-digit", "numeric"],
2859 hour: ["2-digit", "numeric"],
2860 minute: ["2-digit", "numeric"],
2861 second: ["2-digit", "numeric"],
2862 timeZoneName: ["short", "long"]
2863 };
2864
2865 /**
2866 * When the ToDateTimeOptions abstract operation is called with arguments options,
2867 * required, and defaults, the following steps are taken:
2868 */
2869 function ToDateTimeFormats(formats) {
2870 if (Object.prototype.toString.call(formats) === '[object Array]') {
2871 return formats;
2872 }
2873 return createDateTimeFormats(formats);
2874 }
2875
2876 /**
2877 * When the ToDateTimeOptions abstract operation is called with arguments options,
2878 * required, and defaults, the following steps are taken:
2879 */
2880 function ToDateTimeOptions(options, required, defaults) {
2881 // 1. If options is undefined, then let options be null, else let options be
2882 // ToObject(options).
2883 if (options === undefined) options = null;else {
2884 // (#12) options needs to be a Record, but it also needs to inherit properties
2885 var opt2 = toObject(options);
2886 options = new Record();
2887
2888 for (var k in opt2) {
2889 options[k] = opt2[k];
2890 }
2891 }
2892
2893 // 2. Let create be the standard built-in function object defined in ES5, 15.2.3.5.
2894 var create = objCreate;
2895
2896 // 3. Let options be the result of calling the [[Call]] internal method of create with
2897 // undefined as the this value and an argument list containing the single item
2898 // options.
2899 options = create(options);
2900
2901 // 4. Let needDefaults be true.
2902 var needDefaults = true;
2903
2904 // 5. If required is "date" or "any", then
2905 if (required === 'date' || required === 'any') {
2906 // a. For each of the property names "weekday", "year", "month", "day":
2907 // i. If the result of calling the [[Get]] internal method of options with the
2908 // property name is not undefined, then let needDefaults be false.
2909 if (options.weekday !== undefined || options.year !== undefined || options.month !== undefined || options.day !== undefined) needDefaults = false;
2910 }
2911
2912 // 6. If required is "time" or "any", then
2913 if (required === 'time' || required === 'any') {
2914 // a. For each of the property names "hour", "minute", "second":
2915 // i. If the result of calling the [[Get]] internal method of options with the
2916 // property name is not undefined, then let needDefaults be false.
2917 if (options.hour !== undefined || options.minute !== undefined || options.second !== undefined) needDefaults = false;
2918 }
2919
2920 // 7. If needDefaults is true and defaults is either "date" or "all", then
2921 if (needDefaults && (defaults === 'date' || defaults === 'all'))
2922 // a. For each of the property names "year", "month", "day":
2923 // i. Call the [[DefineOwnProperty]] internal method of options with the
2924 // property name, Property Descriptor {[[Value]]: "numeric", [[Writable]]:
2925 // true, [[Enumerable]]: true, [[Configurable]]: true}, and false.
2926 options.year = options.month = options.day = 'numeric';
2927
2928 // 8. If needDefaults is true and defaults is either "time" or "all", then
2929 if (needDefaults && (defaults === 'time' || defaults === 'all'))
2930 // a. For each of the property names "hour", "minute", "second":
2931 // i. Call the [[DefineOwnProperty]] internal method of options with the
2932 // property name, Property Descriptor {[[Value]]: "numeric", [[Writable]]:
2933 // true, [[Enumerable]]: true, [[Configurable]]: true}, and false.
2934 options.hour = options.minute = options.second = 'numeric';
2935
2936 // 9. Return options.
2937 return options;
2938 }
2939
2940 /**
2941 * When the BasicFormatMatcher abstract operation is called with two arguments options and
2942 * formats, the following steps are taken:
2943 */
2944 function BasicFormatMatcher(options, formats) {
2945 // 1. Let removalPenalty be 120.
2946 var removalPenalty = 120;
2947
2948 // 2. Let additionPenalty be 20.
2949 var additionPenalty = 20;
2950
2951 // 3. Let longLessPenalty be 8.
2952 var longLessPenalty = 8;
2953
2954 // 4. Let longMorePenalty be 6.
2955 var longMorePenalty = 6;
2956
2957 // 5. Let shortLessPenalty be 6.
2958 var shortLessPenalty = 6;
2959
2960 // 6. Let shortMorePenalty be 3.
2961 var shortMorePenalty = 3;
2962
2963 // 7. Let bestScore be -Infinity.
2964 var bestScore = -Infinity;
2965
2966 // 8. Let bestFormat be undefined.
2967 var bestFormat = void 0;
2968
2969 // 9. Let i be 0.
2970 var i = 0;
2971
2972 // 10. Assert: formats is an Array object.
2973
2974 // 11. Let len be the result of calling the [[Get]] internal method of formats with argument "length".
2975 var len = formats.length;
2976
2977 // 12. Repeat while i < len:
2978 while (i < len) {
2979 // a. Let format be the result of calling the [[Get]] internal method of formats with argument ToString(i).
2980 var format = formats[i];
2981
2982 // b. Let score be 0.
2983 var score = 0;
2984
2985 // c. For each property shown in Table 3:
2986 for (var property in dateTimeComponents) {
2987 if (!hop.call(dateTimeComponents, property)) continue;
2988
2989 // i. Let optionsProp be options.[[<property>]].
2990 var optionsProp = options['[[' + property + ']]'];
2991
2992 // ii. Let formatPropDesc be the result of calling the [[GetOwnProperty]] internal method of format
2993 // with argument property.
2994 // iii. If formatPropDesc is not undefined, then
2995 // 1. Let formatProp be the result of calling the [[Get]] internal method of format with argument property.
2996 var formatProp = hop.call(format, property) ? format[property] : undefined;
2997
2998 // iv. If optionsProp is undefined and formatProp is not undefined, then decrease score by
2999 // additionPenalty.
3000 if (optionsProp === undefined && formatProp !== undefined) score -= additionPenalty;
3001
3002 // v. Else if optionsProp is not undefined and formatProp is undefined, then decrease score by
3003 // removalPenalty.
3004 else if (optionsProp !== undefined && formatProp === undefined) score -= removalPenalty;
3005
3006 // vi. Else
3007 else {
3008 // 1. Let values be the array ["2-digit", "numeric", "narrow", "short",
3009 // "long"].
3010 var values = ['2-digit', 'numeric', 'narrow', 'short', 'long'];
3011
3012 // 2. Let optionsPropIndex be the index of optionsProp within values.
3013 var optionsPropIndex = arrIndexOf.call(values, optionsProp);
3014
3015 // 3. Let formatPropIndex be the index of formatProp within values.
3016 var formatPropIndex = arrIndexOf.call(values, formatProp);
3017
3018 // 4. Let delta be max(min(formatPropIndex - optionsPropIndex, 2), -2).
3019 var delta = Math.max(Math.min(formatPropIndex - optionsPropIndex, 2), -2);
3020
3021 // 5. If delta = 2, decrease score by longMorePenalty.
3022 if (delta === 2) score -= longMorePenalty;
3023
3024 // 6. Else if delta = 1, decrease score by shortMorePenalty.
3025 else if (delta === 1) score -= shortMorePenalty;
3026
3027 // 7. Else if delta = -1, decrease score by shortLessPenalty.
3028 else if (delta === -1) score -= shortLessPenalty;
3029
3030 // 8. Else if delta = -2, decrease score by longLessPenalty.
3031 else if (delta === -2) score -= longLessPenalty;
3032 }
3033 }
3034
3035 // d. If score > bestScore, then
3036 if (score > bestScore) {
3037 // i. Let bestScore be score.
3038 bestScore = score;
3039
3040 // ii. Let bestFormat be format.
3041 bestFormat = format;
3042 }
3043
3044 // e. Increase i by 1.
3045 i++;
3046 }
3047
3048 // 13. Return bestFormat.
3049 return bestFormat;
3050 }
3051
3052 /**
3053 * When the BestFitFormatMatcher abstract operation is called with two arguments options
3054 * and formats, it performs implementation dependent steps, which should return a set of
3055 * component representations that a typical user of the selected locale would perceive as
3056 * at least as good as the one returned by BasicFormatMatcher.
3057 *
3058 * This polyfill defines the algorithm to be the same as BasicFormatMatcher,
3059 * with the addition of bonus points awarded where the requested format is of
3060 * the same data type as the potentially matching format.
3061 *
3062 * This algo relies on the concept of closest distance matching described here:
3063 * http://unicode.org/reports/tr35/tr35-dates.html#Matching_Skeletons
3064 * Typically a “best match” is found using a closest distance match, such as:
3065 *
3066 * Symbols requesting a best choice for the locale are replaced.
3067 * j → one of {H, k, h, K}; C → one of {a, b, B}
3068 * -> Covered by cldr.js matching process
3069 *
3070 * For fields with symbols representing the same type (year, month, day, etc):
3071 * Most symbols have a small distance from each other.
3072 * M ≅ L; E ≅ c; a ≅ b ≅ B; H ≅ k ≅ h ≅ K; ...
3073 * -> Covered by cldr.js matching process
3074 *
3075 * Width differences among fields, other than those marking text vs numeric, are given small distance from each other.
3076 * MMM ≅ MMMM
3077 * MM ≅ M
3078 * Numeric and text fields are given a larger distance from each other.
3079 * MMM ≈ MM
3080 * Symbols representing substantial differences (week of year vs week of month) are given much larger a distances from each other.
3081 * d ≋ D; ...
3082 * Missing or extra fields cause a match to fail. (But see Missing Skeleton Fields).
3083 *
3084 *
3085 * For example,
3086 *
3087 * { month: 'numeric', day: 'numeric' }
3088 *
3089 * should match
3090 *
3091 * { month: '2-digit', day: '2-digit' }
3092 *
3093 * rather than
3094 *
3095 * { month: 'short', day: 'numeric' }
3096 *
3097 * This makes sense because a user requesting a formatted date with numeric parts would
3098 * not expect to see the returned format containing narrow, short or long part names
3099 */
3100 function BestFitFormatMatcher(options, formats) {
3101
3102 // 1. Let removalPenalty be 120.
3103 var removalPenalty = 120;
3104
3105 // 2. Let additionPenalty be 20.
3106 var additionPenalty = 20;
3107
3108 // 3. Let longLessPenalty be 8.
3109 var longLessPenalty = 8;
3110
3111 // 4. Let longMorePenalty be 6.
3112 var longMorePenalty = 6;
3113
3114 // 5. Let shortLessPenalty be 6.
3115 var shortLessPenalty = 6;
3116
3117 // 6. Let shortMorePenalty be 3.
3118 var shortMorePenalty = 3;
3119
3120 var hour12Penalty = 1;
3121
3122 // 7. Let bestScore be -Infinity.
3123 var bestScore = -Infinity;
3124
3125 // 8. Let bestFormat be undefined.
3126 var bestFormat = void 0;
3127
3128 // 9. Let i be 0.
3129 var i = 0;
3130
3131 // 10. Assert: formats is an Array object.
3132
3133 // 11. Let len be the result of calling the [[Get]] internal method of formats with argument "length".
3134 var len = formats.length;
3135
3136 // 12. Repeat while i < len:
3137 while (i < len) {
3138 // a. Let format be the result of calling the [[Get]] internal method of formats with argument ToString(i).
3139 var format = formats[i];
3140
3141 // b. Let score be 0.
3142 var score = 0;
3143
3144 // c. For each property shown in Table 3:
3145 for (var property in dateTimeComponents) {
3146 if (!hop.call(dateTimeComponents, property)) continue;
3147
3148 // i. Let optionsProp be options.[[<property>]].
3149 var optionsProp = options['[[' + property + ']]'];
3150
3151 // ii. Let formatPropDesc be the result of calling the [[GetOwnProperty]] internal method of format
3152 // with argument property.
3153 // iii. If formatPropDesc is not undefined, then
3154 // 1. Let formatProp be the result of calling the [[Get]] internal method of format with argument property.
3155 var formatProp = hop.call(format, property) ? format[property] : undefined;
3156
3157 // iv. If optionsProp is undefined and formatProp is not undefined, then decrease score by
3158 // additionPenalty.
3159 if (optionsProp === undefined && formatProp !== undefined) score -= additionPenalty;
3160
3161 // v. Else if optionsProp is not undefined and formatProp is undefined, then decrease score by
3162 // removalPenalty.
3163 else if (optionsProp !== undefined && formatProp === undefined) score -= removalPenalty;
3164
3165 // vi. Else
3166 else {
3167 // 1. Let values be the array ["2-digit", "numeric", "narrow", "short",
3168 // "long"].
3169 var values = ['2-digit', 'numeric', 'narrow', 'short', 'long'];
3170
3171 // 2. Let optionsPropIndex be the index of optionsProp within values.
3172 var optionsPropIndex = arrIndexOf.call(values, optionsProp);
3173
3174 // 3. Let formatPropIndex be the index of formatProp within values.
3175 var formatPropIndex = arrIndexOf.call(values, formatProp);
3176
3177 // 4. Let delta be max(min(formatPropIndex - optionsPropIndex, 2), -2).
3178 var delta = Math.max(Math.min(formatPropIndex - optionsPropIndex, 2), -2);
3179
3180 {
3181 // diverging from spec
3182 // When the bestFit argument is true, subtract additional penalty where data types are not the same
3183 if (formatPropIndex <= 1 && optionsPropIndex >= 2 || formatPropIndex >= 2 && optionsPropIndex <= 1) {
3184 // 5. If delta = 2, decrease score by longMorePenalty.
3185 if (delta > 0) score -= longMorePenalty;else if (delta < 0) score -= longLessPenalty;
3186 } else {
3187 // 5. If delta = 2, decrease score by longMorePenalty.
3188 if (delta > 1) score -= shortMorePenalty;else if (delta < -1) score -= shortLessPenalty;
3189 }
3190 }
3191 }
3192 }
3193
3194 {
3195 // diverging to also take into consideration differences between 12 or 24 hours
3196 // which is special for the best fit only.
3197 if (format._.hour12 !== options.hour12) {
3198 score -= hour12Penalty;
3199 }
3200 }
3201
3202 // d. If score > bestScore, then
3203 if (score > bestScore) {
3204 // i. Let bestScore be score.
3205 bestScore = score;
3206 // ii. Let bestFormat be format.
3207 bestFormat = format;
3208 }
3209
3210 // e. Increase i by 1.
3211 i++;
3212 }
3213
3214 // 13. Return bestFormat.
3215 return bestFormat;
3216 }
3217
3218 /* 12.2.3 */internals.DateTimeFormat = {
3219 '[[availableLocales]]': [],
3220 '[[relevantExtensionKeys]]': ['ca', 'nu'],
3221 '[[localeData]]': {}
3222 };
3223
3224 /**
3225 * When the supportedLocalesOf method of Intl.DateTimeFormat is called, the
3226 * following steps are taken:
3227 */
3228 /* 12.2.2 */
3229 defineProperty(Intl$1.DateTimeFormat, 'supportedLocalesOf', {
3230 configurable: true,
3231 writable: true,
3232 value: fnBind.call(function (locales) {
3233 // Bound functions only have the `this` value altered if being used as a constructor,
3234 // this lets us imitate a native function that has no constructor
3235 if (!hop.call(this, '[[availableLocales]]')) throw new TypeError('supportedLocalesOf() is not a constructor');
3236
3237 // Create an object whose props can be used to restore the values of RegExp props
3238 var regexpState = createRegExpRestore(),
3239
3240
3241 // 1. If options is not provided, then let options be undefined.
3242 options = arguments[1],
3243
3244
3245 // 2. Let availableLocales be the value of the [[availableLocales]] internal
3246 // property of the standard built-in object that is the initial value of
3247 // Intl.NumberFormat.
3248
3249 availableLocales = this['[[availableLocales]]'],
3250
3251
3252 // 3. Let requestedLocales be the result of calling the CanonicalizeLocaleList
3253 // abstract operation (defined in 9.2.1) with argument locales.
3254 requestedLocales = CanonicalizeLocaleList(locales);
3255
3256 // Restore the RegExp properties
3257 regexpState.exp.test(regexpState.input);
3258
3259 // 4. Return the result of calling the SupportedLocales abstract operation
3260 // (defined in 9.2.8) with arguments availableLocales, requestedLocales,
3261 // and options.
3262 return SupportedLocales(availableLocales, requestedLocales, options);
3263 }, internals.NumberFormat)
3264 });
3265
3266 /**
3267 * This named accessor property returns a function that formats a number
3268 * according to the effective locale and the formatting options of this
3269 * DateTimeFormat object.
3270 */
3271 /* 12.3.2 */defineProperty(Intl$1.DateTimeFormat.prototype, 'format', {
3272 configurable: true,
3273 get: GetFormatDateTime
3274 });
3275
3276 defineProperty(Intl$1.DateTimeFormat.prototype, 'formatToParts', {
3277 configurable: true,
3278 get: GetFormatToPartsDateTime
3279 });
3280
3281 function GetFormatDateTime() {
3282 var internal = this !== null && babelHelpers["typeof"](this) === 'object' && getInternalProperties(this);
3283
3284 // Satisfy test 12.3_b
3285 if (!internal || !internal['[[initializedDateTimeFormat]]']) throw new TypeError('`this` value for format() is not an initialized Intl.DateTimeFormat object.');
3286
3287 // The value of the [[Get]] attribute is a function that takes the following
3288 // steps:
3289
3290 // 1. If the [[boundFormat]] internal property of this DateTimeFormat object
3291 // is undefined, then:
3292 if (internal['[[boundFormat]]'] === undefined) {
3293 // a. Let F be a Function object, with internal properties set as
3294 // specified for built-in functions in ES5, 15, or successor, and the
3295 // length property set to 0, that takes the argument date and
3296 // performs the following steps:
3297 var F = function F() {
3298 // i. If date is not provided or is undefined, then let x be the
3299 // result as if by the expression Date.now() where Date.now is
3300 // the standard built-in function defined in ES5, 15.9.4.4.
3301 // ii. Else let x be ToNumber(date).
3302 // iii. Return the result of calling the FormatDateTime abstract
3303 // operation (defined below) with arguments this and x.
3304 var x = Number(arguments.length === 0 ? Date.now() : arguments[0]);
3305 return FormatDateTime(this, x);
3306 };
3307 // b. Let bind be the standard built-in function object defined in ES5,
3308 // 15.3.4.5.
3309 // c. Let bf be the result of calling the [[Call]] internal method of
3310 // bind with F as the this value and an argument list containing
3311 // the single item this.
3312 var bf = fnBind.call(F, this);
3313 // d. Set the [[boundFormat]] internal property of this NumberFormat
3314 // object to bf.
3315 internal['[[boundFormat]]'] = bf;
3316 }
3317 // Return the value of the [[boundFormat]] internal property of this
3318 // NumberFormat object.
3319 return internal['[[boundFormat]]'];
3320 }
3321
3322 function GetFormatToPartsDateTime() {
3323 var internal = this !== null && babelHelpers["typeof"](this) === 'object' && getInternalProperties(this);
3324
3325 if (!internal || !internal['[[initializedDateTimeFormat]]']) throw new TypeError('`this` value for formatToParts() is not an initialized Intl.DateTimeFormat object.');
3326
3327 if (internal['[[boundFormatToParts]]'] === undefined) {
3328 var F = function F() {
3329 var x = Number(arguments.length === 0 ? Date.now() : arguments[0]);
3330 return FormatToPartsDateTime(this, x);
3331 };
3332 var bf = fnBind.call(F, this);
3333 internal['[[boundFormatToParts]]'] = bf;
3334 }
3335 return internal['[[boundFormatToParts]]'];
3336 }
3337
3338 function CreateDateTimeParts(dateTimeFormat, x) {
3339 // 1. If x is not a finite Number, then throw a RangeError exception.
3340 if (!isFinite(x)) throw new RangeError('Invalid valid date passed to format');
3341
3342 var internal = dateTimeFormat.__getInternalProperties(secret);
3343
3344 // Creating restore point for properties on the RegExp object... please wait
3345 /* let regexpState = */createRegExpRestore(); // ###TODO: review this
3346
3347 // 2. Let locale be the value of the [[locale]] internal property of dateTimeFormat.
3348 var locale = internal['[[locale]]'];
3349
3350 // 3. Let nf be the result of creating a new NumberFormat object as if by the
3351 // expression new Intl.NumberFormat([locale], {useGrouping: false}) where
3352 // Intl.NumberFormat is the standard built-in constructor defined in 11.1.3.
3353 var nf = new Intl$1.NumberFormat([locale], { useGrouping: false });
3354
3355 // 4. Let nf2 be the result of creating a new NumberFormat object as if by the
3356 // expression new Intl.NumberFormat([locale], {minimumIntegerDigits: 2, useGrouping:
3357 // false}) where Intl.NumberFormat is the standard built-in constructor defined in
3358 // 11.1.3.
3359 var nf2 = new Intl$1.NumberFormat([locale], { minimumIntegerDigits: 2, useGrouping: false });
3360
3361 // 5. Let tm be the result of calling the ToLocalTime abstract operation (defined
3362 // below) with x, the value of the [[calendar]] internal property of dateTimeFormat,
3363 // and the value of the [[timeZone]] internal property of dateTimeFormat.
3364 var tm = ToLocalTime(x, internal['[[calendar]]'], internal['[[timeZone]]']);
3365
3366 // 6. Let result be the value of the [[pattern]] internal property of dateTimeFormat.
3367 var pattern = internal['[[pattern]]'];
3368
3369 // 7.
3370 var result = new List();
3371
3372 // 8.
3373 var index = 0;
3374
3375 // 9.
3376 var beginIndex = pattern.indexOf('{');
3377
3378 // 10.
3379 var endIndex = 0;
3380
3381 // Need the locale minus any extensions
3382 var dataLocale = internal['[[dataLocale]]'];
3383
3384 // Need the calendar data from CLDR
3385 var localeData = internals.DateTimeFormat['[[localeData]]'][dataLocale].calendars;
3386 var ca = internal['[[calendar]]'];
3387
3388 // 11.
3389 while (beginIndex !== -1) {
3390 var fv = void 0;
3391 // a.
3392 endIndex = pattern.indexOf('}', beginIndex);
3393 // b.
3394 if (endIndex === -1) {
3395 throw new Error('Unclosed pattern');
3396 }
3397 // c.
3398 if (beginIndex > index) {
3399 arrPush.call(result, {
3400 type: 'literal',
3401 value: pattern.substring(index, beginIndex)
3402 });
3403 }
3404 // d.
3405 var p = pattern.substring(beginIndex + 1, endIndex);
3406 // e.
3407 if (dateTimeComponents.hasOwnProperty(p)) {
3408 // i. Let f be the value of the [[<p>]] internal property of dateTimeFormat.
3409 var f = internal['[[' + p + ']]'];
3410 // ii. Let v be the value of tm.[[<p>]].
3411 var v = tm['[[' + p + ']]'];
3412 // iii. If p is "year" and v ≤ 0, then let v be 1 - v.
3413 if (p === 'year' && v <= 0) {
3414 v = 1 - v;
3415 }
3416 // iv. If p is "month", then increase v by 1.
3417 else if (p === 'month') {
3418 v++;
3419 }
3420 // v. If p is "hour" and the value of the [[hour12]] internal property of
3421 // dateTimeFormat is true, then
3422 else if (p === 'hour' && internal['[[hour12]]'] === true) {
3423 // 1. Let v be v modulo 12.
3424 v = v % 12;
3425 // 2. If v is 0 and the value of the [[hourNo0]] internal property of
3426 // dateTimeFormat is true, then let v be 12.
3427 if (v === 0 && internal['[[hourNo0]]'] === true) {
3428 v = 12;
3429 }
3430 }
3431
3432 // vi. If f is "numeric", then
3433 if (f === 'numeric') {
3434 // 1. Let fv be the result of calling the FormatNumber abstract operation
3435 // (defined in 11.3.2) with arguments nf and v.
3436 fv = FormatNumber(nf, v);
3437 }
3438 // vii. Else if f is "2-digit", then
3439 else if (f === '2-digit') {
3440 // 1. Let fv be the result of calling the FormatNumber abstract operation
3441 // with arguments nf2 and v.
3442 fv = FormatNumber(nf2, v);
3443 // 2. If the length of fv is greater than 2, let fv be the substring of fv
3444 // containing the last two characters.
3445 if (fv.length > 2) {
3446 fv = fv.slice(-2);
3447 }
3448 }
3449 // viii. Else if f is "narrow", "short", or "long", then let fv be a String
3450 // value representing f in the desired form; the String value depends upon
3451 // the implementation and the effective locale and calendar of
3452 // dateTimeFormat. If p is "month", then the String value may also depend
3453 // on whether dateTimeFormat has a [[day]] internal property. If p is
3454 // "timeZoneName", then the String value may also depend on the value of
3455 // the [[inDST]] field of tm.
3456 else if (f in dateWidths) {
3457 switch (p) {
3458 case 'month':
3459 fv = resolveDateString(localeData, ca, 'months', f, tm['[[' + p + ']]']);
3460 break;
3461
3462 case 'weekday':
3463 try {
3464 fv = resolveDateString(localeData, ca, 'days', f, tm['[[' + p + ']]']);
3465 // fv = resolveDateString(ca.days, f)[tm['[['+ p +']]']];
3466 } catch (e) {
3467 throw new Error('Could not find weekday data for locale ' + locale);
3468 }
3469 break;
3470
3471 case 'timeZoneName':
3472 fv = ''; // ###TODO
3473 break;
3474
3475 case 'era':
3476 try {
3477 fv = resolveDateString(localeData, ca, 'eras', f, tm['[[' + p + ']]']);
3478 } catch (e) {
3479 throw new Error('Could not find era data for locale ' + locale);
3480 }
3481 break;
3482
3483 default:
3484 fv = tm['[[' + p + ']]'];
3485 }
3486 }
3487 // ix
3488 arrPush.call(result, {
3489 type: p,
3490 value: fv
3491 });
3492 // f.
3493 } else if (p === 'ampm') {
3494 // i.
3495 var _v = tm['[[hour]]'];
3496 // ii./iii.
3497 fv = resolveDateString(localeData, ca, 'dayPeriods', _v > 11 ? 'pm' : 'am', null);
3498 // iv.
3499 arrPush.call(result, {
3500 type: 'dayPeriod',
3501 value: fv
3502 });
3503 // g.
3504 } else {
3505 arrPush.call(result, {
3506 type: 'literal',
3507 value: pattern.substring(beginIndex, endIndex + 1)
3508 });
3509 }
3510 // h.
3511 index = endIndex + 1;
3512 // i.
3513 beginIndex = pattern.indexOf('{', index);
3514 }
3515 // 12.
3516 if (endIndex < pattern.length - 1) {
3517 arrPush.call(result, {
3518 type: 'literal',
3519 value: pattern.substr(endIndex + 1)
3520 });
3521 }
3522 // 13.
3523 return result;
3524 }
3525
3526 /**
3527 * When the FormatDateTime abstract operation is called with arguments dateTimeFormat
3528 * (which must be an object initialized as a DateTimeFormat) and x (which must be a Number
3529 * value), it returns a String value representing x (interpreted as a time value as
3530 * specified in ES5, 15.9.1.1) according to the effective locale and the formatting
3531 * options of dateTimeFormat.
3532 */
3533 function FormatDateTime(dateTimeFormat, x) {
3534 var parts = CreateDateTimeParts(dateTimeFormat, x);
3535 var result = '';
3536
3537 for (var i = 0; parts.length > i; i++) {
3538 var part = parts[i];
3539 result += part.value;
3540 }
3541 return result;
3542 }
3543
3544 function FormatToPartsDateTime(dateTimeFormat, x) {
3545 var parts = CreateDateTimeParts(dateTimeFormat, x);
3546 var result = [];
3547 for (var i = 0; parts.length > i; i++) {
3548 var part = parts[i];
3549 result.push({
3550 type: part.type,
3551 value: part.value
3552 });
3553 }
3554 return result;
3555 }
3556
3557 /**
3558 * When the ToLocalTime abstract operation is called with arguments date, calendar, and
3559 * timeZone, the following steps are taken:
3560 */
3561 function ToLocalTime(date, calendar, timeZone) {
3562 // 1. Apply calendrical calculations on date for the given calendar and time zone to
3563 // produce weekday, era, year, month, day, hour, minute, second, and inDST values.
3564 // The calculations should use best available information about the specified
3565 // calendar and time zone. If the calendar is "gregory", then the calculations must
3566 // match the algorithms specified in ES5, 15.9.1, except that calculations are not
3567 // bound by the restrictions on the use of best available information on time zones
3568 // for local time zone adjustment and daylight saving time adjustment imposed by
3569 // ES5, 15.9.1.7 and 15.9.1.8.
3570 // ###TODO###
3571 var d = new Date(date),
3572 m = 'get' + (timeZone || '');
3573
3574 // 2. Return a Record with fields [[weekday]], [[era]], [[year]], [[month]], [[day]],
3575 // [[hour]], [[minute]], [[second]], and [[inDST]], each with the corresponding
3576 // calculated value.
3577 return new Record({
3578 '[[weekday]]': d[m + 'Day'](),
3579 '[[era]]': +(d[m + 'FullYear']() >= 0),
3580 '[[year]]': d[m + 'FullYear'](),
3581 '[[month]]': d[m + 'Month'](),
3582 '[[day]]': d[m + 'Date'](),
3583 '[[hour]]': d[m + 'Hours'](),
3584 '[[minute]]': d[m + 'Minutes'](),
3585 '[[second]]': d[m + 'Seconds'](),
3586 '[[inDST]]': false });
3587 }
3588
3589 /**
3590 * The function returns a new object whose properties and attributes are set as if
3591 * constructed by an object literal assigning to each of the following properties the
3592 * value of the corresponding internal property of this DateTimeFormat object (see 12.4):
3593 * locale, calendar, numberingSystem, timeZone, hour12, weekday, era, year, month, day,
3594 * hour, minute, second, and timeZoneName. Properties whose corresponding internal
3595 * properties are not present are not assigned.
3596 */
3597 /* 12.3.3 */ // ###TODO###
3598 defineProperty(Intl$1.DateTimeFormat.prototype, 'resolvedOptions', {
3599 writable: true,
3600 configurable: true,
3601 value: function value() {
3602 var prop = void 0,
3603 descs = new Record(),
3604 props = ['locale', 'calendar', 'numberingSystem', 'timeZone', 'hour12', 'weekday', 'era', 'year', 'month', 'day', 'hour', 'minute', 'second', 'timeZoneName'],
3605 internal = this !== null && babelHelpers["typeof"](this) === 'object' && getInternalProperties(this);
3606
3607 // Satisfy test 12.3_b
3608 if (!internal || !internal['[[initializedDateTimeFormat]]']) throw new TypeError('`this` value for resolvedOptions() is not an initialized Intl.DateTimeFormat object.');
3609
3610 for (var i = 0, max = props.length; i < max; i++) {
3611 if (hop.call(internal, prop = '[[' + props[i] + ']]')) descs[props[i]] = { value: internal[prop], writable: true, configurable: true, enumerable: true };
3612 }
3613
3614 return objCreate({}, descs);
3615 }
3616 });
3617
3618 var ls = Intl$1.__localeSensitiveProtos = {
3619 Number: {},
3620 Date: {}
3621 };
3622
3623 /**
3624 * When the toLocaleString method is called with optional arguments locales and options,
3625 * the following steps are taken:
3626 */
3627 /* 13.2.1 */ls.Number.toLocaleString = function () {
3628 // Satisfy test 13.2.1_1
3629 if (Object.prototype.toString.call(this) !== '[object Number]') throw new TypeError('`this` value must be a number for Number.prototype.toLocaleString()');
3630
3631 // 1. Let x be this Number value (as defined in ES5, 15.7.4).
3632 // 2. If locales is not provided, then let locales be undefined.
3633 // 3. If options is not provided, then let options be undefined.
3634 // 4. Let numberFormat be the result of creating a new object as if by the
3635 // expression new Intl.NumberFormat(locales, options) where
3636 // Intl.NumberFormat is the standard built-in constructor defined in 11.1.3.
3637 // 5. Return the result of calling the FormatNumber abstract operation
3638 // (defined in 11.3.2) with arguments numberFormat and x.
3639 return FormatNumber(new NumberFormatConstructor(arguments[0], arguments[1]), this);
3640 };
3641
3642 /**
3643 * When the toLocaleString method is called with optional arguments locales and options,
3644 * the following steps are taken:
3645 */
3646 /* 13.3.1 */ls.Date.toLocaleString = function () {
3647 // Satisfy test 13.3.0_1
3648 if (Object.prototype.toString.call(this) !== '[object Date]') throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleString()');
3649
3650 // 1. Let x be this time value (as defined in ES5, 15.9.5).
3651 var x = +this;
3652
3653 // 2. If x is NaN, then return "Invalid Date".
3654 if (isNaN(x)) return 'Invalid Date';
3655
3656 // 3. If locales is not provided, then let locales be undefined.
3657 var locales = arguments[0];
3658
3659 // 4. If options is not provided, then let options be undefined.
3660 var options = arguments[1];
3661
3662 // 5. Let options be the result of calling the ToDateTimeOptions abstract
3663 // operation (defined in 12.1.1) with arguments options, "any", and "all".
3664 options = ToDateTimeOptions(options, 'any', 'all');
3665
3666 // 6. Let dateTimeFormat be the result of creating a new object as if by the
3667 // expression new Intl.DateTimeFormat(locales, options) where
3668 // Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
3669 var dateTimeFormat = new DateTimeFormatConstructor(locales, options);
3670
3671 // 7. Return the result of calling the FormatDateTime abstract operation (defined
3672 // in 12.3.2) with arguments dateTimeFormat and x.
3673 return FormatDateTime(dateTimeFormat, x);
3674 };
3675
3676 /**
3677 * When the toLocaleDateString method is called with optional arguments locales and
3678 * options, the following steps are taken:
3679 */
3680 /* 13.3.2 */ls.Date.toLocaleDateString = function () {
3681 // Satisfy test 13.3.0_1
3682 if (Object.prototype.toString.call(this) !== '[object Date]') throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleDateString()');
3683
3684 // 1. Let x be this time value (as defined in ES5, 15.9.5).
3685 var x = +this;
3686
3687 // 2. If x is NaN, then return "Invalid Date".
3688 if (isNaN(x)) return 'Invalid Date';
3689
3690 // 3. If locales is not provided, then let locales be undefined.
3691 var locales = arguments[0],
3692
3693
3694 // 4. If options is not provided, then let options be undefined.
3695 options = arguments[1];
3696
3697 // 5. Let options be the result of calling the ToDateTimeOptions abstract
3698 // operation (defined in 12.1.1) with arguments options, "date", and "date".
3699 options = ToDateTimeOptions(options, 'date', 'date');
3700
3701 // 6. Let dateTimeFormat be the result of creating a new object as if by the
3702 // expression new Intl.DateTimeFormat(locales, options) where
3703 // Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
3704 var dateTimeFormat = new DateTimeFormatConstructor(locales, options);
3705
3706 // 7. Return the result of calling the FormatDateTime abstract operation (defined
3707 // in 12.3.2) with arguments dateTimeFormat and x.
3708 return FormatDateTime(dateTimeFormat, x);
3709 };
3710
3711 /**
3712 * When the toLocaleTimeString method is called with optional arguments locales and
3713 * options, the following steps are taken:
3714 */
3715 /* 13.3.3 */ls.Date.toLocaleTimeString = function () {
3716 // Satisfy test 13.3.0_1
3717 if (Object.prototype.toString.call(this) !== '[object Date]') throw new TypeError('`this` value must be a Date instance for Date.prototype.toLocaleTimeString()');
3718
3719 // 1. Let x be this time value (as defined in ES5, 15.9.5).
3720 var x = +this;
3721
3722 // 2. If x is NaN, then return "Invalid Date".
3723 if (isNaN(x)) return 'Invalid Date';
3724
3725 // 3. If locales is not provided, then let locales be undefined.
3726 var locales = arguments[0];
3727
3728 // 4. If options is not provided, then let options be undefined.
3729 var options = arguments[1];
3730
3731 // 5. Let options be the result of calling the ToDateTimeOptions abstract
3732 // operation (defined in 12.1.1) with arguments options, "time", and "time".
3733 options = ToDateTimeOptions(options, 'time', 'time');
3734
3735 // 6. Let dateTimeFormat be the result of creating a new object as if by the
3736 // expression new Intl.DateTimeFormat(locales, options) where
3737 // Intl.DateTimeFormat is the standard built-in constructor defined in 12.1.3.
3738 var dateTimeFormat = new DateTimeFormatConstructor(locales, options);
3739
3740 // 7. Return the result of calling the FormatDateTime abstract operation (defined
3741 // in 12.3.2) with arguments dateTimeFormat and x.
3742 return FormatDateTime(dateTimeFormat, x);
3743 };
3744
3745 defineProperty(Intl$1, '__applyLocaleSensitivePrototypes', {
3746 writable: true,
3747 configurable: true,
3748 value: function value() {
3749 defineProperty(Number.prototype, 'toLocaleString', { writable: true, configurable: true, value: ls.Number.toLocaleString });
3750 // Need this here for IE 8, to avoid the _DontEnum_ bug
3751 defineProperty(Date.prototype, 'toLocaleString', { writable: true, configurable: true, value: ls.Date.toLocaleString });
3752
3753 for (var k in ls.Date) {
3754 if (hop.call(ls.Date, k)) defineProperty(Date.prototype, k, { writable: true, configurable: true, value: ls.Date[k] });
3755 }
3756 }
3757 });
3758
3759 /**
3760 * Can't really ship a single script with data for hundreds of locales, so we provide
3761 * this __addLocaleData method as a means for the developer to add the data on an
3762 * as-needed basis
3763 */
3764 defineProperty(Intl$1, '__addLocaleData', {
3765 value: function value(data) {
3766 if (!IsStructurallyValidLanguageTag(data.locale)) throw new Error("Object passed doesn't identify itself with a valid language tag");
3767
3768 addLocaleData(data, data.locale);
3769 }
3770 });
3771
3772 function addLocaleData(data, tag) {
3773 // Both NumberFormat and DateTimeFormat require number data, so throw if it isn't present
3774 if (!data.number) throw new Error("Object passed doesn't contain locale data for Intl.NumberFormat");
3775
3776 var locale = void 0,
3777 locales = [tag],
3778 parts = tag.split('-');
3779
3780 // Create fallbacks for locale data with scripts, e.g. Latn, Hans, Vaii, etc
3781 if (parts.length > 2 && parts[1].length === 4) arrPush.call(locales, parts[0] + '-' + parts[2]);
3782
3783 while (locale = arrShift.call(locales)) {
3784 // Add to NumberFormat internal properties as per 11.2.3
3785 arrPush.call(internals.NumberFormat['[[availableLocales]]'], locale);
3786 internals.NumberFormat['[[localeData]]'][locale] = data.number;
3787
3788 // ...and DateTimeFormat internal properties as per 12.2.3
3789 if (data.date) {
3790 data.date.nu = data.number.nu;
3791 arrPush.call(internals.DateTimeFormat['[[availableLocales]]'], locale);
3792 internals.DateTimeFormat['[[localeData]]'][locale] = data.date;
3793 }
3794 }
3795
3796 // If this is the first set of locale data added, make it the default
3797 if (defaultLocale === undefined) setDefaultLocale(tag);
3798 }
3799
3800 // hack to export the polyfill as global Intl if needed
3801 if (typeof Intl === 'undefined') {
3802 try {
3803 window.Intl = Intl$1;
3804 Intl$1.__applyLocaleSensitivePrototypes();
3805 } catch (e) {
3806 // can be read only property
3807 }
3808 }
3809
3810 return Intl$1;
3811
3812}));
3813//# sourceMappingURL=Intl.js.map
\No newline at end of file