@formatjs/intl-datetimeformat 7.1.0 → 7.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/add-all-tz.js +319 -318
  2. package/add-golden-tz.js +1 -1
  3. package/index.d.ts +1 -1
  4. package/index.js +1 -1
  5. package/package.json +5 -5
  6. package/polyfill-force.js +28 -44
  7. package/polyfill.iife.js +2803 -3046
  8. package/polyfill.js +29 -45
  9. package/should-polyfill.js +38 -50
  10. package/src/abstract/BasicFormatMatcher.d.ts +5 -5
  11. package/src/abstract/BasicFormatMatcher.js +87 -103
  12. package/src/abstract/BestFitFormatMatcher.d.ts +12 -12
  13. package/src/abstract/BestFitFormatMatcher.js +102 -108
  14. package/src/abstract/DateTimeStyleFormat.d.ts +2 -2
  15. package/src/abstract/DateTimeStyleFormat.js +47 -62
  16. package/src/abstract/FormatDateTime.d.ts +7 -7
  17. package/src/abstract/FormatDateTime.js +12 -12
  18. package/src/abstract/FormatDateTimePattern.d.ts +11 -11
  19. package/src/abstract/FormatDateTimePattern.js +185 -198
  20. package/src/abstract/FormatDateTimeRange.d.ts +3 -3
  21. package/src/abstract/FormatDateTimeRange.js +9 -8
  22. package/src/abstract/FormatDateTimeRangeToParts.d.ts +4 -4
  23. package/src/abstract/FormatDateTimeRangeToParts.js +14 -12
  24. package/src/abstract/FormatDateTimeToParts.d.ts +9 -9
  25. package/src/abstract/FormatDateTimeToParts.js +17 -18
  26. package/src/abstract/InitializeDateTimeFormat.d.ts +15 -15
  27. package/src/abstract/InitializeDateTimeFormat.js +206 -184
  28. package/src/abstract/PartitionDateTimePattern.d.ts +8 -8
  29. package/src/abstract/PartitionDateTimePattern.js +15 -14
  30. package/src/abstract/PartitionDateTimeRangePattern.d.ts +4 -4
  31. package/src/abstract/PartitionDateTimeRangePattern.js +168 -161
  32. package/src/abstract/ToDateTimeOptions.d.ts +5 -5
  33. package/src/abstract/ToDateTimeOptions.js +68 -60
  34. package/src/abstract/ToLocalTime.d.ts +21 -21
  35. package/src/abstract/ToLocalTime.js +116 -45
  36. package/src/abstract/skeleton.d.ts +8 -8
  37. package/src/abstract/skeleton.js +231 -280
  38. package/src/abstract/utils.d.ts +1 -1
  39. package/src/abstract/utils.js +21 -20
  40. package/src/core.d.ts +17 -17
  41. package/src/core.js +250 -286
  42. package/src/data/all-tz.generated.d.ts +8 -0
  43. package/src/data/all-tz.generated.js +462 -0
  44. package/src/data/links.generated.d.ts +261 -0
  45. package/src/data/links.generated.js +260 -0
  46. package/src/get_internal_slots.d.ts +3 -1
  47. package/src/get_internal_slots.js +8 -7
  48. package/src/packer.d.ts +2 -2
  49. package/src/packer.js +23 -35
  50. package/src/to_locale_string.d.ts +3 -3
  51. package/src/to_locale_string.js +11 -11
  52. package/src/types.d.ts +34 -39
  53. package/src/types.js +1 -1
  54. package/supported-locales.generated.js +573 -1
  55. package/test262-main.d.ts +4 -1
  56. package/test262-main.js +24600 -44193
  57. package/src/data/all-tz.d.ts +0 -6
  58. package/src/data/all-tz.js +0 -461
  59. package/src/data/links.d.ts +0 -259
  60. package/src/data/links.js +0 -260
