@formatjs/intl-displaynames 7.1.1 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- export declare function CanonicalCodeForDisplayNames(type: 'language' | 'region' | 'script' | 'calendar' | 'dateTimeField' | 'currency', code: string): string;
1
+ export declare function CanonicalCodeForDisplayNames(type: "language" | "region" | "script" | "calendar" | "dateTimeField" | "currency", code: string): string;
@@ -1,48 +1,48 @@
1
- import { CanonicalizeLocaleList, invariant, IsWellFormedCurrencyCode, } from '@formatjs/ecma402-abstract';
2
- import { IsValidDateTimeFieldCode } from './IsValidDateTimeFieldCode';
3
- var UNICODE_REGION_SUBTAG_REGEX = /^([a-z]{2}|[0-9]{3})$/i;
4
- var ALPHA_4 = /^[a-z]{4}$/i;
5
- var UNICODE_TYPE_REGEX = /^[a-z0-9]{3,8}([-_][a-z0-9]{3,8})*$/i;
1
+ import { CanonicalizeLocaleList, invariant, IsWellFormedCurrencyCode } from "@formatjs/ecma402-abstract";
2
+ import { IsValidDateTimeFieldCode } from "./IsValidDateTimeFieldCode.js";
3
+ const UNICODE_REGION_SUBTAG_REGEX = /^([a-z]{2}|[0-9]{3})$/i;
4
+ const ALPHA_4 = /^[a-z]{4}$/i;
5
+ const UNICODE_TYPE_REGEX = /^[a-z0-9]{3,8}([-_][a-z0-9]{3,8})*$/i;
6
6
  function isUnicodeRegionSubtag(region) {
7
- return UNICODE_REGION_SUBTAG_REGEX.test(region);
7
+ return UNICODE_REGION_SUBTAG_REGEX.test(region);
8
8
  }
9
9
  function isUnicodeScriptSubtag(script) {
10
- return ALPHA_4.test(script);
10
+ return ALPHA_4.test(script);
11
11
  }
12
12
  function isUnicodeLocaleIdentifierType(code) {
13
- return UNICODE_TYPE_REGEX.test(code);
13
+ return UNICODE_TYPE_REGEX.test(code);
14
14
  }
15
15
  export function CanonicalCodeForDisplayNames(type, code) {
16
- if (type === 'language') {
17
- return CanonicalizeLocaleList([code])[0];
18
- }
19
- if (type === 'region') {
20
- if (!isUnicodeRegionSubtag(code)) {
21
- throw RangeError('invalid region');
22
- }
23
- return code.toUpperCase();
24
- }
25
- if (type === 'script') {
26
- if (!isUnicodeScriptSubtag(code)) {
27
- throw RangeError('invalid script');
28
- }
29
- return "".concat(code[0].toUpperCase()).concat(code.slice(1).toLowerCase());
30
- }
31
- if (type === 'calendar') {
32
- if (!isUnicodeLocaleIdentifierType(code)) {
33
- throw RangeError('invalid calendar');
34
- }
35
- return code.toLowerCase();
36
- }
37
- if (type === 'dateTimeField') {
38
- if (!IsValidDateTimeFieldCode(code)) {
39
- throw RangeError('invalid dateTimeField');
40
- }
41
- return code;
42
- }
43
- invariant(type === 'currency', 'invalid type');
44
- if (!IsWellFormedCurrencyCode(code)) {
45
- throw RangeError('invalid currency');
46
- }
47
- return code.toUpperCase();
16
+ if (type === "language") {
17
+ return CanonicalizeLocaleList([code])[0];
18
+ }
19
+ if (type === "region") {
20
+ if (!isUnicodeRegionSubtag(code)) {
21
+ throw RangeError("invalid region");
22
+ }
23
+ return code.toUpperCase();
24
+ }
25
+ if (type === "script") {
26
+ if (!isUnicodeScriptSubtag(code)) {
27
+ throw RangeError("invalid script");
28
+ }
29
+ return `${code[0].toUpperCase()}${code.slice(1).toLowerCase()}`;
30
+ }
31
+ if (type === "calendar") {
32
+ if (!isUnicodeLocaleIdentifierType(code)) {
33
+ throw RangeError("invalid calendar");
34
+ }
35
+ return code.toLowerCase();
36
+ }
37
+ if (type === "dateTimeField") {
38
+ if (!IsValidDateTimeFieldCode(code)) {
39
+ throw RangeError("invalid dateTimeField");
40
+ }
41
+ return code;
42
+ }
43
+ invariant(type === "currency", "invalid type");
44
+ if (!IsWellFormedCurrencyCode(code)) {
45
+ throw RangeError("invalid currency");
46
+ }
47
+ return code.toUpperCase();
48
48
  }
