@formatjs/intl-displaynames 7.3.1 → 7.3.3

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.
package/index.d.ts CHANGED
@@ -1,27 +1,102 @@
1
- import { type DisplayNamesData, type DisplayNamesLocaleData } from "@formatjs/ecma402-abstract";
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";
1
+ //#region packages/ecma402-abstract/types/core.d.ts
2
+ type Locale = string;
3
+ interface LocaleData<T> {
4
+ data: T;
5
+ locale: Locale;
8
6
  }
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"]>;
7
+ //#endregion
8
+ //#region packages/ecma402-abstract/types/displaynames.d.ts
9
+ type LanguageTag = string;
10
+ type RegionCode = string;
11
+ type ScriptCode = string;
12
+ type CurrencyCode = string;
13
+ type CalendarCode = string;
14
+ type DateTimeFieldCode = string;
15
+ interface DisplayNamesData {
16
+ /**
17
+ * Note that for style fields, `short` and `narrow` might not exist.
18
+ * At runtime, the fallback order will be narrow -> short -> long.
19
+ */
20
+ types: {
21
+ /**
22
+ * Maps language subtag like `zh-CN` to their display names.
23
+ */
24
+ language: {
25
+ dialect: {
26
+ narrow: Record<LanguageTag, string>;
27
+ short: Record<LanguageTag, string>;
28
+ long: Record<LanguageTag, string>;
29
+ };
30
+ standard: {
31
+ narrow: Record<LanguageTag, string>;
32
+ short: Record<LanguageTag, string>;
33
+ long: Record<LanguageTag, string>;
34
+ };
35
+ };
36
+ region: {
37
+ narrow: Record<RegionCode, string>;
38
+ short: Record<RegionCode, string>;
39
+ long: Record<RegionCode, string>;
40
+ };
41
+ script: {
42
+ narrow: Record<ScriptCode, string>;
43
+ short: Record<ScriptCode, string>;
44
+ long: Record<ScriptCode, string>;
45
+ };
46
+ currency: {
47
+ narrow: Record<CurrencyCode, string>;
48
+ short: Record<CurrencyCode, string>;
49
+ long: Record<CurrencyCode, string>;
50
+ };
51
+ calendar: {
52
+ narrow: Record<CalendarCode, string>;
53
+ short: Record<CalendarCode, string>;
54
+ long: Record<CalendarCode, string>;
55
+ };
56
+ dateTimeField: {
57
+ narrow: Record<DateTimeFieldCode, string>;
58
+ short: Record<DateTimeFieldCode, string>;
59
+ long: Record<DateTimeFieldCode, string>;
60
+ };
61
+ };
62
+ /**
63
+ * Not in spec, but we need this to display both language and region in display name.
64
+ * e.g. zh-Hans-SG + "{0}({1})" -> 简体中文(新加坡)
65
+ * Here {0} is replaced by language display name and {1} is replaced by region display name.
66
+ */
67
+ patterns: {
68
+ locale: string;
69
+ };
15
70
  }
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;
71
+ type DisplayNamesLocaleData = LocaleData<DisplayNamesData>;
72
+ //#endregion
73
+ //#region packages/intl-displaynames/index.d.ts
74
+ interface DisplayNamesOptions {
75
+ localeMatcher?: "lookup" | "best fit";
76
+ style?: "narrow" | "short" | "long";
77
+ type: "language" | "region" | "script" | "currency" | "calendar" | "dateTimeField";
78
+ fallback?: "code" | "none";
79
+ languageDisplay?: "dialect" | "standard";
27
80
  }