package/polyfill.js CHANGED
@@ -1,47 +1,31 @@
1
- import { DateTimeFormat } from './index.js';
2
- import { defineProperty } from '@formatjs/ecma402-abstract';
3
- import { shouldPolyfill } from './should-polyfill.js';
4
- import { toLocaleString as _toLocaleString, toLocaleDateString as _toLocaleDateString, toLocaleTimeString as _toLocaleTimeString, } from './src/to_locale_string.js';
1
+ import { DateTimeFormat } from "./index.js";
2
+ import { defineProperty } from "@formatjs/ecma402-abstract";
3
+ import { shouldPolyfill } from "./should-polyfill.js";
4
+ import { toLocaleString as _toLocaleString, toLocaleDateString as _toLocaleDateString, toLocaleTimeString as _toLocaleTimeString } from "./src/to_locale_string.js";
5
5
  if (shouldPolyfill()) {
6
- defineProperty(Intl, 'DateTimeFormat', { value: DateTimeFormat });
7
- defineProperty(Date.prototype, 'toLocaleString', {
8
- value: function toLocaleString(locales, options) {
9
- if (options === void 0) { options = {
10
- dateStyle: 'short',
11
- timeStyle: 'medium',
12
- }; }
13
- try {
14
- return _toLocaleString(this, locales, options);
15
- }
16
- catch (_a) {
17
- return 'Invalid Date';
18
- }
19
- },
20
- });
21
- defineProperty(Date.prototype, 'toLocaleDateString', {
22
- value: function toLocaleDateString(locales, options) {
23
- if (options === void 0) { options = {
24
- dateStyle: 'short',
25
- }; }
26
- try {
27
- return _toLocaleDateString(this, locales, options);
28
- }
29
- catch (_a) {
30
- return 'Invalid Date';
31
- }
32
- },
33
- });
34
- defineProperty(Date.prototype, 'toLocaleTimeString', {
35
- value: function toLocaleTimeString(locales, options) {
36
- if (options === void 0) { options = {
37
- timeStyle: 'medium',
38
- }; }
39
- try {
40
- return _toLocaleTimeString(this, locales, options);
41
- }
42
- catch (_a) {
43
- return 'Invalid Date';
44
- }
45
- },
46
- });
6
+ defineProperty(Intl, "DateTimeFormat", { value: DateTimeFormat });
7
+ defineProperty(Date.prototype, "toLocaleString", { value: function toLocaleString(locales, options = {
8
+ dateStyle: "short",
9
+ timeStyle: "medium"
10
+ }) {
11
+ try {
12
+ return _toLocaleString(this, locales, options);
13
+ } catch {
14
+ return "Invalid Date";
15
+ }
16
+ } });
17
+ defineProperty(Date.prototype, "toLocaleDateString", { value: function toLocaleDateString(locales, options = { dateStyle: "short" }) {
18
+ try {
19
+ return _toLocaleDateString(this, locales, options);
20
+ } catch {
21
+ return "Invalid Date";
22
+ }
23
+ } });
24
+ defineProperty(Date.prototype, "toLocaleTimeString", { value: function toLocaleTimeString(locales, options = { timeStyle: "medium" }) {
25
+ try {
26
+ return _toLocaleTimeString(this, locales, options);
27
+ } catch {
28
+ return "Invalid Date";
29
+ }
30
+ } });
47
31
  }
@@ -1,61 +1,49 @@
1
- import { match } from '@formatjs/intl-localematcher';
2
- import { supportedLocales } from './supported-locales.generated.js';
1
+ import { match } from "@formatjs/intl-localematcher";
2
+ import { supportedLocales } from "./supported-locales.generated.js";
3
3
  function supportsDateStyle() {
4
- try {
5
- return !!new Intl.DateTimeFormat(undefined, {
6
- dateStyle: 'short',
7
- }).resolvedOptions().dateStyle;
8
- }
9
- catch (_a) {
10
- return false;
11
- }
4
+ try {
5
+ return !!new Intl.DateTimeFormat(undefined, { dateStyle: "short" }).resolvedOptions().dateStyle;
6
+ } catch {
7
+ return false;
8
+ }
12
9
  }