@@ -1,17 +1,17 @@
1
- var CODES_FOR_DATE_TIME_FIELD = [
2
- 'era',
3
- 'year',
4
- 'quarter',
5
- 'month',
6
- 'weekOfYear',
7
- 'weekday',
8
- 'day',
9
- 'dayPeriod',
10
- 'hour',
11
- 'minute',
12
- 'second',
13
- 'timeZoneName',
1
+ const CODES_FOR_DATE_TIME_FIELD = [
2
+ "era",
3
+ "year",
4
+ "quarter",
5
+ "month",
6
+ "weekOfYear",
7
+ "weekday",
8
+ "day",
9
+ "dayPeriod",
10
+ "hour",
11
+ "minute",
12
+ "second",
13
+ "timeZoneName"
14
14
  ];
15
15
  export function IsValidDateTimeFieldCode(field) {
16
- return CODES_FOR_DATE_TIME_FIELD.indexOf(field) >= 0;
16
+ return CODES_FOR_DATE_TIME_FIELD.indexOf(field) >= 0;
17
17
  }
package/index.d.ts CHANGED
@@ -1,27 +1,27 @@
1
- import { DisplayNamesData, DisplayNamesLocaleData } from '@formatjs/ecma402-abstract';
1
+ import { type DisplayNamesData, type DisplayNamesLocaleData } from "@formatjs/ecma402-abstract";
2
2
  export interface DisplayNamesOptions {
3
- localeMatcher?: 'lookup' | 'best fit';
4
- style?: 'narrow' | 'short' | 'long';
5
- type: 'language' | 'region' | 'script' | 'currency' | 'calendar' | 'dateTimeField';
6
- fallback?: 'code' | 'none';
7
- languageDisplay?: 'dialect' | 'standard';
3
+ localeMatcher?: "lookup" | "best fit";
4
+ style?: "narrow" | "short" | "long";
5
+ type: "language" | "region" | "script" | "currency" | "calendar" | "dateTimeField";
6
+ fallback?: "code" | "none";
7
+ languageDisplay?: "dialect" | "standard";
8
8
  }
9
9
  export interface DisplayNamesResolvedOptions {
10
- locale: string;
11
- style: NonNullable<DisplayNamesOptions['style']>;
12
- type: NonNullable<DisplayNamesOptions['type']>;
13
- fallback: NonNullable<DisplayNamesOptions['fallback']>;
14
- languageDisplay: NonNullable<DisplayNamesOptions['languageDisplay']>;
10
+ locale: string;
11
+ style: NonNullable<DisplayNamesOptions["style"]>;
12
+ type: NonNullable<DisplayNamesOptions["type"]>;
13
+ fallback: NonNullable<DisplayNamesOptions["fallback"]>;
14
+ languageDisplay: NonNullable<DisplayNamesOptions["languageDisplay"]>;
15
15
  }
16
16
  export declare class DisplayNames {
17
- constructor(locales: string | string[] | undefined, options: DisplayNamesOptions);
18
- static supportedLocalesOf(locales?: string | string[], options?: Pick<DisplayNamesOptions, 'localeMatcher'>): string[];
19
- static __addLocaleData(...data: DisplayNamesLocaleData[]): void;
20
- of(code: string | number | Record<string, unknown>): string | undefined;
21
- resolvedOptions(): DisplayNamesResolvedOptions;
22
- static localeData: Record<string, DisplayNamesData | undefined>;
23
- private static availableLocales;
24
- private static __defaultLocale;
25
- private static getDefaultLocale;
26
- static readonly polyfilled = true;
17
+ constructor(locales: string | string[] | undefined, options: DisplayNamesOptions);
18
+ static supportedLocalesOf(locales?: string | string[], options?: Pick<DisplayNamesOptions, "localeMatcher">): string[];
19
+ static __addLocaleData(...data: DisplayNamesLocaleData[]): void;
20
+ of(code: string | number | Record<string, unknown>): string | undefined;
21
+ resolvedOptions(): DisplayNamesResolvedOptions;
22
+ static localeData: Record<string, DisplayNamesData | undefined>;
23
+ private static availableLocales;
24
+ private static __defaultLocale;
25
+ private static getDefaultLocale;
26
+ static readonly polyfilled: true;
27
27
  }
package/index.js CHANGED
@@ -1,210 +1,196 @@
1
- import { __assign } from "tslib";
2
- import { CanonicalizeLocaleList, GetOption, GetOptionsObject, IsWellFormedCurrencyCode, SupportedLocales, ToString, getInternalSlot, getMultiInternalSlots, invariant, setInternalSlot, } from '@formatjs/ecma402-abstract';
3
- import { CanonicalCodeForDisplayNames } from './abstract/CanonicalCodeForDisplayNames.js';
4
- import { IsValidDateTimeFieldCode } from './abstract/IsValidDateTimeFieldCode.js';
5
- import { ResolveLocale } from '@formatjs/intl-localematcher';
6
- var DisplayNames = /** @class */ (function () {
7
- function DisplayNames(locales, options) {
8
- var _newTarget = this.constructor;
9
- if (_newTarget === undefined) {
10
- throw TypeError("Constructor Intl.DisplayNames requires 'new'");
11
- }
12
- var requestedLocales = CanonicalizeLocaleList(locales);
13
- options = GetOptionsObject(options);
14
- var opt = Object.create(null);
15
- var localeData = DisplayNames.localeData;
16
- var matcher = GetOption(options, 'localeMatcher', 'string', ['lookup', 'best fit'], 'best fit');
17
- opt.localeMatcher = matcher;
18
- var r = ResolveLocale(Array.from(DisplayNames.availableLocales), requestedLocales, opt, [], // there is no relevantExtensionKeys
19
- DisplayNames.localeData, DisplayNames.getDefaultLocale);
20
- var style = GetOption(options, 'style', 'string', ['narrow', 'short', 'long'], 'long');
21
- setSlot(this, 'style', style);
22
- var type = GetOption(options, 'type', 'string', ['language', 'region', 'script', 'currency', 'calendar', 'dateTimeField'], undefined);
23
- if (type === undefined) {
24
- throw TypeError("Intl.DisplayNames constructor requires \"type\" option");
25
- }
26
- setSlot(this, 'type', type);
27
- var fallback = GetOption(options, 'fallback', 'string', ['code', 'none'], 'code');
28
- setSlot(this, 'fallback', fallback);
29
- setSlot(this, 'locale', r.locale);
30
- var dataLocale = r.dataLocale;
31
- var dataLocaleData = localeData[dataLocale];
32
- invariant(!!dataLocaleData, "Missing locale data for ".concat(dataLocale));
33
- setSlot(this, 'localeData', dataLocaleData);
34
- invariant(dataLocaleData !== undefined, "locale data for ".concat(r.locale, " does not exist."));
35
- var types = dataLocaleData.types;
36
- invariant(typeof types === 'object' && types != null, 'invalid types data');
37
- var typeFields = types[type];
38
- invariant(typeof typeFields === 'object' && typeFields != null, 'invalid typeFields data');
39
- var languageDisplay = GetOption(options, 'languageDisplay', 'string', ['dialect', 'standard'], 'dialect');
40
- if (type === 'language') {
41
- setSlot(this, 'languageDisplay', languageDisplay);
42
- // Using types[type] instead of typeFields because TypeScript cannot infer the correct type
43
- var typeFields_1 = types[type][languageDisplay];
44
- invariant(typeof typeFields_1 === 'object' && typeFields_1 != null, 'invalid language typeFields data');
45
- }
46
- // Using types[type] instead of typeFields because TypeScript cannot infer the correct type
47
- var styleFields = type === 'language'
48
- ? types[type][languageDisplay][style]
49
- : types[type][style];
50
- invariant(typeof styleFields === 'object' && styleFields != null, 'invalid styleFields data');
51
- setSlot(this, 'fields', styleFields);
52
- }
53
- DisplayNames.supportedLocalesOf = function (locales, options) {
54
- return SupportedLocales(DisplayNames.availableLocales, CanonicalizeLocaleList(locales), options);
55
- };
56
- DisplayNames.__addLocaleData = function () {
57
- var data = [];
58
- for (var _i = 0; _i < arguments.length; _i++) {
59
- data[_i] = arguments[_i];
60
- }
61
- for (var _a = 0, data_1 = data; _a < data_1.length; _a++) {
62
- var _b = data_1[_a], d = _b.data, locale = _b.locale;
63
- var minimizedLocale = new Intl.Locale(locale)
64
- .minimize()
65
- .toString();
66
- DisplayNames.localeData[locale] = DisplayNames.localeData[minimizedLocale] = d;
67
- DisplayNames.availableLocales.add(minimizedLocale);
68
- DisplayNames.availableLocales.add(locale);
69
- if (!DisplayNames.__defaultLocale) {
70
- DisplayNames.__defaultLocale = minimizedLocale;
71
- }
72
- }
73
- };
74
- DisplayNames.prototype.of = function (code) {
75
- checkReceiver(this, 'of');
76
- var type = getSlot(this, 'type');
77
- var codeAsString = ToString(code);
78
- if (!isValidCodeForDisplayNames(type, codeAsString)) {
79
- throw RangeError('invalid code for Intl.DisplayNames.prototype.of');
80
- }
81
- var _a = getMultiInternalSlots(__INTERNAL_SLOT_MAP__, this, 'localeData', 'style', 'fallback'), localeData = _a.localeData, style = _a.style, fallback = _a.fallback;
82
- // Canonicalize the case.
83
- var canonicalCode = CanonicalCodeForDisplayNames(type, codeAsString);
84
- var name;
85
- if (type === 'language') {
86
- var languageDisplay = getSlot(this, 'languageDisplay');
87
- name = getNameForTypeLanguage(languageDisplay, localeData, style, canonicalCode, fallback);
88
- }
89
- else {
90
- // All the other types
91
- var typesData = localeData.types[type];
92
- name = typesData[style][canonicalCode] || typesData.long[canonicalCode];
93
- }
94
- if (name !== undefined) {
95
- return name;
96
- }
97
- if (fallback === 'code') {
98
- return codeAsString;
99
- }
100
- };
101
- DisplayNames.prototype.resolvedOptions = function () {
102
- checkReceiver(this, 'resolvedOptions');
103
- return __assign({}, getMultiInternalSlots(__INTERNAL_SLOT_MAP__, this, 'locale', 'style', 'type', 'fallback', 'languageDisplay'));
104
- };
105
- DisplayNames.getDefaultLocale = function () {
106
- return DisplayNames.__defaultLocale;
107
- };
108
- DisplayNames.localeData = {};
109
- DisplayNames.availableLocales = new Set();
110
- DisplayNames.__defaultLocale = '';
111
- DisplayNames.polyfilled = true;
112
- return DisplayNames;
113
- }());
114
- export { DisplayNames };
1
+ import { CanonicalizeLocaleList, GetOption, GetOptionsObject, IsWellFormedCurrencyCode, SupportedLocales, ToString, getInternalSlot, getMultiInternalSlots, invariant, setInternalSlot } from "@formatjs/ecma402-abstract";
2
+ import { CanonicalCodeForDisplayNames } from "./abstract/CanonicalCodeForDisplayNames.js";
3
+ import { IsValidDateTimeFieldCode } from "./abstract/IsValidDateTimeFieldCode.js";
4
+ import { ResolveLocale } from "@formatjs/intl-localematcher";
5
+ export class DisplayNames {
6
+ constructor(locales, options) {
7
+ if (new.target === undefined) {
8
+ throw TypeError(`Constructor Intl.DisplayNames requires 'new'`);
9
+ }
10
+ const requestedLocales = CanonicalizeLocaleList(locales);
11
+ options = GetOptionsObject(options);
12
+ const opt = Object.create(null);
13
+ const { localeData } = DisplayNames;
14
+ const matcher = GetOption(options, "localeMatcher", "string", ["lookup", "best fit"], "best fit");
15
+ opt.localeMatcher = matcher;
16
+ const r = ResolveLocale(Array.from(DisplayNames.availableLocales), requestedLocales, opt, [], DisplayNames.localeData, DisplayNames.getDefaultLocale);
17
+ const style = GetOption(options, "style", "string", [
18
+ "narrow",
19
+ "short",
20
+ "long"
21
+ ], "long");
22
+ setSlot(this, "style", style);
23
+ const type = GetOption(options, "type", "string", [
24
+ "language",
25
+ "region",
26
+ "script",
27
+ "currency",
28
+ "calendar",
29
+ "dateTimeField"
30
+ ], undefined);
31
+ if (type === undefined) {
32
+ throw TypeError(`Intl.DisplayNames constructor requires "type" option`);
33
+ }
34
+ setSlot(this, "type", type);
35
+ const fallback = GetOption(options, "fallback", "string", ["code", "none"], "code");
36
+ setSlot(this, "fallback", fallback);
37
+ setSlot(this, "locale", r.locale);
38
+ const { dataLocale } = r;
39
+ const dataLocaleData = localeData[dataLocale];
40
+ invariant(!!dataLocaleData, `Missing locale data for ${dataLocale}`);
41
+ setSlot(this, "localeData", dataLocaleData);
42
+ invariant(dataLocaleData !== undefined, `locale data for ${r.locale} does not exist.`);
43
+ const { types } = dataLocaleData;
44
+ invariant(typeof types === "object" && types != null, "invalid types data");
45
+ const typeFields = types[type];
46
+ invariant(typeof typeFields === "object" && typeFields != null, "invalid typeFields data");
47
+ const languageDisplay = GetOption(options, "languageDisplay", "string", ["dialect", "standard"], "dialect");
48
+ if (type === "language") {
49
+ setSlot(this, "languageDisplay", languageDisplay);
50
+ // Using types[type] instead of typeFields because TypeScript cannot infer the correct type
51
+ const typeFields = types[type][languageDisplay];
52
+ invariant(typeof typeFields === "object" && typeFields != null, "invalid language typeFields data");
53
+ }
54
+ // Using types[type] instead of typeFields because TypeScript cannot infer the correct type
55
+ const styleFields = type === "language" ? types[type][languageDisplay][style] : types[type][style];
56
+ invariant(typeof styleFields === "object" && styleFields != null, "invalid styleFields data");
57
+ setSlot(this, "fields", styleFields);
58
+ }
59
+ static supportedLocalesOf(locales, options) {
60
+ return SupportedLocales(DisplayNames.availableLocales, CanonicalizeLocaleList(locales), options);
61
+ }
62
+ static __addLocaleData(...data) {
63
+ for (const { data: d, locale } of data) {
64
+ const minimizedLocale = new Intl.Locale(locale).minimize().toString();
65
+ DisplayNames.localeData[locale] = DisplayNames.localeData[minimizedLocale] = d;
66
+ DisplayNames.availableLocales.add(minimizedLocale);
67
+ DisplayNames.availableLocales.add(locale);
68
+ if (!DisplayNames.__defaultLocale) {
69
+ DisplayNames.__defaultLocale = minimizedLocale;
70
+ }
71
+ }
72
+ }
73
+ of(code) {
74
+ checkReceiver(this, "of");
75
+ const type = getSlot(this, "type");
76
+ const codeAsString = ToString(code);
77
+ if (!isValidCodeForDisplayNames(type, codeAsString)) {
78
+ throw RangeError("invalid code for Intl.DisplayNames.prototype.of");
79
+ }
80
+ const { localeData, style, fallback } = getMultiInternalSlots(__INTERNAL_SLOT_MAP__, this, "localeData", "style", "fallback");
81
+ // Canonicalize the case.
82
+ let canonicalCode = CanonicalCodeForDisplayNames(type, codeAsString);
83
+ let name;
84
+ if (type === "language") {
85
+ const languageDisplay = getSlot(this, "languageDisplay");
86
+ name = getNameForTypeLanguage(languageDisplay, localeData, style, canonicalCode, fallback);
87
+ } else {
88
+ // All the other types
89
+ const typesData = localeData.types[type];
90
+ name = typesData[style][canonicalCode] || typesData.long[canonicalCode];
91
+ }
92
+ if (name !== undefined) {
93
+ return name;
94
+ }
95
+ if (fallback === "code") {
96
+ return codeAsString;
97
+ }
98
+ }
99
+ resolvedOptions() {
100
+ checkReceiver(this, "resolvedOptions");
101
+ return { ...getMultiInternalSlots(__INTERNAL_SLOT_MAP__, this, "locale", "style", "type", "fallback", "languageDisplay") };
102
+ }
103
+ static localeData = {};
104
+ static availableLocales = new Set();
105
+ static __defaultLocale = "";
106
+ static getDefaultLocale() {
107
+ return DisplayNames.__defaultLocale;
108
+ }
109
+ static polyfilled = true;
110
+ }
115
111
  // https://tc39.es/proposal-intl-displaynames/#sec-isvalidcodefordisplaynames
116
112
  function isValidCodeForDisplayNames(type, code) {
117
- switch (type) {
118
- case 'language':
119
- // subset of unicode_language_id
120
- // languageCode ["-" scriptCode] ["-" regionCode] *("-" variant)
121
- // where:
122
- // - languageCode is either a two letters ISO 639-1 language code or a three letters ISO 639-2 language code.
123
- // - scriptCode is should be an ISO-15924 four letters script code
124
- // - regionCode is either an ISO-3166 two letters region code, or a three digits UN M49 Geographic Regions.
125
- return /^[a-z]{2,3}(-[a-z]{4})?(-([a-z]{2}|\d{3}))?(-([a-z\d]{5,8}|\d[a-z\d]{3}))*$/i.test(code);
126
- case 'region':
127
- // unicode_region_subtag
128
- return /^([a-z]{2}|\d{3})$/i.test(code);
129
- case 'script':
130
- // unicode_script_subtag
131
- return /^[a-z]{4}$/i.test(code);
132
- case 'currency':
133
- return IsWellFormedCurrencyCode(code);
134
- case 'calendar':
135
- // unicode locale identifier type
136
- return /^[a-z0-9]{3,8}([-_][a-z0-9]{3,8})*$/i.test(code);
137
- case 'dateTimeField':
138
- return IsValidDateTimeFieldCode(code);
139
- }
113
+ switch (type) {
114
+ case "language":
115
+ // subset of unicode_language_id
116
+ // languageCode ["-" scriptCode] ["-" regionCode] *("-" variant)
117
+ // where:
118
+ // - languageCode is either a two letters ISO 639-1 language code or a three letters ISO 639-2 language code.
119
+ // - scriptCode is should be an ISO-15924 four letters script code
120
+ // - regionCode is either an ISO-3166 two letters region code, or a three digits UN M49 Geographic Regions.
121
+ return /^[a-z]{2,3}(-[a-z]{4})?(-([a-z]{2}|\d{3}))?(-([a-z\d]{5,8}|\d[a-z\d]{3}))*$/i.test(code);
122
+ case "region":
123
+ // unicode_region_subtag
124
+ return /^([a-z]{2}|\d{3})$/i.test(code);
125
+ case "script":
126
+ // unicode_script_subtag
127
+ return /^[a-z]{4}$/i.test(code);
128
+ case "currency": return IsWellFormedCurrencyCode(code);
129
+ case "calendar":
130
+ // unicode locale identifier type
131
+ return /^[a-z0-9]{3,8}([-_][a-z0-9]{3,8})*$/i.test(code);
132
+ case "dateTimeField": return IsValidDateTimeFieldCode(code);
133
+ }
140
134
  }
141
135
  try {
142
- // IE11 does not have Symbol
143
- if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
144
- Object.defineProperty(DisplayNames.prototype, Symbol.toStringTag, {
145
- value: 'Intl.DisplayNames',
146
- configurable: true,
147
- enumerable: false,
148
- writable: false,
149
- });
150
- }
151
- Object.defineProperty(DisplayNames, 'length', {
152
- value: 2,
153
- writable: false,
154
- enumerable: false,
155
- configurable: true,
156
- });
157
- }
158
- catch (_a) {
159
- // Make test 262 compliant
160
- }
161
- var __INTERNAL_SLOT_MAP__ = new WeakMap();
136
+ // IE11 does not have Symbol
137
+ if (typeof Symbol !== "undefined" && Symbol.toStringTag) {
138
+ Object.defineProperty(DisplayNames.prototype, Symbol.toStringTag, {
139
+ value: "Intl.DisplayNames",
140
+ configurable: true,
141
+ enumerable: false,
142
+ writable: false
143
+ });
144
+ }
145
+ Object.defineProperty(DisplayNames, "length", {
146
+ value: 2,
147
+ writable: false,
148
+ enumerable: false,
149
+ configurable: true
150
+ });
151
+ } catch {}
152
+ const __INTERNAL_SLOT_MAP__ = new WeakMap();
162
153
  function getSlot(instance, key) {
163
- return getInternalSlot(__INTERNAL_SLOT_MAP__, instance, key);
154
+ return getInternalSlot(__INTERNAL_SLOT_MAP__, instance, key);
164
155
  }
