resolve-accept-language 2.1.23 → 3.0.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.
- package/README.md +28 -18
- package/lib/cjs/directives.d.ts +27 -0
- package/lib/cjs/directives.js +1 -0
- package/lib/cjs/index.d.ts +42 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/locales.d.ts +42 -0
- package/lib/cjs/locales.js +1 -0
- package/lib/esm/directives.d.ts +27 -0
- package/lib/esm/directives.js +1 -0
- package/lib/esm/index.d.ts +42 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/locales.d.ts +42 -0
- package/lib/esm/locales.js +1 -0
- package/package.json +17 -15
- package/lib/cjs/locale-list.d.ts +0 -19
- package/lib/cjs/locale-list.js +0 -34
- package/lib/cjs/locale.d.ts +0 -39
- package/lib/cjs/locale.js +0 -56
- package/lib/cjs/lookup-list.d.ts +0 -79
- package/lib/cjs/lookup-list.js +0 -209
- package/lib/cjs/resolve-accept-language.d.ts +0 -63
- package/lib/cjs/resolve-accept-language.js +0 -106
- package/lib/esm/locale-list.d.ts +0 -19
- package/lib/esm/locale-list.js +0 -32
- package/lib/esm/locale.d.ts +0 -39
- package/lib/esm/locale.js +0 -54
- package/lib/esm/lookup-list.d.ts +0 -79
- package/lib/esm/lookup-list.js +0 -207
- package/lib/esm/resolve-accept-language.d.ts +0 -63
- package/lib/esm/resolve-accept-language.js +0 -103
package/README.md
CHANGED
|
@@ -9,6 +9,8 @@ Resolve the best locale based on the value of an `Accept-Language` HTTP header.
|
|
|
9
9
|
|
|
10
10
|
## Usage
|
|
11
11
|
|
|
12
|
+
> ⚠ In March 2024, version 3 of this package was released, which includes breaking changes. Please refer to the [upgrade guide](./V2-TO-V3-UPGRADE-GUIDE.md) before upgrading.
|
|
13
|
+
|
|
12
14
|
Add the package as a dependency:
|
|
13
15
|
|
|
14
16
|
```
|
|
@@ -18,7 +20,7 @@ npm install resolve-accept-language
|
|
|
18
20
|
Code example:
|
|
19
21
|
|
|
20
22
|
```ts
|
|
21
|
-
import resolveAcceptLanguage from 'resolve-accept-language'
|
|
23
|
+
import { resolveAcceptLanguage } from 'resolve-accept-language'
|
|
22
24
|
|
|
23
25
|
/**
|
|
24
26
|
* The API is well documented from within your IDE using TSDoc. The arguments are as follows:
|
|
@@ -45,26 +47,29 @@ fr-CA
|
|
|
45
47
|
|
|
46
48
|
## Advanced use cases
|
|
47
49
|
|
|
48
|
-
You may want to control exactly the behavior depending on the type of match. For example, you
|
|
50
|
+
You may want to control exactly the behavior depending on the type of match. For example, you might want to display a language picker on your home page if the match is not satisfactory. In those cases, you will need to use the `{ returnMatchType: true }` option. It offers more visibility into the selection process while matching a locale:
|
|
49
51
|
|
|
50
52
|
```ts
|
|
51
|
-
import { MATCH_TYPES,
|
|
53
|
+
import { MATCH_TYPES, resolveAcceptLanguage } from 'resolve-accept-language'
|
|
52
54
|
|
|
53
|
-
const
|
|
55
|
+
const { match, matchType } = resolveAcceptLanguage(
|
|
54
56
|
'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001' as const,
|
|
55
57
|
['en-US', 'fr-CA'],
|
|
56
|
-
'fr-CA'
|
|
58
|
+
'fr-CA',
|
|
59
|
+
{ returnMatchType: true }
|
|
57
60
|
)
|
|
58
61
|
|
|
59
|
-
console.log(`A locale was matched: ${
|
|
62
|
+
console.log(`A locale was matched: ${match}`)
|
|
60
63
|
|
|
61
|
-
if (
|
|
64
|
+
if (matchType === MATCH_TYPES.locale) {
|
|
62
65
|
console.log('The match is locale-based')
|
|
63
|
-
} else if (
|
|
66
|
+
} else if (matchType === MATCH_TYPES.languageSpecificLocale) {
|
|
67
|
+
console.log('The match is language specific locale-based')
|
|
68
|
+
} else if (matchType === MATCH_TYPES.language) {
|
|
64
69
|
console.log('The match is language-based')
|
|
65
|
-
} else if (
|
|
70
|
+
} else if (matchType === MATCH_TYPES.relatedLocale) {
|
|
66
71
|
console.log('The match is related-locale-based')
|
|
67
|
-
} else if (
|
|
72
|
+
} else if (matchType === MATCH_TYPES.defaultLocale) {
|
|
68
73
|
console.log('The match is the default locale')
|
|
69
74
|
}
|
|
70
75
|
```
|
|
@@ -75,14 +80,19 @@ As per RFC 4647, this package uses the "lookup" matching scheme. This means that
|
|
|
75
80
|
|
|
76
81
|
The matching strategy will use the following logic:
|
|
77
82
|
|
|
78
|
-
1. The default locale
|
|
79
|
-
2. All locales and languages are extracted from the HTTP header and sorted by quality factor
|
|
80
|
-
3.
|
|
81
|
-
1.
|
|
82
|
-
1.
|
|
83
|
-
2.
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
1. The default locale will always be placed as the first locale being evaluated since it is considered the highest quality content available. Otherwise, the locales will be evaluated in the order provided.
|
|
84
|
+
2. All locales and languages are extracted from the HTTP header and sorted by quality factor and position in the header.
|
|
85
|
+
3. The following matching logic will be performed:
|
|
86
|
+
1. Perform a first loop on the directives
|
|
87
|
+
1. If the directive is a locale, then try to find an exact locale match (**locale match**)
|
|
88
|
+
2. If the directive is a language, then
|
|
89
|
+
1. Create a new list of directives that have locales matching the language
|
|
90
|
+
2. Loop through this new list, and try to find an exact locale match (**language-specific locale match**)
|
|
91
|
+
3. If no match is found, then try to find a locale that matches the language (**language match**)
|
|
92
|
+
2. Perform a second loop on the directives
|
|
93
|
+
1. Create a new list of languages from the locales
|
|
94
|
+
2. Loop through this new list, then try to find a locale that matches the language (**related-locale match**)
|
|
95
|
+
3. If everything fails, then the fallback is the default locale (**default locale match**)
|
|
86
96
|
|
|
87
97
|
## Why another `Accept-Language` package?
|
|
88
98
|
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** An object representing an HTTP `Accept-Language` header directive. */
|
|
2
|
+
type Directive = {
|
|
3
|
+
/** The ISO 639-1 alpha-2 language code. */
|
|
4
|
+
languageCode: string;
|
|
5
|
+
/** The locale identifier using the BCP 47 `language`-`country` in case-normalized format. */
|
|
6
|
+
locale?: string;
|
|
7
|
+
/** The quality factor (default is 1; values can range from 0 to 1 with up to 3 decimals). */
|
|
8
|
+
quality: number;
|
|
9
|
+
};
|
|
10
|
+
/** A directive object including the position in its HTTP header. */
|
|
11
|
+
type IndexedDirective = Directive & {
|
|
12
|
+
/** This is the position of the directive in the HTTP header. */
|
|
13
|
+
headerPosition: number;
|
|
14
|
+
};
|
|
15
|
+
/** A directive object including the position in its HTTP header and the locale. */
|
|
16
|
+
export type IndexedDirectiveWithLocale = IndexedDirective & {
|
|
17
|
+
locale: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Get a list of directives from an HTTP `Accept-Language` header.
|
|
21
|
+
*
|
|
22
|
+
* @param acceptLanguageHeader - The value of an HTTP request `Accept-Language` header (also known as a "language priority list").
|
|
23
|
+
*
|
|
24
|
+
* @returns A list of `IndexedDirective` objects sorted by quality and header position.
|
|
25
|
+
*/
|
|
26
|
+
export declare const getDirectives: (acceptLanguageHeader: string) => IndexedDirective[];
|
|
27
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var __assign=this&&this.__assign||function(){return __assign=Object.assign||function(e){for(var r,t=1,a=arguments.length;t<a;t++)for(var i in r=arguments[t])Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i]);return e},__assign.apply(this,arguments)},__read=this&&this.__read||function(e,r){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var a,i,o=t.call(e),n=[];try{for(;(void 0===r||r-- >0)&&!(a=o.next()).done;)n.push(a.value)}catch(e){i={error:e}}finally{try{a&&!a.done&&(t=o.return)&&t.call(o)}finally{if(i)throw i.error}}return n},__spreadArray=this&&this.__spreadArray||function(e,r,t){if(t||2===arguments.length)for(var a,i=0,o=r.length;i<o;i++)!a&&i in r||(a||(a=Array.prototype.slice.call(r,0,i)),a[i]=r[i]);return e.concat(a||Array.prototype.slice.call(r))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.getDirectives=void 0;var getDirective=function(e){var r=e.match(/^((?<matchedLanguageCode>([a-z]{2}))(-(?<matchedCountryCode>[a-z]{2}|419))?)(;q=(?<matchedQuality>(1(\.0{0,3})?)|(0(\.\d{0,3})?)))?$/i);if(null==r?void 0:r.groups){var t=r.groups,a=t.matchedLanguageCode,i=t.matchedCountryCode,o=t.matchedQuality,n=a.toLowerCase(),s=i?i.toUpperCase():void 0;if("419"!==s||"es"===n){var c=void 0===o?1:Number.parseFloat(o);return{languageCode:n,locale:s?"".concat(n,"-").concat(s):void 0,quality:c}}}},getDirectives=function(e){var r=[],t=0;e.split(",").forEach((function(e){var a=getDirective(e.trim());a&&(r.some((function(e){return e.languageCode===a.languageCode&&e.locale===a.locale}))||(r.push(__assign(__assign({},a),{headerPosition:t})),t++))}));var a=r.findIndex((function(e){return"es-419"===e.locale}));if(a>=0){var i=r[a],o=["es-AR","es-CL","es-CO","es-CR","es-HN","es-MX","es-PE","es-US","es-UY","es-VE"].map((function(e){return __assign(__assign({},i),{locale:e})}));r.splice.apply(r,__spreadArray([a,1],__read(o),!1))}return r.sort((function(e,r){var t=r.quality-e.quality;return t||e.headerPosition-r.headerPosition}))};exports.getDirectives=getDirectives;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** The type of matches. */
|
|
2
|
+
export type MatchType = 'locale' | 'languageSpecificLocale' | 'language' | 'relatedLocale' | 'defaultLocale';
|
|
3
|
+
/** The type of matches enumeration. */
|
|
4
|
+
export declare const MATCH_TYPES: {
|
|
5
|
+
readonly [K in MatchType]: K;
|
|
6
|
+
};
|
|
7
|
+
/** Type to normalize the locale format. */
|
|
8
|
+
export type NormalizeLocale<Remainder extends string> = Remainder extends `${infer LanguageCode}-${infer CountryCode}` ? `${Lowercase<LanguageCode>}-${Uppercase<CountryCode>}` : Remainder;
|
|
9
|
+
/** Additional options to apply. */
|
|
10
|
+
type Options<WithMatchType extends boolean | undefined> = {
|
|
11
|
+
/** Should the match type be returned? */
|
|
12
|
+
returnMatchType?: WithMatchType;
|
|
13
|
+
};
|
|
14
|
+
type Result<Locales extends string[], WithMatchType extends boolean | undefined> = WithMatchType extends true ? {
|
|
15
|
+
/** The best locale match. */
|
|
16
|
+
match: NormalizeLocale<Locales[number]>;
|
|
17
|
+
/** The type of match. */
|
|
18
|
+
matchType: MatchType;
|
|
19
|
+
} : NormalizeLocale<Locales[number]>;
|
|
20
|
+
/**
|
|
21
|
+
* Resolve the preferred locale from an HTTP `Accept-Language` header.
|
|
22
|
+
*
|
|
23
|
+
* All locale identifiers provided as parameters must following the BCP 47 `language`-`country` (case insensitive).
|
|
24
|
+
*
|
|
25
|
+
* @param acceptLanguageHeader - The value of an HTTP request `Accept-Language` header (also known as a "language priority list").
|
|
26
|
+
* @param locales - An array of locale identifiers that must include the default locale. The order will be used for matching where
|
|
27
|
+
* the first identifier will be more likely to be matched than the last identifier.
|
|
28
|
+
* @param defaultLocale - The default locale identifier when no match is found.
|
|
29
|
+
* @param options - Additional options to apply.
|
|
30
|
+
*
|
|
31
|
+
* @returns Either the best locale match or a match object, depending on options.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* // returns 'fr-CA'
|
|
35
|
+
* resolveAcceptLanguage(
|
|
36
|
+
* 'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001',
|
|
37
|
+
* ['en-US', 'fr-CA'],
|
|
38
|
+
* 'en-US'
|
|
39
|
+
* )
|
|
40
|
+
*/
|
|
41
|
+
export declare const resolveAcceptLanguage: <Locales extends string[], WithMatchType extends boolean | undefined = undefined>(acceptLanguageHeader: string, locales: Locales, defaultLocale: Locales[number], options?: Options<WithMatchType> | undefined) => Result<Locales, WithMatchType>;
|
|
42
|
+
export {};
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var __read=this&&this.__read||function(e,a){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var t,o,l=r.call(e),n=[];try{for(;(void 0===a||a-- >0)&&!(t=l.next()).done;)n.push(t.value)}catch(e){o={error:e}}finally{try{t&&!t.done&&(r=l.return)&&r.call(l)}finally{if(o)throw o.error}}return n},__spreadArray=this&&this.__spreadArray||function(e,a,r){if(r||2===arguments.length)for(var t,o=0,l=a.length;o<l;o++)!t&&o in a||(t||(t=Array.prototype.slice.call(a,0,o)),t[o]=a[o]);return e.concat(t||Array.prototype.slice.call(a))},__values=this&&this.__values||function(e){var a="function"==typeof Symbol&&Symbol.iterator,r=a&&e[a],t=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&t>=e.length&&(e=void 0),{value:e&&e[t++],done:!e}}};throw new TypeError(a?"Object is not iterable.":"Symbol.iterator is not defined.")};Object.defineProperty(exports,"__esModule",{value:!0}),exports.resolveAcceptLanguage=exports.MATCH_TYPES=void 0;var directives_1=require("./directives"),locales_1=require("./locales");exports.MATCH_TYPES={locale:"locale",languageSpecificLocale:"languageSpecificLocale",language:"language",relatedLocale:"relatedLocale",defaultLocale:"defaultLocale"};var resolveAcceptLanguage=function(e,a,r,t){if(a.forEach((function(e){if(!(0,locales_1.isLocale)(e,!1))throw new Error("Invalid locale identifier '".concat(e,"'. A valid locale should follow the BCP 47 'language-country' format."))})),!(0,locales_1.isLocale)(r,!1))throw new Error("Invalid default locale identifier '".concat(r,"'. A valid locale should follow the BCP 47 'language-country' format."));if(!a.some((function(e){return e.toLowerCase()===r.toLowerCase()})))throw new Error("The default locale '".concat(r,"' must be included in the locales array because it is used as a fallback when no match is found."));var o=new locales_1.Locale(r).identifier,l=new Set(__spreadArray([o],__read(a.map((function(e){return new locales_1.Locale(e).identifier}))),!1)),n=function(){var a,r,t,n,c=new locales_1.LocaleList(l),i=(0,directives_1.getDirectives)(e),u=function(e){var a=e.locale,r=e.languageCode;if(!c.languages.has(r))return"continue";if(void 0!==a)return c.locales.has(a)?{value:{match:a,matchType:exports.MATCH_TYPES.locale}}:"continue";var t=i.find((function(e){return e.languageCode===r&&void 0!==e.locale&&c.locales.has(e.locale)}));if(t)return{value:{match:t.locale,matchType:exports.MATCH_TYPES.languageSpecificLocale}};var o=c.objects.find((function(e){return e.languageCode===r}));return o?{value:{match:o.identifier,matchType:exports.MATCH_TYPES.language}}:void 0};try{for(var f=__values(i),s=f.next();!s.done;s=f.next()){var d=u(s.value);if("object"==typeof d)return d.value}}catch(e){a={error:e}}finally{try{s&&!s.done&&(r=f.return)&&r.call(f)}finally{if(a)throw a.error}}var v=function(e){if(!c.languages.has(e.languageCode))return"continue";var a=c.objects.find((function(a){return a.languageCode===e.languageCode}));return a?{value:{match:a.identifier,matchType:exports.MATCH_TYPES.relatedLocale}}:void 0};try{for(var h=__values(i),g=h.next();!g.done;g=h.next()){var y=v(g.value);if("object"==typeof y)return y.value}}catch(e){t={error:e}}finally{try{g&&!g.done&&(n=h.return)&&n.call(h)}finally{if(t)throw t.error}}return{match:o,matchType:exports.MATCH_TYPES.defaultLocale}}();return(null==t?void 0:t.returnMatchType)?n:n.match};exports.resolveAcceptLanguage=resolveAcceptLanguage;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** Class to manage a locale identifier using the BCP 47 `language`-`country` format. */
|
|
2
|
+
export declare class Locale {
|
|
3
|
+
/** The ISO 3166-1 alpha-2 country code. */
|
|
4
|
+
readonly countryCode: string;
|
|
5
|
+
/** The locale identifier using the BCP 47 `language`-`country` case-normalized format. */
|
|
6
|
+
readonly identifier: string;
|
|
7
|
+
/** The ISO 639-1 alpha-2 language code. */
|
|
8
|
+
readonly languageCode: string;
|
|
9
|
+
/**
|
|
10
|
+
* Create a new `Locale` object.
|
|
11
|
+
*
|
|
12
|
+
* @param identifier - A locale identifier using the BCP 47 `language`-`country` format (case insensitive).
|
|
13
|
+
*
|
|
14
|
+
* @throws An error if the `identifier` format is invalid.
|
|
15
|
+
*/
|
|
16
|
+
constructor(identifier: string);
|
|
17
|
+
}
|
|
18
|
+
export declare class LocaleList {
|
|
19
|
+
/** A set of ISO 3166-1 alpha-2 country codes. */
|
|
20
|
+
readonly countries: Set<string>;
|
|
21
|
+
/** A set of ISO 639-1 alpha-2 language codes. */
|
|
22
|
+
readonly languages: Set<string>;
|
|
23
|
+
/** A set of locale identifiers using the BCP 47 `language`-`country` case-normalized format. */
|
|
24
|
+
readonly locales: Set<string>;
|
|
25
|
+
/** A list of locale objects. */
|
|
26
|
+
readonly objects: Locale[];
|
|
27
|
+
/**
|
|
28
|
+
* Create a list of locale identifiers.
|
|
29
|
+
*
|
|
30
|
+
* @param locales - An set of unique locale identifiers using the BCP 47 `language`-`country` format (case insensitive).
|
|
31
|
+
*
|
|
32
|
+
* @throws Will throw an error if one of the locale's format is invalid.
|
|
33
|
+
*/
|
|
34
|
+
constructor(locales: Set<string>);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Is a given string a locale identifier following the BCP 47 `language`-`country` format.
|
|
38
|
+
*
|
|
39
|
+
* @param identifier - A potential locale identify to verify.
|
|
40
|
+
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
|
|
41
|
+
*/
|
|
42
|
+
export declare const isLocale: (identifier: string, caseNormalized?: boolean) => boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var __read=this&&this.__read||function(e,o){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var a,r,i=t.call(e),s=[];try{for(;(void 0===o||o-- >0)&&!(a=i.next()).done;)s.push(a.value)}catch(e){r={error:e}}finally{try{a&&!a.done&&(t=i.return)&&t.call(i)}finally{if(r)throw r.error}}return s};Object.defineProperty(exports,"__esModule",{value:!0}),exports.isLocale=exports.LocaleList=exports.Locale=void 0;var Locale=function(e){if(!(0,exports.isLocale)(e,!1))throw new Error("invalid locale identifier '".concat(e,"'"));var o=__read(e.split("-"),2),t=o[0],a=o[1];this.languageCode=t.toLowerCase(),this.countryCode=a.toUpperCase(),this.identifier="".concat(this.languageCode,"-").concat(this.countryCode)};exports.Locale=Locale;var LocaleList=function(e){var o=this;this.countries=new Set,this.languages=new Set,this.locales=new Set,this.objects=[],e.forEach((function(e){var t=new Locale(e);o.objects.push(t),o.locales.add(t.identifier),o.languages.add(t.languageCode),o.countries.add(t.countryCode)}))};exports.LocaleList=LocaleList;var isLocale=function(e,o){return void 0===o&&(o=!0),new RegExp(/^[a-z]{2}-[A-Z]{2}$/,o?void 0:"i").test(e)};exports.isLocale=isLocale;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** An object representing an HTTP `Accept-Language` header directive. */
|
|
2
|
+
type Directive = {
|
|
3
|
+
/** The ISO 639-1 alpha-2 language code. */
|
|
4
|
+
languageCode: string;
|
|
5
|
+
/** The locale identifier using the BCP 47 `language`-`country` in case-normalized format. */
|
|
6
|
+
locale?: string;
|
|
7
|
+
/** The quality factor (default is 1; values can range from 0 to 1 with up to 3 decimals). */
|
|
8
|
+
quality: number;
|
|
9
|
+
};
|
|
10
|
+
/** A directive object including the position in its HTTP header. */
|
|
11
|
+
type IndexedDirective = Directive & {
|
|
12
|
+
/** This is the position of the directive in the HTTP header. */
|
|
13
|
+
headerPosition: number;
|
|
14
|
+
};
|
|
15
|
+
/** A directive object including the position in its HTTP header and the locale. */
|
|
16
|
+
export type IndexedDirectiveWithLocale = IndexedDirective & {
|
|
17
|
+
locale: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Get a list of directives from an HTTP `Accept-Language` header.
|
|
21
|
+
*
|
|
22
|
+
* @param acceptLanguageHeader - The value of an HTTP request `Accept-Language` header (also known as a "language priority list").
|
|
23
|
+
*
|
|
24
|
+
* @returns A list of `IndexedDirective` objects sorted by quality and header position.
|
|
25
|
+
*/
|
|
26
|
+
export declare const getDirectives: (acceptLanguageHeader: string) => IndexedDirective[];
|
|
27
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var __assign=this&&this.__assign||function(){return __assign=Object.assign||function(e){for(var r,a=1,t=arguments.length;a<t;a++)for(var n in r=arguments[a])Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n]);return e},__assign.apply(this,arguments)},__read=this&&this.__read||function(e,r){var a="function"==typeof Symbol&&e[Symbol.iterator];if(!a)return e;var t,n,o=a.call(e),i=[];try{for(;(void 0===r||r-- >0)&&!(t=o.next()).done;)i.push(t.value)}catch(e){n={error:e}}finally{try{t&&!t.done&&(a=o.return)&&a.call(o)}finally{if(n)throw n.error}}return i},__spreadArray=this&&this.__spreadArray||function(e,r,a){if(a||2===arguments.length)for(var t,n=0,o=r.length;n<o;n++)!t&&n in r||(t||(t=Array.prototype.slice.call(r,0,n)),t[n]=r[n]);return e.concat(t||Array.prototype.slice.call(r))},getDirective=function(e){var r=e.match(/^((?<matchedLanguageCode>([a-z]{2}))(-(?<matchedCountryCode>[a-z]{2}|419))?)(;q=(?<matchedQuality>(1(\.0{0,3})?)|(0(\.\d{0,3})?)))?$/i);if(null==r?void 0:r.groups){var a=r.groups,t=a.matchedLanguageCode,n=a.matchedCountryCode,o=a.matchedQuality,i=t.toLowerCase(),s=n?n.toUpperCase():void 0;if("419"!==s||"es"===i){var l=void 0===o?1:Number.parseFloat(o);return{languageCode:i,locale:s?"".concat(i,"-").concat(s):void 0,quality:l}}}};export var getDirectives=function(e){var r=[],a=0;e.split(",").forEach((function(e){var t=getDirective(e.trim());t&&(r.some((function(e){return e.languageCode===t.languageCode&&e.locale===t.locale}))||(r.push(__assign(__assign({},t),{headerPosition:a})),a++))}));var t=r.findIndex((function(e){return"es-419"===e.locale}));if(t>=0){var n=r[t],o=["es-AR","es-CL","es-CO","es-CR","es-HN","es-MX","es-PE","es-US","es-UY","es-VE"].map((function(e){return __assign(__assign({},n),{locale:e})}));r.splice.apply(r,__spreadArray([t,1],__read(o),!1))}return r.sort((function(e,r){var a=r.quality-e.quality;return a||e.headerPosition-r.headerPosition}))};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** The type of matches. */
|
|
2
|
+
export type MatchType = 'locale' | 'languageSpecificLocale' | 'language' | 'relatedLocale' | 'defaultLocale';
|
|
3
|
+
/** The type of matches enumeration. */
|
|
4
|
+
export declare const MATCH_TYPES: {
|
|
5
|
+
readonly [K in MatchType]: K;
|
|
6
|
+
};
|
|
7
|
+
/** Type to normalize the locale format. */
|
|
8
|
+
export type NormalizeLocale<Remainder extends string> = Remainder extends `${infer LanguageCode}-${infer CountryCode}` ? `${Lowercase<LanguageCode>}-${Uppercase<CountryCode>}` : Remainder;
|
|
9
|
+
/** Additional options to apply. */
|
|
10
|
+
type Options<WithMatchType extends boolean | undefined> = {
|
|
11
|
+
/** Should the match type be returned? */
|
|
12
|
+
returnMatchType?: WithMatchType;
|
|
13
|
+
};
|
|
14
|
+
type Result<Locales extends string[], WithMatchType extends boolean | undefined> = WithMatchType extends true ? {
|
|
15
|
+
/** The best locale match. */
|
|
16
|
+
match: NormalizeLocale<Locales[number]>;
|
|
17
|
+
/** The type of match. */
|
|
18
|
+
matchType: MatchType;
|
|
19
|
+
} : NormalizeLocale<Locales[number]>;
|
|
20
|
+
/**
|
|
21
|
+
* Resolve the preferred locale from an HTTP `Accept-Language` header.
|
|
22
|
+
*
|
|
23
|
+
* All locale identifiers provided as parameters must following the BCP 47 `language`-`country` (case insensitive).
|
|
24
|
+
*
|
|
25
|
+
* @param acceptLanguageHeader - The value of an HTTP request `Accept-Language` header (also known as a "language priority list").
|
|
26
|
+
* @param locales - An array of locale identifiers that must include the default locale. The order will be used for matching where
|
|
27
|
+
* the first identifier will be more likely to be matched than the last identifier.
|
|
28
|
+
* @param defaultLocale - The default locale identifier when no match is found.
|
|
29
|
+
* @param options - Additional options to apply.
|
|
30
|
+
*
|
|
31
|
+
* @returns Either the best locale match or a match object, depending on options.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* // returns 'fr-CA'
|
|
35
|
+
* resolveAcceptLanguage(
|
|
36
|
+
* 'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001',
|
|
37
|
+
* ['en-US', 'fr-CA'],
|
|
38
|
+
* 'en-US'
|
|
39
|
+
* )
|
|
40
|
+
*/
|
|
41
|
+
export declare const resolveAcceptLanguage: <Locales extends string[], WithMatchType extends boolean | undefined = undefined>(acceptLanguageHeader: string, locales: Locales, defaultLocale: Locales[number], options?: Options<WithMatchType> | undefined) => Result<Locales, WithMatchType>;
|
|
42
|
+
export {};
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var __read=this&&this.__read||function(e,a){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var t,o,l=r.call(e),n=[];try{for(;(void 0===a||a-- >0)&&!(t=l.next()).done;)n.push(t.value)}catch(e){o={error:e}}finally{try{t&&!t.done&&(r=l.return)&&r.call(l)}finally{if(o)throw o.error}}return n},__spreadArray=this&&this.__spreadArray||function(e,a,r){if(r||2===arguments.length)for(var t,o=0,l=a.length;o<l;o++)!t&&o in a||(t||(t=Array.prototype.slice.call(a,0,o)),t[o]=a[o]);return e.concat(t||Array.prototype.slice.call(a))},__values=this&&this.__values||function(e){var a="function"==typeof Symbol&&Symbol.iterator,r=a&&e[a],t=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&t>=e.length&&(e=void 0),{value:e&&e[t++],done:!e}}};throw new TypeError(a?"Object is not iterable.":"Symbol.iterator is not defined.")};import{getDirectives}from"./directives.js";import{Locale,LocaleList,isLocale}from"./locales.js";export var MATCH_TYPES={locale:"locale",languageSpecificLocale:"languageSpecificLocale",language:"language",relatedLocale:"relatedLocale",defaultLocale:"defaultLocale"};export var resolveAcceptLanguage=function(e,a,r,t){if(a.forEach((function(e){if(!isLocale(e,!1))throw new Error("Invalid locale identifier '".concat(e,"'. A valid locale should follow the BCP 47 'language-country' format."))})),!isLocale(r,!1))throw new Error("Invalid default locale identifier '".concat(r,"'. A valid locale should follow the BCP 47 'language-country' format."));if(!a.some((function(e){return e.toLowerCase()===r.toLowerCase()})))throw new Error("The default locale '".concat(r,"' must be included in the locales array because it is used as a fallback when no match is found."));var o=new Locale(r).identifier,l=new Set(__spreadArray([o],__read(a.map((function(e){return new Locale(e).identifier}))),!1)),n=function(){var a,r,t,n,i=new LocaleList(l),c=getDirectives(e),u=function(e){var a=e.locale,r=e.languageCode;if(!i.languages.has(r))return"continue";if(void 0!==a)return i.locales.has(a)?{value:{match:a,matchType:MATCH_TYPES.locale}}:"continue";var t=c.find((function(e){return e.languageCode===r&&void 0!==e.locale&&i.locales.has(e.locale)}));if(t)return{value:{match:t.locale,matchType:MATCH_TYPES.languageSpecificLocale}};var o=i.objects.find((function(e){return e.languageCode===r}));return o?{value:{match:o.identifier,matchType:MATCH_TYPES.language}}:void 0};try{for(var f=__values(c),d=f.next();!d.done;d=f.next()){var s=u(d.value);if("object"==typeof s)return s.value}}catch(e){a={error:e}}finally{try{d&&!d.done&&(r=f.return)&&r.call(f)}finally{if(a)throw a.error}}var v=function(e){if(!i.languages.has(e.languageCode))return"continue";var a=i.objects.find((function(a){return a.languageCode===e.languageCode}));return a?{value:{match:a.identifier,matchType:MATCH_TYPES.relatedLocale}}:void 0};try{for(var h=__values(c),g=h.next();!g.done;g=h.next()){var y=v(g.value);if("object"==typeof y)return y.value}}catch(e){t={error:e}}finally{try{g&&!g.done&&(n=h.return)&&n.call(h)}finally{if(t)throw t.error}}return{match:o,matchType:MATCH_TYPES.defaultLocale}}();return(null==t?void 0:t.returnMatchType)?n:n.match};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** Class to manage a locale identifier using the BCP 47 `language`-`country` format. */
|
|
2
|
+
export declare class Locale {
|
|
3
|
+
/** The ISO 3166-1 alpha-2 country code. */
|
|
4
|
+
readonly countryCode: string;
|
|
5
|
+
/** The locale identifier using the BCP 47 `language`-`country` case-normalized format. */
|
|
6
|
+
readonly identifier: string;
|
|
7
|
+
/** The ISO 639-1 alpha-2 language code. */
|
|
8
|
+
readonly languageCode: string;
|
|
9
|
+
/**
|
|
10
|
+
* Create a new `Locale` object.
|
|
11
|
+
*
|
|
12
|
+
* @param identifier - A locale identifier using the BCP 47 `language`-`country` format (case insensitive).
|
|
13
|
+
*
|
|
14
|
+
* @throws An error if the `identifier` format is invalid.
|
|
15
|
+
*/
|
|
16
|
+
constructor(identifier: string);
|
|
17
|
+
}
|
|
18
|
+
export declare class LocaleList {
|
|
19
|
+
/** A set of ISO 3166-1 alpha-2 country codes. */
|
|
20
|
+
readonly countries: Set<string>;
|
|
21
|
+
/** A set of ISO 639-1 alpha-2 language codes. */
|
|
22
|
+
readonly languages: Set<string>;
|
|
23
|
+
/** A set of locale identifiers using the BCP 47 `language`-`country` case-normalized format. */
|
|
24
|
+
readonly locales: Set<string>;
|
|
25
|
+
/** A list of locale objects. */
|
|
26
|
+
readonly objects: Locale[];
|
|
27
|
+
/**
|
|
28
|
+
* Create a list of locale identifiers.
|
|
29
|
+
*
|
|
30
|
+
* @param locales - An set of unique locale identifiers using the BCP 47 `language`-`country` format (case insensitive).
|
|
31
|
+
*
|
|
32
|
+
* @throws Will throw an error if one of the locale's format is invalid.
|
|
33
|
+
*/
|
|
34
|
+
constructor(locales: Set<string>);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Is a given string a locale identifier following the BCP 47 `language`-`country` format.
|
|
38
|
+
*
|
|
39
|
+
* @param identifier - A potential locale identify to verify.
|
|
40
|
+
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
|
|
41
|
+
*/
|
|
42
|
+
export declare const isLocale: (identifier: string, caseNormalized?: boolean) => boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var __read=this&&this.__read||function(e,t){var o="function"==typeof Symbol&&e[Symbol.iterator];if(!o)return e;var a,r,i=o.call(e),n=[];try{for(;(void 0===t||t-- >0)&&!(a=i.next()).done;)n.push(a.value)}catch(e){r={error:e}}finally{try{a&&!a.done&&(o=i.return)&&o.call(i)}finally{if(r)throw r.error}}return n},Locale=function(e){if(!isLocale(e,!1))throw new Error("invalid locale identifier '".concat(e,"'"));var t=__read(e.split("-"),2),o=t[0],a=t[1];this.languageCode=o.toLowerCase(),this.countryCode=a.toUpperCase(),this.identifier="".concat(this.languageCode,"-").concat(this.countryCode)};export{Locale};var LocaleList=function(e){var t=this;this.countries=new Set,this.languages=new Set,this.locales=new Set,this.objects=[],e.forEach((function(e){var o=new Locale(e);t.objects.push(o),t.locales.add(o.identifier),t.languages.add(o.languageCode),t.countries.add(o.countryCode)}))};export{LocaleList};export var isLocale=function(e,t){return void 0===t&&(t=!0),new RegExp(/^[a-z]{2}-[A-Z]{2}$/,t?void 0:"i").test(e)};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "resolve-accept-language",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Resolve the preferred locale based on the value of an `Accept-Language` HTTP header.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"accept-language",
|
|
@@ -26,35 +26,37 @@
|
|
|
26
26
|
"exports": {
|
|
27
27
|
".": {
|
|
28
28
|
"import": {
|
|
29
|
-
"types": "./lib/esm/
|
|
30
|
-
"default": "./lib/esm/
|
|
29
|
+
"types": "./lib/esm/index.d.ts",
|
|
30
|
+
"default": "./lib/esm/index.js"
|
|
31
31
|
},
|
|
32
32
|
"require": {
|
|
33
|
-
"types": "./lib/cjs/
|
|
34
|
-
"default": "./lib/cjs/
|
|
33
|
+
"types": "./lib/cjs/index.d.ts",
|
|
34
|
+
"default": "./lib/cjs/index.js"
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
|
-
"main": "lib/cjs/
|
|
39
|
-
"module": "lib/esm/
|
|
40
|
-
"types": "lib/esm/
|
|
38
|
+
"main": "lib/cjs/index.js",
|
|
39
|
+
"module": "lib/esm/index.js",
|
|
40
|
+
"types": "lib/esm/index.d.ts",
|
|
41
41
|
"files": [
|
|
42
42
|
"lib"
|
|
43
43
|
],
|
|
44
44
|
"scripts": {
|
|
45
|
-
"build": "npm run prettier && npm run lint-fix && rm -Rf ./lib && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json && npm run test",
|
|
46
|
-
"
|
|
45
|
+
"build": "npm run prettier && npm run lint-fix && rm -Rf ./lib && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json && tsc -p tsconfig.build-scripts.json && node lib/build-scripts/build.js && npm run test",
|
|
46
|
+
"depcheck": "depcheck",
|
|
47
47
|
"lint-fix": "eslint --fix .",
|
|
48
|
-
"lint-print-config": "eslint --print-config ./eslintrc.yaml",
|
|
49
48
|
"prettier": "prettier --write .",
|
|
50
49
|
"release": "dotenv -- release-it --only-version",
|
|
51
50
|
"test": "jest --coverage"
|
|
52
51
|
},
|
|
53
52
|
"devDependencies": {
|
|
53
|
+
"@eslint/js": "^8.57.0",
|
|
54
54
|
"@release-it/conventional-changelog": "8.0.1",
|
|
55
55
|
"@types/jest": "29.5.12",
|
|
56
|
-
"@
|
|
57
|
-
"@typescript-eslint/
|
|
56
|
+
"@types/node": "^20.11.24",
|
|
57
|
+
"@typescript-eslint/eslint-plugin": "7.1.1",
|
|
58
|
+
"@typescript-eslint/parser": "7.1.1",
|
|
59
|
+
"depcheck": "^1.4.7",
|
|
58
60
|
"dotenv-cli": "7.3.0",
|
|
59
61
|
"eslint": "8.57.0",
|
|
60
62
|
"eslint-config-prettier": "9.1.0",
|
|
@@ -64,7 +66,7 @@
|
|
|
64
66
|
"eslint-plugin-jest": "27.9.0",
|
|
65
67
|
"eslint-plugin-jsdoc": "48.2.0",
|
|
66
68
|
"eslint-plugin-json-files": "4.1.0",
|
|
67
|
-
"eslint-plugin-prefer-arrow-functions": "3.3.
|
|
69
|
+
"eslint-plugin-prefer-arrow-functions": "3.3.2",
|
|
68
70
|
"eslint-plugin-prettier": "5.1.3",
|
|
69
71
|
"eslint-plugin-tsdoc": "0.2.17",
|
|
70
72
|
"eslint-plugin-unicorn": "51.0.1",
|
|
@@ -75,8 +77,8 @@
|
|
|
75
77
|
"prettier-plugin-organize-imports": "3.2.4",
|
|
76
78
|
"prettier-plugin-sh": "0.14.0",
|
|
77
79
|
"release-it": "17.1.1",
|
|
80
|
+
"terser": "^5.28.1",
|
|
78
81
|
"ts-jest": "29.1.2",
|
|
79
|
-
"ts-node": "10.9.2",
|
|
80
82
|
"typescript": "5.3.3"
|
|
81
83
|
},
|
|
82
84
|
"engines": {
|
package/lib/cjs/locale-list.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import Locale from './locale';
|
|
2
|
-
export default class LocaleList<TLocales extends readonly string[]> {
|
|
3
|
-
/** A set of ISO 3166-1 alpha-2 country codes. */
|
|
4
|
-
readonly countries: Set<string>;
|
|
5
|
-
/** A set of ISO 639-1 alpha-2 language codes. */
|
|
6
|
-
readonly languages: Set<string>;
|
|
7
|
-
/** A set of locale identifiers using the BCP 47 `language`-`country` case-normalized format. */
|
|
8
|
-
readonly locales: Set<string>;
|
|
9
|
-
/** A list of locale objects. */
|
|
10
|
-
readonly objects: Locale[];
|
|
11
|
-
/**
|
|
12
|
-
* Create a list of locale identifiers.
|
|
13
|
-
*
|
|
14
|
-
* @param locales - An array of locale identifiers using the BCP 47 `language`-`country` format.
|
|
15
|
-
*
|
|
16
|
-
* @throws Will throw an error if one of the locale's format is invalid.
|
|
17
|
-
*/
|
|
18
|
-
constructor(locales: TLocales extends string[] ? TLocales[number][] : TLocales);
|
|
19
|
-
}
|
package/lib/cjs/locale-list.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var locale_1 = require("./locale");
|
|
4
|
-
var LocaleList = /** @class */ (function () {
|
|
5
|
-
/**
|
|
6
|
-
* Create a list of locale identifiers.
|
|
7
|
-
*
|
|
8
|
-
* @param locales - An array of locale identifiers using the BCP 47 `language`-`country` format.
|
|
9
|
-
*
|
|
10
|
-
* @throws Will throw an error if one of the locale's format is invalid.
|
|
11
|
-
*/
|
|
12
|
-
function LocaleList(locales) {
|
|
13
|
-
var _this = this;
|
|
14
|
-
/** A set of ISO 3166-1 alpha-2 country codes. */
|
|
15
|
-
this.countries = new Set();
|
|
16
|
-
/** A set of ISO 639-1 alpha-2 language codes. */
|
|
17
|
-
this.languages = new Set();
|
|
18
|
-
/** A set of locale identifiers using the BCP 47 `language`-`country` case-normalized format. */
|
|
19
|
-
this.locales = new Set();
|
|
20
|
-
/** A list of locale objects. */
|
|
21
|
-
this.objects = [];
|
|
22
|
-
locales.forEach(function (locale) {
|
|
23
|
-
var localeObject = new locale_1.default(locale);
|
|
24
|
-
if (!_this.locales.has(localeObject.identifier)) {
|
|
25
|
-
_this.objects.push(localeObject);
|
|
26
|
-
_this.locales.add(localeObject.identifier);
|
|
27
|
-
_this.languages.add(localeObject.languageCode);
|
|
28
|
-
_this.countries.add(localeObject.countryCode);
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
return LocaleList;
|
|
33
|
-
}());
|
|
34
|
-
exports.default = LocaleList;
|
package/lib/cjs/locale.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { NormalizeLocale } from './resolve-accept-language';
|
|
2
|
-
/** Class to manage a locale identifier using the BCP 47 `language`-`country` format. */
|
|
3
|
-
export default class Locale<TLocale extends string = string> {
|
|
4
|
-
/** The ISO 3166-1 alpha-2 country code. */
|
|
5
|
-
readonly countryCode: string;
|
|
6
|
-
/** The locale identifier using the BCP 47 `language`-`country` case-normalized format. */
|
|
7
|
-
readonly identifier: NormalizeLocale<TLocale>;
|
|
8
|
-
/** The ISO 639-1 alpha-2 language code. */
|
|
9
|
-
readonly languageCode: string;
|
|
10
|
-
/**
|
|
11
|
-
* Create a new `Locale` object.
|
|
12
|
-
*
|
|
13
|
-
* @param identifier - A locale identifier using the BCP 47 `language`-`country` format (case insensitive).
|
|
14
|
-
*
|
|
15
|
-
* @throws An error if the `identifier` format is invalid.
|
|
16
|
-
*/
|
|
17
|
-
constructor(identifier: string);
|
|
18
|
-
/**
|
|
19
|
-
* Is a given string an ISO 3166-1 alpha-2 country code.
|
|
20
|
-
*
|
|
21
|
-
* @param countryCode - An ISO 3166-1 alpha-2 country code.
|
|
22
|
-
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
|
|
23
|
-
*/
|
|
24
|
-
static isCountryCode(countryCode: string, caseNormalized?: boolean): boolean;
|
|
25
|
-
/**
|
|
26
|
-
* Is a given string an ISO 639-1 alpha-2 language code.
|
|
27
|
-
*
|
|
28
|
-
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
29
|
-
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
|
|
30
|
-
*/
|
|
31
|
-
static isLanguageCode(languageCode: string, caseNormalized?: boolean): boolean;
|
|
32
|
-
/**
|
|
33
|
-
* Is a given string a locale identifier following the BCP 47 `language`-`country` format.
|
|
34
|
-
*
|
|
35
|
-
* @param identifier - A potential locale identify to verify.
|
|
36
|
-
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
|
|
37
|
-
*/
|
|
38
|
-
static isLocale(identifier: string, caseNormalized?: boolean): boolean;
|
|
39
|
-
}
|
package/lib/cjs/locale.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
/** Class to manage a locale identifier using the BCP 47 `language`-`country` format. */
|
|
4
|
-
var Locale = /** @class */ (function () {
|
|
5
|
-
/**
|
|
6
|
-
* Create a new `Locale` object.
|
|
7
|
-
*
|
|
8
|
-
* @param identifier - A locale identifier using the BCP 47 `language`-`country` format (case insensitive).
|
|
9
|
-
*
|
|
10
|
-
* @throws An error if the `identifier` format is invalid.
|
|
11
|
-
*/
|
|
12
|
-
function Locale(identifier) {
|
|
13
|
-
if (!Locale.isLocale(identifier, false)) {
|
|
14
|
-
throw new Error("invalid locale identifier '".concat(identifier, "'"));
|
|
15
|
-
}
|
|
16
|
-
var _a = identifier.split('-'), languageCode = _a[0], countryCode = _a[1];
|
|
17
|
-
this.languageCode = languageCode.toLowerCase();
|
|
18
|
-
this.countryCode = countryCode.toUpperCase();
|
|
19
|
-
this.identifier = "".concat(this.languageCode, "-").concat(this.countryCode);
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Is a given string an ISO 3166-1 alpha-2 country code.
|
|
23
|
-
*
|
|
24
|
-
* @param countryCode - An ISO 3166-1 alpha-2 country code.
|
|
25
|
-
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
|
|
26
|
-
*/
|
|
27
|
-
Locale.isCountryCode = function (countryCode, caseNormalized) {
|
|
28
|
-
if (caseNormalized === void 0) { caseNormalized = true; }
|
|
29
|
-
var regExp = new RegExp(/^[A-Z]{2}$/, caseNormalized ? undefined : 'i');
|
|
30
|
-
return regExp.test(countryCode);
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Is a given string an ISO 639-1 alpha-2 language code.
|
|
34
|
-
*
|
|
35
|
-
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
36
|
-
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
|
|
37
|
-
*/
|
|
38
|
-
Locale.isLanguageCode = function (languageCode, caseNormalized) {
|
|
39
|
-
if (caseNormalized === void 0) { caseNormalized = true; }
|
|
40
|
-
var regExp = new RegExp(/^[a-z]{2}$/, caseNormalized ? undefined : 'i');
|
|
41
|
-
return regExp.test(languageCode);
|
|
42
|
-
};
|
|
43
|
-
/**
|
|
44
|
-
* Is a given string a locale identifier following the BCP 47 `language`-`country` format.
|
|
45
|
-
*
|
|
46
|
-
* @param identifier - A potential locale identify to verify.
|
|
47
|
-
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
|
|
48
|
-
*/
|
|
49
|
-
Locale.isLocale = function (identifier, caseNormalized) {
|
|
50
|
-
if (caseNormalized === void 0) { caseNormalized = true; }
|
|
51
|
-
var regExp = new RegExp(/^[a-z]{2}-[A-Z]{2}$/, caseNormalized ? undefined : 'i');
|
|
52
|
-
return regExp.test(identifier);
|
|
53
|
-
};
|
|
54
|
-
return Locale;
|
|
55
|
-
}());
|
|
56
|
-
exports.default = Locale;
|