81
+ interface DisplayNamesResolvedOptions {
82
+ locale: string;
83
+ style: NonNullable<DisplayNamesOptions["style"]>;
84
+ type: NonNullable<DisplayNamesOptions["type"]>;
85
+ fallback: NonNullable<DisplayNamesOptions["fallback"]>;
86
+ languageDisplay: NonNullable<DisplayNamesOptions["languageDisplay"]>;
87
+ }
88
+ declare class DisplayNames {
89
+ constructor(locales: string | string[] | undefined, options: DisplayNamesOptions);
90
+ static supportedLocalesOf(locales?: string | string[], options?: Pick<DisplayNamesOptions, "localeMatcher">): string[];
91
+ static __addLocaleData(...data: DisplayNamesLocaleData[]): void;
92
+ of(code: string | number | Record<string, unknown>): string | undefined;
93
+ resolvedOptions(): DisplayNamesResolvedOptions;
94
+ static localeData: Record<string, DisplayNamesData | undefined>;
95
+ private static availableLocales;
96
+ private static __defaultLocale;
97
+ private static getDefaultLocale;
98
+ static readonly polyfilled = true;
99
+ }
100
+ //#endregion
101
+ export { DisplayNames, DisplayNamesOptions, DisplayNamesResolvedOptions };
102
+ //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -1,18 +1,256 @@
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 {
1
+ import { LookupSupportedLocales, ResolveLocale } from "@formatjs/intl-localematcher";
2
+ //#region packages/ecma262-abstract/ToString.js
3
+ /**
4
+ * https://tc39.es/ecma262/#sec-tostring
5
+ */
6
+ function ToString(o) {
7
+ if (typeof o === "symbol") throw TypeError("Cannot convert a Symbol value to a string");
8
+ return String(o);
9
+ }
10
+ //#endregion
11
+ //#region packages/ecma402-abstract/CanonicalizeLocaleList.js
12
+ /**
13
+ * http://ecma-international.org/ecma-402/7.0/index.html#sec-canonicalizelocalelist
14
+ * @param locales
15
+ */
16
+ function CanonicalizeLocaleList(locales) {
17
+ return Intl.getCanonicalLocales(locales);
18
+ }
19
+ //#endregion
20
+ //#region packages/ecma402-abstract/GetOption.js
21
+ /**
22
+ * https://tc39.es/ecma402/#sec-getoption
23
+ * @param opts
24
+ * @param prop
25
+ * @param type
26
+ * @param values
27
+ * @param fallback
28
+ */
29
+ function GetOption(opts, prop, type, values, fallback) {
30
+ if (typeof opts !== "object") throw new TypeError("Options must be an object");
31
+ let value = opts[prop];
32
+ if (value !== void 0) {
33
+ if (type !== "boolean" && type !== "string") throw new TypeError("invalid type");
34
+ if (type === "boolean") value = Boolean(value);
35
+ if (type === "string") value = ToString(value);
36
+ if (values !== void 0 && !values.filter((val) => val == value).length) throw new RangeError(`${value} is not within ${values.join(", ")}`);
37
+ return value;
38
+ }
39
+ return fallback;
40
+ }
41
+ //#endregion
42
+ //#region packages/ecma402-abstract/GetOptionsObject.js
43
+ /**
44
+ * https://tc39.es/ecma402/#sec-getoptionsobject
45
+ * @param options
46
+ * @returns
47
+ */
48
+ function GetOptionsObject(options) {
49
+ if (typeof options === "undefined") return Object.create(null);
50
+ if (typeof options === "object") return options;
51
+ throw new TypeError("Options must be an object");
52
+ }
53
+ //#endregion
54
+ //#region packages/ecma402-abstract/IsWellFormedCurrencyCode.js
55
+ /**
56
+ * This follows https://tc39.es/ecma402/#sec-case-sensitivity-and-case-mapping
57
+ * @param str string to convert
58
+ */
59
+ function toUpperCase(str) {
60
+ return str.replace(/([a-z])/g, (_, c) => c.toUpperCase());
61
+ }
62
+ const NOT_A_Z_REGEX = /[^A-Z]/;
63
+ /**
64
+ * https://tc39.es/ecma402/#sec-iswellformedcurrencycode
65
+ */
66
+ function IsWellFormedCurrencyCode(currency) {
67
+ currency = toUpperCase(currency);
68
+ if (currency.length !== 3) return false;
69
+ if (NOT_A_Z_REGEX.test(currency)) return false;
70
+ return true;
71
+ }
72
+ //#endregion
73
+ //#region packages/ecma262-abstract/ToObject.js
74
+ /**
75
+ * https://tc39.es/ecma262/#sec-toobject
76
+ */
77
+ function ToObject(arg) {
78
+ if (arg == null) throw new TypeError("undefined/null cannot be converted to object");
79
+ return Object(arg);
80
+ }
81
+ //#endregion
82
+ //#region packages/ecma402-abstract/SupportedLocales.js
83
+ /**
84
+ * https://tc39.es/ecma402/#sec-supportedlocales
85
+ * @param availableLocales
86
+ * @param requestedLocales
87
+ * @param options
88
+ */
89
+ function SupportedLocales(availableLocales, requestedLocales, options) {
90
+ let matcher = "best fit";
91
+ if (options !== void 0) {
92
+ options = ToObject(options);
93
+ matcher = GetOption(options, "localeMatcher", "string", ["lookup", "best fit"], "best fit");
94
+ }
95
+ if (matcher === "best fit") return LookupSupportedLocales(Array.from(availableLocales), requestedLocales);
96
+ return LookupSupportedLocales(Array.from(availableLocales), requestedLocales);
97
+ }
98
+ //#endregion
99
+ //#region node_modules/.aspect_rules_js/@formatjs+fast-memoize@0.0.0/node_modules/@formatjs/fast-memoize/index.js
100
+ function memoize(fn, options) {
101
+ const cache = options && options.cache ? options.cache : cacheDefault;
102
+ const serializer = options && options.serializer ? options.serializer : serializerDefault;
103
+ return (options && options.strategy ? options.strategy : strategyDefault)(fn, {
104
+ cache,
105
+ serializer
106
+ });
107
+ }
108
+ function isPrimitive(value) {
109
+ return value == null || typeof value === "number" || typeof value === "boolean";
110
+ }
111
+ function monadic(fn, cache, serializer, arg) {
112
+ const cacheKey = isPrimitive(arg) ? arg : serializer(arg);
113
+ let computedValue = cache.get(cacheKey);
114
+ if (typeof computedValue === "undefined") {
115
+ computedValue = fn.call(this, arg);
116
+ cache.set(cacheKey, computedValue);
117
+ }
118
+ return computedValue;
119
+ }
120
+ function variadic(fn, cache, serializer) {
121
+ const args = Array.prototype.slice.call(arguments, 3);
122
+ const cacheKey = serializer(args);
123
+ let computedValue = cache.get(cacheKey);
124
+ if (typeof computedValue === "undefined") {
125
+ computedValue = fn.apply(this, args);
126
+ cache.set(cacheKey, computedValue);
127
+ }
128
+ return computedValue;
129
+ }
130
+ function assemble(fn, context, strategy, cache, serialize) {
131
+ return strategy.bind(context, fn, cache, serialize);
132
+ }
133
+ function strategyDefault(fn, options) {
134
+ const strategy = fn.length === 1 ? monadic : variadic;
135
+ return assemble(fn, this, strategy, options.cache.create(), options.serializer);
136
+ }
137
+ function strategyVariadic(fn, options) {
138
+ return assemble(fn, this, variadic, options.cache.create(), options.serializer);
139
+ }
140
+ function strategyMonadic(fn, options) {
141
+ return assemble(fn, this, monadic, options.cache.create(), options.serializer);
142
+ }
143
+ const serializerDefault = function() {
144
+ return JSON.stringify(arguments);
145
+ };
146
+ var ObjectWithoutPrototypeCache = class {
147
+ constructor() {
148
+ this.cache = Object.create(null);
149
+ }
150
+ get(key) {
151
+ return this.cache[key];
152
+ }
153
+ set(key, value) {
154
+ this.cache[key] = value;
155
+ }
156
+ };
157
+ const cacheDefault = { create: function create() {
158
+ return new ObjectWithoutPrototypeCache();
159
+ } };
160
+ const strategies = {
161
+ variadic: strategyVariadic,
162
+ monadic: strategyMonadic
163
+ };
164
+ //#endregion
165
+ //#region packages/ecma402-abstract/utils.js
166
+ function setInternalSlot(map, pl, field, value) {
167
+ if (!map.get(pl)) map.set(pl, Object.create(null));
168
+ const slots = map.get(pl);
169
+ slots[field] = value;
170
+ }
171
+ function getInternalSlot(map, pl, field) {
172
+ return getMultiInternalSlots(map, pl, field)[field];
173
+ }
174
+ function getMultiInternalSlots(map, pl, ...fields) {
175
+ const slots = map.get(pl);
176
+ if (!slots) throw new TypeError(`${pl} InternalSlot has not been initialized`);
177
+ return fields.reduce((all, f) => {
178
+ all[f] = slots[f];
179
+ return all;
180
+ }, Object.create(null));
181
+ }
182
+ function invariant(condition, message, Err = Error) {
183
+ if (!condition) throw new Err(message);
184
+ }
185
+ memoize((...args) => new Intl.NumberFormat(...args), { strategy: strategies.variadic });
186
+ memoize((...args) => new Intl.PluralRules(...args), { strategy: strategies.variadic });
187
+ memoize((...args) => new Intl.Locale(...args), { strategy: strategies.variadic });
188
+ memoize((...args) => new Intl.ListFormat(...args), { strategy: strategies.variadic });
189
+ //#endregion
190
+ //#region packages/ecma402-abstract/DisplayNames/IsValidDateTimeFieldCode.js
191
+ const CODES_FOR_DATE_TIME_FIELD = [
192
+ "era",
193
+ "year",
194
+ "quarter",
195
+ "month",
196
+ "weekOfYear",
197
+ "weekday",
198
+ "day",
199
+ "dayPeriod",
200
+ "hour",
201
+ "minute",
202
+ "second",
203
+ "timeZoneName"
204
+ ];
205
+ function IsValidDateTimeFieldCode(field) {
206
+ return CODES_FOR_DATE_TIME_FIELD.indexOf(field) >= 0;
207
+ }
208
+ //#endregion
209
+ //#region packages/ecma402-abstract/DisplayNames/CanonicalCodeForDisplayNames.js
210
+ const UNICODE_REGION_SUBTAG_REGEX = /^([a-z]{2}|[0-9]{3})$/i;
211
+ const ALPHA_4 = /^[a-z]{4}$/i;
212
+ const UNICODE_TYPE_REGEX = /^[a-z0-9]{3,8}([-_][a-z0-9]{3,8})*$/i;
213
+ function isUnicodeRegionSubtag(region) {
214
+ return UNICODE_REGION_SUBTAG_REGEX.test(region);
215
+ }
216
+ function isUnicodeScriptSubtag(script) {
217
+ return ALPHA_4.test(script);
218
+ }
219
+ function isUnicodeLocaleIdentifierType(code) {
220
+ return UNICODE_TYPE_REGEX.test(code);
221
+ }
222
+ function CanonicalCodeForDisplayNames(type, code) {
223
+ if (type === "language") return CanonicalizeLocaleList([code])[0];
224
+ if (type === "region") {
225
+ if (!isUnicodeRegionSubtag(code)) throw RangeError("invalid region");
226
+ return code.toUpperCase();
227
+ }
228
+ if (type === "script") {
229
+ if (!isUnicodeScriptSubtag(code)) throw RangeError("invalid script");
230
+ return `${code[0].toUpperCase()}${code.slice(1).toLowerCase()}`;
231
+ }
232
+ if (type === "calendar") {
233
+ if (!isUnicodeLocaleIdentifierType(code)) throw RangeError("invalid calendar");
234
+ return code.toLowerCase();
235
+ }
236
+ if (type === "dateTimeField") {
237
+ if (!IsValidDateTimeFieldCode(code)) throw RangeError("invalid dateTimeField");
238
+ return code;
239
+ }
240
+ invariant(type === "currency", "invalid type");
241
+ if (!IsWellFormedCurrencyCode(code)) throw RangeError("invalid currency");
242
+ return code.toUpperCase();
243
+ }
244
+ //#endregion
245
+ //#region packages/intl-displaynames/index.ts
246
+ var DisplayNames = class DisplayNames {
6
247
  constructor(locales, options) {
7
- if (new.target === undefined) {
8
- throw TypeError(`Constructor Intl.DisplayNames requires 'new'`);
9
- }
248
+ if (new.target === void 0) throw TypeError(`Constructor Intl.DisplayNames requires 'new'`);
10
249
  const requestedLocales = CanonicalizeLocaleList(locales);
11
250
  options = GetOptionsObject(options);
12
251
  const opt = Object.create(null);
13
252
  const { localeData } = DisplayNames;
14
- const matcher = GetOption(options, "localeMatcher", "string", ["lookup", "best fit"], "best fit");
15
- opt.localeMatcher = matcher;
253
+ opt.localeMatcher = GetOption(options, "localeMatcher", "string", ["lookup", "best fit"], "best fit");
16
254
  const r = ResolveLocale(Array.from(DisplayNames.availableLocales), requestedLocales, opt, [], DisplayNames.localeData, DisplayNames.getDefaultLocale);
17
255
  const style = GetOption(options, "style", "string", [
18
256
  "narrow",
@@ -27,10 +265,8 @@ export class DisplayNames {
27
265
  "currency",
28
266
  "calendar",
29
267
  "dateTimeField"
30
- ], undefined);
31
- if (type === undefined) {
32
- throw TypeError(`Intl.DisplayNames constructor requires "type" option`);
33
- }
268
+ ], void 0);
269
+ if (type === void 0) throw TypeError(`Intl.DisplayNames constructor requires "type" option`);
34
270
  setSlot(this, "type", type);
35
271
  const fallback = GetOption(options, "fallback", "string", ["code", "none"], "code");
36
272
  setSlot(this, "fallback", fallback);
@@ -39,7 +275,7 @@ export class DisplayNames {
39
275
  const dataLocaleData = localeData[dataLocale];
40
276
  invariant(!!dataLocaleData, `Missing locale data for ${dataLocale}`);
41
277
  setSlot(this, "localeData", dataLocaleData);
42
- invariant(dataLocaleData !== undefined, `locale data for ${r.locale} does not exist.`);
278
+ invariant(dataLocaleData !== void 0, `locale data for ${r.locale} does not exist.`);
43
279
  const { types } = dataLocaleData;
44
280
  invariant(typeof types === "object" && types != null, "invalid types data");
45
281
  const typeFields = types[type];
@@ -47,11 +283,9 @@ export class DisplayNames {
47
283
  const languageDisplay = GetOption(options, "languageDisplay", "string", ["dialect", "standard"], "dialect");
48
284
  if (type === "language") {
49
285
  setSlot(this, "languageDisplay", languageDisplay);
50
- // Using types[type] instead of typeFields because TypeScript cannot infer the correct type
51
286
  const typeFields = types[type][languageDisplay];
52
287
  invariant(typeof typeFields === "object" && typeFields != null, "invalid language typeFields data");
53
288
  }
54
- // Using types[type] instead of typeFields because TypeScript cannot infer the correct type
55
289
  const styleFields = type === "language" ? types[type][languageDisplay][style] : types[type][style];
56
290
  invariant(typeof styleFields === "object" && styleFields != null, "invalid styleFields data");
57
291
  setSlot(this, "fields", styleFields);
@@ -65,83 +299,62 @@ export class DisplayNames {
65
299
  DisplayNames.localeData[locale] = DisplayNames.localeData[minimizedLocale] = d;
66
300
  DisplayNames.availableLocales.add(minimizedLocale);
67
301
  DisplayNames.availableLocales.add(locale);
68
- if (!DisplayNames.__defaultLocale) {
69
- DisplayNames.__defaultLocale = minimizedLocale;
70
- }
302
+ if (!DisplayNames.__defaultLocale) DisplayNames.__defaultLocale = minimizedLocale;
71
303
  }
72
304
  }
73
305
  of(code) {
74
306
  checkReceiver(this, "of");
75
307
  const type = getSlot(this, "type");
76
308
  const codeAsString = ToString(code);
77
- if (!isValidCodeForDisplayNames(type, codeAsString)) {
78
- throw RangeError("invalid code for Intl.DisplayNames.prototype.of");
79
- }
309
+ if (!isValidCodeForDisplayNames(type, codeAsString)) throw RangeError("invalid code for Intl.DisplayNames.prototype.of");
80
310
  const { localeData, style, fallback } = getMultiInternalSlots(__INTERNAL_SLOT_MAP__, this, "localeData", "style", "fallback");
81
- // Canonicalize the case.
82
311
  let canonicalCode = CanonicalCodeForDisplayNames(type, codeAsString);
83
312
  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
313
+ if (type === "language") name = getNameForTypeLanguage(getSlot(this, "languageDisplay"), localeData, style, canonicalCode, fallback);
314
+ else {
89
315
  const typesData = localeData.types[type];
90
316
  name = typesData[style][canonicalCode] || typesData.long[canonicalCode];
91
317
  }
92
- if (name !== undefined) {
93
- return name;
94
- }
95
- if (fallback === "code") {
96
- return codeAsString;
97
- }
318
+ if (name !== void 0) return name;
319
+ if (fallback === "code") return codeAsString;
98
320
  }
99
321
  resolvedOptions() {
100
322
  checkReceiver(this, "resolvedOptions");
101
323
  return { ...getMultiInternalSlots(__INTERNAL_SLOT_MAP__, this, "locale", "style", "type", "fallback", "languageDisplay") };
102
324
  }
103
- static localeData = {};
104
- static availableLocales = new Set();
105
- static __defaultLocale = "";
325
+ static {
326
+ this.localeData = {};
327
+ }
328
+ static {
329
+ this.availableLocales = /* @__PURE__ */ new Set();
330
+ }
331
+ static {
332
+ this.__defaultLocale = "";
333
+ }
106
334
  static getDefaultLocale() {
107
335
  return DisplayNames.__defaultLocale;
108
336
  }
109
- static polyfilled = true;
110
- }
111
- // https://tc39.es/proposal-intl-displaynames/#sec-isvalidcodefordisplaynames
337
+ static {
338
+ this.polyfilled = true;
339
+ }
340
+ };
112
341
  function isValidCodeForDisplayNames(type, code) {
113
342
  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);
343
+ case "language": 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);
344
+ case "region": return /^([a-z]{2}|\d{3})$/i.test(code);
345
+ case "script": return /^[a-z]{4}$/i.test(code);
128
346
  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);