165
156
  function setSlot(instance, key, value) {
166
- setInternalSlot(__INTERNAL_SLOT_MAP__, instance, key, value);
157
+ setInternalSlot(__INTERNAL_SLOT_MAP__, instance, key, value);
167
158
  }
168
159
  function checkReceiver(receiver, methodName) {
169
- if (!(receiver instanceof DisplayNames)) {
170
- throw TypeError("Method Intl.DisplayNames.prototype.".concat(methodName, " called on incompatible receiver"));
171
- }
160
+ if (!(receiver instanceof DisplayNames)) {
161
+ throw TypeError(`Method Intl.DisplayNames.prototype.${methodName} called on incompatible receiver`);
162
+ }
172
163
  }
173
164
  function getNameForTypeLanguage(languageDisplay, localeData, style, canonicalCode, fallback) {
174
- // First, try to get the name using the canonicalCode
175
- var typesData = localeData.types.language[languageDisplay];
176
- var name = typesData[style][canonicalCode] || typesData.long[canonicalCode];
177
- if (name === undefined) {
178
- // If no name has been found using the canonicalCode,
179
- // check if the latter contains a region sub tag
180
- var regionMatch = /-([a-z]{2}|\d{3})\b/i.exec(canonicalCode);
181
- if (regionMatch) {
182
- // Extract the language and region sub tags
183
- var languageSubTag = canonicalCode.substring(0, regionMatch.index) +
184
- canonicalCode.substring(regionMatch.index + regionMatch[0].length);
185
- var regionSubTag = regionMatch[1];
186
- // Let's try again using languageSubTag this time
187
- var name_1 = typesData[style][languageSubTag] || typesData.long[languageSubTag];
188
- // If a name has been found and a region sub tag exists,
189
- // compose them together or use the code fallback
190
- if (name_1 !== undefined && regionSubTag) {
191
- // Retrieve region display names
192
- var regionsData = localeData.types.region;
193
- var regionDisplayName = regionsData[style][regionSubTag] || regionsData.long[regionSubTag];
194
- if (regionDisplayName || fallback === 'code') {
195
- // Interpolate into locale-specific pattern.
196
- var pattern = localeData.patterns.locale;
197
- return pattern
198
- .replace('{0}', name_1)
199
- .replace('{1}', regionDisplayName || regionSubTag);
200
- }
201
- }
202
- else {
203
- return name_1;
204
- }
205
- }
206
- }
207
- else {
208
- return name;
209
- }
165
+ // First, try to get the name using the canonicalCode
166
+ const typesData = localeData.types.language[languageDisplay];
167
+ const name = typesData[style][canonicalCode] || typesData.long[canonicalCode];
168
+ if (name === undefined) {
169
+ // If no name has been found using the canonicalCode,
170
+ // check if the latter contains a region sub tag
171
+ const regionMatch = /-([a-z]{2}|\d{3})\b/i.exec(canonicalCode);
172
+ if (regionMatch) {
173
+ // Extract the language and region sub tags
174
+ const languageSubTag = canonicalCode.substring(0, regionMatch.index) + canonicalCode.substring(regionMatch.index + regionMatch[0].length);
175
+ const regionSubTag = regionMatch[1];
176
+ // Let's try again using languageSubTag this time
177
+ const name = typesData[style][languageSubTag] || typesData.long[languageSubTag];
178
+ // If a name has been found and a region sub tag exists,
179
+ // compose them together or use the code fallback
180
+ if (name !== undefined && regionSubTag) {
181
+ // Retrieve region display names
182
+ const regionsData = localeData.types.region;
183
+ const regionDisplayName = regionsData[style][regionSubTag] || regionsData.long[regionSubTag];
184
+ if (regionDisplayName || fallback === "code") {
185
+ // Interpolate into locale-specific pattern.
186
+ const pattern = localeData.patterns.locale;
187
+ return pattern.replace("{0}", name).replace("{1}", regionDisplayName || regionSubTag);
188
+ }
189
+ } else {
190
+ return name;
191
+ }
192
+ }
193
+ } else {
194
+ return name;
195
+ }
210
196
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formatjs/intl-displaynames",
3
3
  "description": "Polyfill for: https://tc39.es/proposal-intl-displaynames",
4
- "version": "7.1.1",
4
+ "version": "7.2.0",
5
5
  "license": "MIT",
6
6
  "author": "Linjie Ding <pyrocat101@users.noreply.github.com>",
7
7
  "type": "module",
@@ -14,13 +14,13 @@
14
14
  "./locale-data/*": "./locale-data/*"
15
15
  },