13
10
  /**
14
- * https://bugs.chromium.org/p/chromium/issues/detail?id=865351
15
- */
11
+ * https://bugs.chromium.org/p/chromium/issues/detail?id=865351
12
+ */
16
13
  function hasChromeLt71Bug() {
17
- try {
18
- return (new Intl.DateTimeFormat('en', {
19
- hourCycle: 'h11',
20
- hour: 'numeric',
21
- }).formatToParts(0)[2].type !== 'dayPeriod');
22
- }
23
- catch (_a) {
24
- return false;
25
- }
14
+ try {
15
+ return new Intl.DateTimeFormat("en", {
16
+ hourCycle: "h11",
17
+ hour: "numeric"
18
+ }).formatToParts(0)[2].type !== "dayPeriod";
19
+ } catch {
20
+ return false;
21
+ }
26
22
  }
27
23
  /**
28
- * Node 14's version of Intl.DateTimeFormat does not throw
29
- * when dateStyle/timeStyle is used with other options.
30
- * This was fixed in newer V8 versions
31
- */
24
+ * Node 14's version of Intl.DateTimeFormat does not throw
25
+ * when dateStyle/timeStyle is used with other options.
26
+ * This was fixed in newer V8 versions
27
+ */
32
28
  function hasUnthrownDateTimeStyleBug() {
33
- try {
34
- return !!new Intl.DateTimeFormat('en', {
35
- dateStyle: 'short',
36
- hour: 'numeric',
37
- }).format(new Date(0));
38
- }
39
- catch (_a) {
40
- return false;
41
- }
29
+ try {
30
+ return !!new Intl.DateTimeFormat("en", {
31
+ dateStyle: "short",
32
+ hour: "numeric"
33
+ }).format(new Date(0));
34
+ } catch {
35
+ return false;
36
+ }
42
37
  }
43
38
  function supportedLocalesOf(locale) {
44
- if (!locale) {
45
- return true;
46
- }
47
- var locales = Array.isArray(locale) ? locale : [locale];
48
- return (Intl.DateTimeFormat.supportedLocalesOf(locales).length === locales.length);
39
+ if (!locale) {
40
+ return true;
41
+ }
42
+ const locales = Array.isArray(locale) ? locale : [locale];
43
+ return Intl.DateTimeFormat.supportedLocalesOf(locales).length === locales.length;
49
44
  }
