UNPKG

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