16
16
  "dependencies": {
17
- "tslib": "^2.8.0",
18
- "@formatjs/ecma402-abstract": "3.0.7",
19
- "@formatjs/intl-localematcher": "0.7.4"
17
+ "tslib": "^2.8.1",
18
+ "@formatjs/ecma402-abstract": "3.1.0",
19
+ "@formatjs/intl-localematcher": "0.8.0"
20
20
  },
21
21
  "devDependencies": {
22
- "@formatjs/intl-getcanonicallocales": "3.1.1",
23
- "@formatjs/intl-locale": "5.1.1"
22
+ "@formatjs/intl-getcanonicallocales": "3.2.0",
23
+ "@formatjs/intl-locale": "5.2.0"
24
24
  },
25
25
  "bugs": "https://github.com/formatjs/formatjs/issues",
26
26
  "homepage": "https://github.com/formatjs/formatjs",
package/polyfill-force.js CHANGED
@@ -1,7 +1,7 @@
1
- import { DisplayNames } from './index.js';
2
- Object.defineProperty(Intl, 'DisplayNames', {
3
- value: DisplayNames,
4
- enumerable: false,
5
- writable: true,
6
- configurable: true,
1
+ import { DisplayNames } from "./index.js";
2
+ Object.defineProperty(Intl, "DisplayNames", {
3
+ value: DisplayNames,
4
+ enumerable: false,
5
+ writable: true,
6
+ configurable: true
7
7
  });