347
+ case "calendar": return /^[a-z0-9]{3,8}([-_][a-z0-9]{3,8})*$/i.test(code);
132
348
  case "dateTimeField": return IsValidDateTimeFieldCode(code);
133
349
  }
134
350
  }
135
351
  try {
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
- }
352
+ if (typeof Symbol !== "undefined" && Symbol.toStringTag) Object.defineProperty(DisplayNames.prototype, Symbol.toStringTag, {
353
+ value: "Intl.DisplayNames",
354
+ configurable: true,
355
+ enumerable: false,
356
+ writable: false
357
+ });
145
358
  Object.defineProperty(DisplayNames, "length", {
146
359
  value: 2,
147
360
  writable: false,
@@ -149,7 +362,7 @@ try {
149
362
  configurable: true
150
363
  });
151
364
  } catch {}
152
- const __INTERNAL_SLOT_MAP__ = new WeakMap();
365
+ const __INTERNAL_SLOT_MAP__ = /* @__PURE__ */ new WeakMap();
153
366
  function getSlot(instance, key) {
154
367
  return getInternalSlot(__INTERNAL_SLOT_MAP__, instance, key);
155
368
  }
@@ -157,40 +370,26 @@ function setSlot(instance, key, value) {
157
370
  setInternalSlot(__INTERNAL_SLOT_MAP__, instance, key, value);
158
371
  }