50
- export function shouldPolyfill(locale) {
51
- if (locale === void 0) { locale = 'en'; }
52
- if (!('DateTimeFormat' in Intl) ||
53
- !('formatToParts' in Intl.DateTimeFormat.prototype) ||
54
- !('formatRange' in Intl.DateTimeFormat.prototype) ||
55
- hasChromeLt71Bug() ||
56
- hasUnthrownDateTimeStyleBug() ||
57
- !supportsDateStyle() ||
58
- !supportedLocalesOf(locale)) {
59
- return locale ? match([locale], supportedLocales, 'en') : undefined;
60
- }
45
+ export function shouldPolyfill(locale = "en") {
46
+ if (!("DateTimeFormat" in Intl) || !("formatToParts" in Intl.DateTimeFormat.prototype) || !("formatRange" in Intl.DateTimeFormat.prototype) || hasChromeLt71Bug() || hasUnthrownDateTimeStyleBug() || !supportsDateStyle() || !supportedLocalesOf(locale)) {
47
+ return locale ? match([locale], supportedLocales, "en") : undefined;
48
+ }
61
49
  }
@@ -1,7 +1,7 @@
1
- import { Formats } from '@formatjs/ecma402-abstract';
1
+ import { type Formats } from "@formatjs/ecma402-abstract";
2
2
  /**
3
- * https://tc39.es/ecma402/#sec-basicformatmatcher
4
- * @param options
5
- * @param formats
6
- */
3
+ * https://tc39.es/ecma402/#sec-basicformatmatcher
4
+ * @param options
5
+ * @param formats
6
+ */
7
7
  export declare function BasicFormatMatcher(options: Intl.DateTimeFormatOptions, formats: Formats[]): Formats;
@@ -1,106 +1,90 @@
1
- import { __assign } from "tslib";
2
- import { invariant } from '@formatjs/ecma402-abstract';
3
- import { DATE_TIME_PROPS, additionPenalty, removalPenalty, longMorePenalty, shortMorePenalty, shortLessPenalty, longLessPenalty, offsetPenalty, } from './utils.js';
1
+ import { invariant } from "@formatjs/ecma402-abstract";
2
+ import { DATE_TIME_PROPS, additionPenalty, removalPenalty, longMorePenalty, shortMorePenalty, shortLessPenalty, longLessPenalty, offsetPenalty } from "./utils.js";
4
3
  /**
5
- * https://tc39.es/ecma402/#sec-basicformatmatcher
6
- * @param options
7
- * @param formats
8
- */
4
+ * https://tc39.es/ecma402/#sec-basicformatmatcher
5
+ * @param options
6
+ * @param formats
7
+ */
9
8
  export function BasicFormatMatcher(options, formats) {
10
- var bestScore = -Infinity;
11
- var bestFormat = formats[0];
12
- invariant(Array.isArray(formats), 'formats should be a list of things');
13
- for (var _i = 0, formats_1 = formats; _i < formats_1.length; _i++) {
14
- var format = formats_1[_i];
15
- var score = 0;
16
- for (var _a = 0, DATE_TIME_PROPS_1 = DATE_TIME_PROPS; _a < DATE_TIME_PROPS_1.length; _a++) {
17
- var prop = DATE_TIME_PROPS_1[_a];
18
- var optionsProp = options[prop];
19
- var formatProp = format[prop];
20
- if (optionsProp === undefined && formatProp !== undefined) {
21
- score -= additionPenalty;
22
- }
23
- else if (optionsProp !== undefined && formatProp === undefined) {
24
- score -= removalPenalty;
25
- }
26
- else if (prop === 'timeZoneName') {
27
- if (optionsProp === 'short' || optionsProp === 'shortGeneric') {
28
- if (formatProp === 'shortOffset') {
29
- score -= offsetPenalty;
30
- }
31
- else if (formatProp === 'longOffset') {
32
- score -= offsetPenalty + shortMorePenalty;
33
- }
34
- else if (optionsProp === 'short' && formatProp === 'long') {
35
- score -= shortMorePenalty;
36
- }
37
- else if (optionsProp === 'shortGeneric' &&
38
- formatProp === 'longGeneric') {
39
- score -= shortMorePenalty;
40
- }
41
- else if (optionsProp !== formatProp) {
42
- score -= removalPenalty;
43
- }
44
- }
45
- else if (optionsProp === 'shortOffset' &&
46
- formatProp === 'longOffset') {
47
- score -= shortMorePenalty;
48
- }
49
- else if (optionsProp === 'long' || optionsProp === 'longGeneric') {
50
- if (formatProp === 'longOffset') {
51
- score -= offsetPenalty;
52
- }
53
- else if (formatProp === 'shortOffset') {
54
- score -= offsetPenalty + longLessPenalty;
55
- }
56
- else if (optionsProp === 'long' && formatProp === 'short') {
57
- score -= longLessPenalty;
58
- }
59
- else if (optionsProp === 'longGeneric' &&
60
- formatProp === 'shortGeneric') {
61
- score -= longLessPenalty;
62
- }
63
- else if (optionsProp !== formatProp) {
64
- score -= removalPenalty;
65
- }
66
- }
67
- else if (optionsProp === 'longOffset' &&
68
- formatProp === 'shortOffset') {
69
- score -= longLessPenalty;
70
- }
71
- else if (optionsProp !== formatProp) {
72
- score -= removalPenalty;
73
- }
74
- }
75
- else if (optionsProp !== formatProp) {
76
- var values = void 0;
77
- if (prop === 'fractionalSecondDigits') {
78
- values = [1, 2, 3];
79
- }
80
- else {
81
- values = ['2-digit', 'numeric', 'narrow', 'short', 'long'];
82
- }
83
- var optionsPropIndex = values.indexOf(optionsProp);
84
- var formatPropIndex = values.indexOf(formatProp);
85
- var delta = Math.max(-2, Math.min(formatPropIndex - optionsPropIndex, 2));
86
- if (delta === 2) {
87
- score -= longMorePenalty;
88
- }
89
- else if (delta === 1) {
90
- score -= shortMorePenalty;
91
- }
92
- else if (delta === -1) {
93
- score -= shortLessPenalty;
94
- }
95
- else if (delta === -2) {
96
- score -= longLessPenalty;
97
- }
98
- }
99
- }
100
- if (score > bestScore) {
101
- bestScore = score;
102
- bestFormat = format;
103
- }
104
- }
105
- return __assign({}, bestFormat);
9
+ let bestScore = -Infinity;
10
+ let bestFormat = formats[0];
11
+ invariant(Array.isArray(formats), "formats should be a list of things");
12
+ for (const format of formats) {
13
+ let score = 0;
14
+ for (const prop of DATE_TIME_PROPS) {
15
+ const optionsProp = options[prop];
16
+ const formatProp = format[prop];
17
+ if (optionsProp === undefined && formatProp !== undefined) {
18
+ score -= additionPenalty;
19
+ } else if (optionsProp !== undefined && formatProp === undefined) {
20
+ score -= removalPenalty;
21
+ } else if (prop === "timeZoneName") {
22
+ if (optionsProp === "short" || optionsProp === "shortGeneric") {
23
+ if (formatProp === "shortOffset") {
24
+ score -= offsetPenalty;
25
+ } else if (formatProp === "longOffset") {
26
+ score -= offsetPenalty + shortMorePenalty;
27
+ } else if (optionsProp === "short" && formatProp === "long") {
28
+ score -= shortMorePenalty;
29
+ } else if (optionsProp === "shortGeneric" && formatProp === "longGeneric") {
30
+ score -= shortMorePenalty;
31
+ } else if (optionsProp !== formatProp) {
32
+ score -= removalPenalty;
33
+ }
34
+ } else if (optionsProp === "shortOffset" && formatProp === "longOffset") {
35
+ score -= shortMorePenalty;
36
+ } else if (optionsProp === "long" || optionsProp === "longGeneric") {
37
+ if (formatProp === "longOffset") {
38
+ score -= offsetPenalty;
39
+ } else if (formatProp === "shortOffset") {
40
+ score -= offsetPenalty + longLessPenalty;
41
+ } else if (optionsProp === "long" && formatProp === "short") {
42
+ score -= longLessPenalty;
43
+ } else if (optionsProp === "longGeneric" && formatProp === "shortGeneric") {
44
+ score -= longLessPenalty;
45
+ } else if (optionsProp !== formatProp) {
46
+ score -= removalPenalty;
47
+ }
48
+ } else if (optionsProp === "longOffset" && formatProp === "shortOffset") {
49
+ score -= longLessPenalty;
50
+ } else if (optionsProp !== formatProp) {
51
+ score -= removalPenalty;
52
+ }
53
+ } else if (optionsProp !== formatProp) {
54
+ let values;
55
+ if (prop === "fractionalSecondDigits") {
56
+ values = [
57
+ 1,
58
+ 2,
59
+ 3
60
+ ];
61
+ } else {
62
+ values = [
63
+ "2-digit",
64
+ "numeric",
65
+ "narrow",
66
+ "short",
67
+ "long"
68
+ ];
69
+ }
70
+ const optionsPropIndex = values.indexOf(optionsProp);
71
+ const formatPropIndex = values.indexOf(formatProp);
72
+ const delta = Math.max(-2, Math.min(formatPropIndex - optionsPropIndex, 2));
73
+ if (delta === 2) {
74
+ score -= longMorePenalty;
75
+ } else if (delta === 1) {
76
+ score -= shortMorePenalty;
77
+ } else if (delta === -1) {
78
+ score -= shortLessPenalty;
79
+ } else if (delta === -2) {
80
+ score -= longLessPenalty;
81
+ }
82
+ }
83
+ }
84
+ if (score > bestScore) {
85
+ bestScore = score;
86
+ bestFormat = format;
87
+ }
88
+ }
89
+ return { ...bestFormat };
106
90
  }
@@ -1,16 +1,16 @@
1
- import { Formats } from '@formatjs/ecma402-abstract';
1
+ import { type Formats } from "@formatjs/ecma402-abstract";
2
2
  /**
3
- * Credit: https://github.com/andyearnshaw/Intl.js/blob/0958dc1ad8153f1056653ea22b8208f0df289a4e/src/12.datetimeformat.js#L611
4
- * with some modifications
5
- * @param options
6
- * @param format
7
- */
3
+ * Credit: https://github.com/andyearnshaw/Intl.js/blob/0958dc1ad8153f1056653ea22b8208f0df289a4e/src/12.datetimeformat.js#L611
4
+ * with some modifications
5
+ * @param options
6
+ * @param format
7
+ */
8
8
  export declare function bestFitFormatMatcherScore(options: Intl.DateTimeFormatOptions, format: Formats): number;
9
9
  /**
10
- * https://tc39.es/ecma402/#sec-bestfitformatmatcher
11
- * Just alias to basic for now
12
- * @param options
13
- * @param formats
14
- * @param implDetails Implementation details
15
- */
10
+ * https://tc39.es/ecma402/#sec-bestfitformatmatcher
11
+ * Just alias to basic for now
12
+ * @param options
13
+ * @param formats
14
+ * @param implDetails Implementation details
15
+ */
16
16
  export declare function BestFitFormatMatcher(options: Intl.DateTimeFormatOptions, formats: Formats[]): Formats;
@@ -1,116 +1,110 @@
1
- import { __assign } from "tslib";
2
- import { invariant } from '@formatjs/ecma402-abstract';
3
- import { processDateTimePattern } from './skeleton.js';
4
- import { DATE_TIME_PROPS, additionPenalty, differentNumericTypePenalty, longLessPenalty, longMorePenalty, removalPenalty, shortLessPenalty, shortMorePenalty, } from './utils.js';
1
+ import { invariant } from "@formatjs/ecma402-abstract";
2
+ import { processDateTimePattern } from "./skeleton.js";
3
+ import { DATE_TIME_PROPS, additionPenalty, differentNumericTypePenalty, longLessPenalty, longMorePenalty, removalPenalty, shortLessPenalty, shortMorePenalty } from "./utils.js";
5
4
  function isNumericType(t) {
6
- return t === 'numeric' || t === '2-digit';
5
+ return t === "numeric" || t === "2-digit";
7
6
  }
8
7
  /**
9
- * Credit: https://github.com/andyearnshaw/Intl.js/blob/0958dc1ad8153f1056653ea22b8208f0df289a4e/src/12.datetimeformat.js#L611
10
- * with some modifications
11
- * @param options
12
- * @param format
13
- */
8
+ * Credit: https://github.com/andyearnshaw/Intl.js/blob/0958dc1ad8153f1056653ea22b8208f0df289a4e/src/12.datetimeformat.js#L611
9
+ * with some modifications
10
+ * @param options
11
+ * @param format
12
+ */
14
13
  export function bestFitFormatMatcherScore(options, format) {
15
- var score = 0;
16
- if (options.hour12 && !format.hour12) {
17
- score -= removalPenalty;
18
- }
19
- else if (!options.hour12 && format.hour12) {
20
- score -= additionPenalty;
21
- }
22
- for (var _i = 0, DATE_TIME_PROPS_1 = DATE_TIME_PROPS; _i < DATE_TIME_PROPS_1.length; _i++) {
23
- var prop = DATE_TIME_PROPS_1[_i];
24
- var optionsProp = options[prop];
25
- var formatProp = format[prop];
26
- if (optionsProp === undefined && formatProp !== undefined) {
27
- score -= additionPenalty;
28
- }
29
- else if (optionsProp !== undefined && formatProp === undefined) {
30
- score -= removalPenalty;
31
- }
32
- else if (optionsProp !== formatProp) {
33
- // extra penalty for numeric vs non-numeric
34
- if (isNumericType(optionsProp) !==
35
- isNumericType(formatProp)) {
36
- score -= differentNumericTypePenalty;
37
- }
38
- else {
39
- var values = ['2-digit', 'numeric', 'narrow', 'short', 'long'];
40
- var optionsPropIndex = values.indexOf(optionsProp);
41
- var formatPropIndex = values.indexOf(formatProp);
42
- var delta = Math.max(-2, Math.min(formatPropIndex - optionsPropIndex, 2));
43
- if (delta === 2) {
44
- score -= longMorePenalty;
45
- }
46
- else if (delta === 1) {
47
- score -= shortMorePenalty;
48
- }
49
- else if (delta === -1) {
50
- score -= shortLessPenalty;
51
- }
52
- else if (delta === -2) {
53
- score -= longLessPenalty;
54
- }
55
- }
56
- }
57
- }
58
- return score;
14
+ let score = 0;
15
+ if (options.hour12 && !format.hour12) {
16
+ score -= removalPenalty;
17
+ } else if (!options.hour12 && format.hour12) {
18
+ score -= additionPenalty;
19
+ }
20
+ for (const prop of DATE_TIME_PROPS) {
21
+ const optionsProp = options[prop];
22
+ const formatProp = format[prop];
23
+ if (optionsProp === undefined && formatProp !== undefined) {
24
+ score -= additionPenalty;
25
+ } else if (optionsProp !== undefined && formatProp === undefined) {
26
+ score -= removalPenalty;
27
+ } else if (optionsProp !== formatProp) {
28
+ // extra penalty for numeric vs non-numeric
29
+ if (isNumericType(optionsProp) !== isNumericType(formatProp)) {
30
+ score -= differentNumericTypePenalty;
31
+ } else {
32
+ const values = [
33
+ "2-digit",
34
+ "numeric",
35
+ "narrow",
36
+ "short",
37
+ "long"
38
+ ];
39
+ const optionsPropIndex = values.indexOf(optionsProp);
40
+ const formatPropIndex = values.indexOf(formatProp);
41
+ const delta = Math.max(-2, Math.min(formatPropIndex - optionsPropIndex, 2));
42
+ if (delta === 2) {
43
+ score -= longMorePenalty;
44
+ } else if (delta === 1) {
45
+ score -= shortMorePenalty;
46
+ } else if (delta === -1) {
47
+ score -= shortLessPenalty;
48
+ } else if (delta === -2) {
49
+ score -= longLessPenalty;
50
+ }
51
+ }
52
+ }
53
+ }
54
+ return score;
59
55
  }
60
56
  /**
61
- * https://tc39.es/ecma402/#sec-bestfitformatmatcher
62
- * Just alias to basic for now
63
- * @param options
64
- * @param formats
65
- * @param implDetails Implementation details
66
- */
57
+ * https://tc39.es/ecma402/#sec-bestfitformatmatcher
58
+ * Just alias to basic for now
59
+ * @param options
60
+ * @param formats
61
+ * @param implDetails Implementation details
62
+ */
67
63
  export function BestFitFormatMatcher(options, formats) {
68
- var bestScore = -Infinity;
69
- var bestFormat = formats[0];
70
- invariant(Array.isArray(formats), 'formats should be a list of things');
71
- for (var _i = 0, formats_1 = formats; _i < formats_1.length; _i++) {
72
- var format = formats_1[_i];
73
- var score = bestFitFormatMatcherScore(options, format);
74
- if (score > bestScore) {
75
- bestScore = score;
76
- bestFormat = format;
77
- }
78
- }
79
- var skeletonFormat = __assign({}, bestFormat);
80
- var patternFormat = { rawPattern: bestFormat.rawPattern };
81
- processDateTimePattern(bestFormat.rawPattern, patternFormat);
82
- // Kinda following https://github.com/unicode-org/icu/blob/dd50e38f459d84e9bf1b0c618be8483d318458ad/icu4j/main/classes/core/src/com/ibm/icu/text/DateTimePatternGenerator.java
83
- // Method adjustFieldTypes
84
- for (var prop in skeletonFormat) {
85
- var skeletonValue = skeletonFormat[prop];
86
- var patternValue = patternFormat[prop];
87
- var requestedValue = options[prop];
88
- // Don't mess with minute/second or we can get in the situation of
89
- // 7:0:0 which is weird
90
- if (prop === 'minute' || prop === 'second') {
91
- continue;
92
- }
93
- // Nothing to do here
94
- if (!requestedValue) {
95
- continue;
96
- }
97
- // https://unicode.org/reports/tr35/tr35-dates.html#Matching_Skeletons
98
- // Looks like we should not convert numeric to alphabetic but the other way
99
- // around is ok
100
- if (isNumericType(patternValue) &&
101
- !isNumericType(requestedValue)) {
102
- continue;
103
- }
104
- if (skeletonValue === requestedValue) {
105
- continue;
106
- }
107
- patternFormat[prop] = requestedValue;
108
- }
109
- // Copy those over
110
- patternFormat.pattern = skeletonFormat.pattern;
111
- patternFormat.pattern12 = skeletonFormat.pattern12;
112
- patternFormat.skeleton = skeletonFormat.skeleton;
113
- patternFormat.rangePatterns = skeletonFormat.rangePatterns;
114
- patternFormat.rangePatterns12 = skeletonFormat.rangePatterns12;
115
- return patternFormat;
64
+ let bestScore = -Infinity;
65
+ let bestFormat = formats[0];
66
+ invariant(Array.isArray(formats), "formats should be a list of things");
67
+ for (const format of formats) {
68
+ const score = bestFitFormatMatcherScore(options, format);
69
+ if (score > bestScore) {
70
+ bestScore = score;
71
+ bestFormat = format;
72
+ }
73
+ }
74
+ const skeletonFormat = { ...bestFormat };
75
+ const patternFormat = { rawPattern: bestFormat.rawPattern };
76
+ processDateTimePattern(bestFormat.rawPattern, patternFormat);
77
+ // Kinda following https://github.com/unicode-org/icu/blob/dd50e38f459d84e9bf1b0c618be8483d318458ad/icu4j/main/classes/core/src/com/ibm/icu/text/DateTimePatternGenerator.java
78
+ // Method adjustFieldTypes
79
+ for (const prop in skeletonFormat) {
80
+ const skeletonValue = skeletonFormat[prop];
81
+ const patternValue = patternFormat[prop];
82
+ const requestedValue = options[prop];
83
+ // Don't mess with minute/second or we can get in the situation of
84
+ // 7:0:0 which is weird
85
+ if (prop === "minute" || prop === "second") {
86
+ continue;
87
+ }
88
+ // Nothing to do here
89
+ if (!requestedValue) {
90
+ continue;
91
+ }
92
+ // https://unicode.org/reports/tr35/tr35-dates.html#Matching_Skeletons
93
+ // Looks like we should not convert numeric to alphabetic but the other way
94
+ // around is ok
95
+ if (isNumericType(patternValue) && !isNumericType(requestedValue)) {
96
+ continue;
97
+ }
98
+ if (skeletonValue === requestedValue) {
99
+ continue;
100
+ }
101
+ patternFormat[prop] = requestedValue;
102
+ }
103
+ // Copy those over
104
+ patternFormat.pattern = skeletonFormat.pattern;
105
+ patternFormat.pattern12 = skeletonFormat.pattern12;
106
+ patternFormat.skeleton = skeletonFormat.skeleton;
107
+ patternFormat.rangePatterns = skeletonFormat.rangePatterns;
108
+ patternFormat.rangePatterns12 = skeletonFormat.rangePatterns12;
109
+ return patternFormat;
116
110
  }