159
372
  function checkReceiver(receiver, methodName) {
160
- if (!(receiver instanceof DisplayNames)) {
161
- throw TypeError(`Method Intl.DisplayNames.prototype.${methodName} called on incompatible receiver`);
162
- }
373
+ if (!(receiver instanceof DisplayNames)) throw TypeError(`Method Intl.DisplayNames.prototype.${methodName} called on incompatible receiver`);
163
374
  }
164
375
  function getNameForTypeLanguage(languageDisplay, localeData, style, canonicalCode, fallback) {
165
- // First, try to get the name using the canonicalCode
166
376
  const typesData = localeData.types.language[languageDisplay];
167
377
  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
378
+ if (name === void 0) {
171
379
  const regionMatch = /-([a-z]{2}|\d{3})\b/i.exec(canonicalCode);
172
380
  if (regionMatch) {
173
- // Extract the language and region sub tags
174
381
  const languageSubTag = canonicalCode.substring(0, regionMatch.index) + canonicalCode.substring(regionMatch.index + regionMatch[0].length);
175
382
  const regionSubTag = regionMatch[1];
176
- // Let's try again using languageSubTag this time
177
383
  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
384
+ if (name !== void 0 && regionSubTag) {
182
385
  const regionsData = localeData.types.region;
183
386
  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
- }
387
+ if (regionDisplayName || fallback === "code") return localeData.patterns.locale.replace("{0}", name).replace("{1}", regionDisplayName || regionSubTag);
388
+ } else return name;
192
389
  }
193
- } else {
194
- return name;
195
- }
390
+ } else return name;
196
391
  }
392
+ //#endregion
393
+ export { DisplayNames };
394
+
395
+ //# sourceMappingURL=index.js.map