resolve-accept-language 3.1.4 → 3.1.6
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/LICENSE +21 -21
- package/README.md +141 -141
- package/lib/cjs/directives.js +1 -1
- package/lib/esm/directives.js +1 -1
- package/package.json +87 -87
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2021 Avansai
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Avansai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,141 +1,141 @@
|
|
|
1
|
-
# resolve-accept-language
|
|
2
|
-
|
|
3
|
-
[](https://opensource.org/licenses/MIT)
|
|
4
|
-
[](https://www.npmjs.com/package/resolve-accept-language)
|
|
5
|
-

|
|
6
|
-

|
|
7
|
-
|
|
8
|
-
Resolve the best locale based on the value of an `Accept-Language` HTTP header.
|
|
9
|
-
|
|
10
|
-
## Usage
|
|
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
|
-
|
|
14
|
-
Add the package as a dependency:
|
|
15
|
-
|
|
16
|
-
```
|
|
17
|
-
npm install resolve-accept-language
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
Code example:
|
|
21
|
-
|
|
22
|
-
```ts
|
|
23
|
-
import { resolveAcceptLanguage } from 'resolve-accept-language'
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The API is well documented from within your IDE using TSDoc. The arguments are as follows:
|
|
27
|
-
*
|
|
28
|
-
* 1) The HTTP accept-language header.
|
|
29
|
-
* 2) The available locales (they must contain the default locale).
|
|
30
|
-
* 3) The default locale.
|
|
31
|
-
*/
|
|
32
|
-
console.log(
|
|
33
|
-
resolveAcceptLanguage(
|
|
34
|
-
'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001',
|
|
35
|
-
// The `as const` is optional for TypeScript but gives better typing.
|
|
36
|
-
['en-US', 'fr-CA'] as const,
|
|
37
|
-
'en-US'
|
|
38
|
-
)
|
|
39
|
-
)
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
Output:
|
|
43
|
-
|
|
44
|
-
```
|
|
45
|
-
fr-CA
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
## Advanced use cases
|
|
49
|
-
|
|
50
|
-
### Match types
|
|
51
|
-
|
|
52
|
-
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:
|
|
53
|
-
|
|
54
|
-
```ts
|
|
55
|
-
import { MATCH_TYPES, resolveAcceptLanguage } from 'resolve-accept-language'
|
|
56
|
-
|
|
57
|
-
const { match, matchType } = resolveAcceptLanguage(
|
|
58
|
-
'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001' as const,
|
|
59
|
-
['en-US', 'fr-CA'],
|
|
60
|
-
'fr-CA',
|
|
61
|
-
{ returnMatchType: true }
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
console.log(`A locale was matched: ${match}`)
|
|
65
|
-
|
|
66
|
-
if (matchType === MATCH_TYPES.locale) {
|
|
67
|
-
console.log('The match is locale-based')
|
|
68
|
-
} else if (matchType === MATCH_TYPES.languageSpecificLocale) {
|
|
69
|
-
console.log('The match is language specific locale-based')
|
|
70
|
-
} else if (matchType === MATCH_TYPES.language) {
|
|
71
|
-
console.log('The match is language-based')
|
|
72
|
-
} else if (matchType === MATCH_TYPES.relatedLocale) {
|
|
73
|
-
console.log('The match is related-locale-based')
|
|
74
|
-
} else if (matchType === MATCH_TYPES.languageCountry) {
|
|
75
|
-
console.log('The match is language country-based')
|
|
76
|
-
} else if (matchType === MATCH_TYPES.defaultLocale) {
|
|
77
|
-
console.log('The match is the default locale')
|
|
78
|
-
}
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
### Country match
|
|
82
|
-
|
|
83
|
-
There may be cases where it is preferred to perform a "country match" before falling back to the default locale match. For example:
|
|
84
|
-
|
|
85
|
-
```ts
|
|
86
|
-
console.log(
|
|
87
|
-
resolveAcceptLanguage('af-ZA', ['en-US', 'zu-ZA'] as const, 'en-US', {
|
|
88
|
-
returnMatchType: true,
|
|
89
|
-
matchCountry: true,
|
|
90
|
-
})
|
|
91
|
-
)
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
Output:
|
|
95
|
-
|
|
96
|
-
```json
|
|
97
|
-
{ "match": "zu-ZA", "matchType": "country" }
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
In this case, the header prefers `af-ZA`, which shares the same country as `zu-ZA`. Instead of falling back to the default `en-US`, `zu-ZA` is matched.
|
|
101
|
-
|
|
102
|
-
This behavior is not set by default because, in most cases, the quality of the default locale is better than the translations. Performing a country match could potentially lower the quality of the selection. However, there may be cases where this is not true, which is why the `matchCountry` option exists.
|
|
103
|
-
|
|
104
|
-
## How does the resolver work?
|
|
105
|
-
|
|
106
|
-
As per RFC 4647, this package uses the "lookup" matching scheme. This means that it will always produce exactly one match for a given request.
|
|
107
|
-
|
|
108
|
-
The matching strategy follows these steps, in order:
|
|
109
|
-
|
|
110
|
-
1. Start with the default locale, as it's considered the highest quality content available.
|
|
111
|
-
2. Extract all locales and languages from the HTTP header, sorted by quality factor and position in the header. Each of these elements is referred to as a "directive".
|
|
112
|
-
3. Perform matching in several stages:
|
|
113
|
-
1. **Locale Match**: Look for an exact locale match in the directives.
|
|
114
|
-
2. **Language-Specific Locale Match**: If no locale match is found, look for a locale that matches the language of the directive.
|
|
115
|
-
3. **Language Match**: If no language-specific locale match is found, look for a locale that matches the language.
|
|
116
|
-
4. **Related-Locale Match**: If no language match is found, look for a locale that matches the language in the next round of directives.
|
|
117
|
-
5. **Language Country Match**: If no related-locale match is found, look for a locale that matches the default locale's language but in a country from a locale specified in a directive.
|
|
118
|
-
6. **Country Match**: If the option is enabled and no language country match is found, look for a locale that matches the country in the next round of directives.
|
|
119
|
-
7. **Default Locale Match**: If all else fails, fall back to the default locale.
|
|
120
|
-
|
|
121
|
-
Each stage only happens if the previous stage didn't find a match. This ensures the best possible match is found according to the given criteria.
|
|
122
|
-
|
|
123
|
-
## Why another `Accept-Language` package?
|
|
124
|
-
|
|
125
|
-
The `Accept-Language` header has been around since 1999. Like many other standards that deal with languages, the header is based
|
|
126
|
-
on BCP 47 language tags. Language tags can be as simple as `fr` (non-country specific French) or more complex, for example,
|
|
127
|
-
`sr-Latn-RS` would represent Latin script Serbian.
|
|
128
|
-
|
|
129
|
-
One of the main challenges is that BCP 47 language tags can be either overly simple or too complex. This is one of the problems this
|
|
130
|
-
library will try to address by focusing on locales identifiers using the `language`-`country` format instead of trying to provide
|
|
131
|
-
full BCP 47 language tags support. The main reasons for this:
|
|
132
|
-
|
|
133
|
-
- Using 2 letter language codes is rarely sufficient. Without being explicit about the targeted country for a given language, it is impossible to provide the right format for some content such as dates and numbers. Also, while languages are similar across countries, there are different ways to say the same thing. Our hypothesis is that by better targeting the audience, the user experience will improve.
|
|
134
|
-
- About 99% of all cases can be covered using the `language`-`country` format. We could possibly extend script support in the future given a valid use case, but in the meantime, our goal is to keep this library as simple as possible, while providing the best matches.
|
|
135
|
-
|
|
136
|
-
## Additional references
|
|
137
|
-
|
|
138
|
-
- Matching of Language Tags ([RFC 4647](https://tools.ietf.org/html/rfc4647))
|
|
139
|
-
- Tags for Identifying Languages ([RFC 4646](https://tools.ietf.org/html/rfc4646))
|
|
140
|
-
- The Accept-Language request-header field ([RFC 2616 section 14.4](https://tools.ietf.org/html/rfc2616#section-14.4))
|
|
141
|
-
- Quality values ([RFC 2616 section 3.9](https://tools.ietf.org/html/rfc2616#section-3.9))
|
|
1
|
+
# resolve-accept-language
|
|
2
|
+
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
[](https://www.npmjs.com/package/resolve-accept-language)
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
Resolve the best locale based on the value of an `Accept-Language` HTTP header.
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
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
|
+
|
|
14
|
+
Add the package as a dependency:
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
npm install resolve-accept-language
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Code example:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { resolveAcceptLanguage } from 'resolve-accept-language'
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The API is well documented from within your IDE using TSDoc. The arguments are as follows:
|
|
27
|
+
*
|
|
28
|
+
* 1) The HTTP accept-language header.
|
|
29
|
+
* 2) The available locales (they must contain the default locale).
|
|
30
|
+
* 3) The default locale.
|
|
31
|
+
*/
|
|
32
|
+
console.log(
|
|
33
|
+
resolveAcceptLanguage(
|
|
34
|
+
'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001',
|
|
35
|
+
// The `as const` is optional for TypeScript but gives better typing.
|
|
36
|
+
['en-US', 'fr-CA'] as const,
|
|
37
|
+
'en-US'
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Output:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
fr-CA
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Advanced use cases
|
|
49
|
+
|
|
50
|
+
### Match types
|
|
51
|
+
|
|
52
|
+
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:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { MATCH_TYPES, resolveAcceptLanguage } from 'resolve-accept-language'
|
|
56
|
+
|
|
57
|
+
const { match, matchType } = resolveAcceptLanguage(
|
|
58
|
+
'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001' as const,
|
|
59
|
+
['en-US', 'fr-CA'],
|
|
60
|
+
'fr-CA',
|
|
61
|
+
{ returnMatchType: true }
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
console.log(`A locale was matched: ${match}`)
|
|
65
|
+
|
|
66
|
+
if (matchType === MATCH_TYPES.locale) {
|
|
67
|
+
console.log('The match is locale-based')
|
|
68
|
+
} else if (matchType === MATCH_TYPES.languageSpecificLocale) {
|
|
69
|
+
console.log('The match is language specific locale-based')
|
|
70
|
+
} else if (matchType === MATCH_TYPES.language) {
|
|
71
|
+
console.log('The match is language-based')
|
|
72
|
+
} else if (matchType === MATCH_TYPES.relatedLocale) {
|
|
73
|
+
console.log('The match is related-locale-based')
|
|
74
|
+
} else if (matchType === MATCH_TYPES.languageCountry) {
|
|
75
|
+
console.log('The match is language country-based')
|
|
76
|
+
} else if (matchType === MATCH_TYPES.defaultLocale) {
|
|
77
|
+
console.log('The match is the default locale')
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Country match
|
|
82
|
+
|
|
83
|
+
There may be cases where it is preferred to perform a "country match" before falling back to the default locale match. For example:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
console.log(
|
|
87
|
+
resolveAcceptLanguage('af-ZA', ['en-US', 'zu-ZA'] as const, 'en-US', {
|
|
88
|
+
returnMatchType: true,
|
|
89
|
+
matchCountry: true,
|
|
90
|
+
})
|
|
91
|
+
)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Output:
|
|
95
|
+
|
|
96
|
+
```json
|
|
97
|
+
{ "match": "zu-ZA", "matchType": "country" }
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
In this case, the header prefers `af-ZA`, which shares the same country as `zu-ZA`. Instead of falling back to the default `en-US`, `zu-ZA` is matched.
|
|
101
|
+
|
|
102
|
+
This behavior is not set by default because, in most cases, the quality of the default locale is better than the translations. Performing a country match could potentially lower the quality of the selection. However, there may be cases where this is not true, which is why the `matchCountry` option exists.
|
|
103
|
+
|
|
104
|
+
## How does the resolver work?
|
|
105
|
+
|
|
106
|
+
As per RFC 4647, this package uses the "lookup" matching scheme. This means that it will always produce exactly one match for a given request.
|
|
107
|
+
|
|
108
|
+
The matching strategy follows these steps, in order:
|
|
109
|
+
|
|
110
|
+
1. Start with the default locale, as it's considered the highest quality content available.
|
|
111
|
+
2. Extract all locales and languages from the HTTP header, sorted by quality factor and position in the header. Each of these elements is referred to as a "directive".
|
|
112
|
+
3. Perform matching in several stages:
|
|
113
|
+
1. **Locale Match**: Look for an exact locale match in the directives.
|
|
114
|
+
2. **Language-Specific Locale Match**: If no locale match is found, look for a locale that matches the language of the directive.
|
|
115
|
+
3. **Language Match**: If no language-specific locale match is found, look for a locale that matches the language.
|
|
116
|
+
4. **Related-Locale Match**: If no language match is found, look for a locale that matches the language in the next round of directives.
|
|
117
|
+
5. **Language Country Match**: If no related-locale match is found, look for a locale that matches the default locale's language but in a country from a locale specified in a directive.
|
|
118
|
+
6. **Country Match**: If the option is enabled and no language country match is found, look for a locale that matches the country in the next round of directives.
|
|
119
|
+
7. **Default Locale Match**: If all else fails, fall back to the default locale.
|
|
120
|
+
|
|
121
|
+
Each stage only happens if the previous stage didn't find a match. This ensures the best possible match is found according to the given criteria.
|
|
122
|
+
|
|
123
|
+
## Why another `Accept-Language` package?
|
|
124
|
+
|
|
125
|
+
The `Accept-Language` header has been around since 1999. Like many other standards that deal with languages, the header is based
|
|
126
|
+
on BCP 47 language tags. Language tags can be as simple as `fr` (non-country specific French) or more complex, for example,
|
|
127
|
+
`sr-Latn-RS` would represent Latin script Serbian.
|
|
128
|
+
|
|
129
|
+
One of the main challenges is that BCP 47 language tags can be either overly simple or too complex. This is one of the problems this
|
|
130
|
+
library will try to address by focusing on locales identifiers using the `language`-`country` format instead of trying to provide
|
|
131
|
+
full BCP 47 language tags support. The main reasons for this:
|
|
132
|
+
|
|
133
|
+
- Using 2 letter language codes is rarely sufficient. Without being explicit about the targeted country for a given language, it is impossible to provide the right format for some content such as dates and numbers. Also, while languages are similar across countries, there are different ways to say the same thing. Our hypothesis is that by better targeting the audience, the user experience will improve.
|
|
134
|
+
- About 99% of all cases can be covered using the `language`-`country` format. We could possibly extend script support in the future given a valid use case, but in the meantime, our goal is to keep this library as simple as possible, while providing the best matches.
|
|
135
|
+
|
|
136
|
+
## Additional references
|
|
137
|
+
|
|
138
|
+
- Matching of Language Tags ([RFC 4647](https://tools.ietf.org/html/rfc4647))
|
|
139
|
+
- Tags for Identifying Languages ([RFC 4646](https://tools.ietf.org/html/rfc4646))
|
|
140
|
+
- The Accept-Language request-header field ([RFC 2616 section 14.4](https://tools.ietf.org/html/rfc2616#section-14.4))
|
|
141
|
+
- Quality values ([RFC 2616 section 3.9](https://tools.ietf.org/html/rfc2616#section-3.9))
|
package/lib/cjs/directives.js
CHANGED
|
@@ -1 +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,
|
|
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,n=t.call(e),o=[];try{for(;(void 0===r||r-- >0)&&!(a=n.next()).done;)o.push(a.value)}catch(e){i={error:e}}finally{try{a&&!a.done&&(t=n.return)&&t.call(n)}finally{if(i)throw i.error}}return o},__spreadArray=this&&this.__spreadArray||function(e,r,t){if(t||2===arguments.length)for(var a,i=0,n=r.length;i<n;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(/^(([a-z]{2})(-([a-z]{2}|419))?)(;q=((1(\.0{0,3})?)|(0(\.\d{0,3})?)))?$/i);if(r){var t=r[2],a=r[4],i=r[6],n=t.toLowerCase(),o=a?a.toUpperCase():void 0;if("419"!==o||"es"===n){var s=void 0===i?1:Number.parseFloat(i);return{languageCode:n,countryCode:o,locale:o?"".concat(n,"-").concat(o):void 0,quality:s}}}},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],n=["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(n),!1))}return r.sort((function(e,r){var t=r.quality-e.quality;return t||e.headerPosition-r.headerPosition}))};exports.getDirectives=getDirectives;
|
package/lib/esm/directives.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var __assign=this&&this.__assign||function(){return __assign=Object.assign||function(
|
|
1
|
+
var __assign=this&&this.__assign||function(){return __assign=Object.assign||function(r){for(var e,a=1,t=arguments.length;a<t;a++)for(var n in e=arguments[a])Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n]);return r},__assign.apply(this,arguments)},__read=this&&this.__read||function(r,e){var a="function"==typeof Symbol&&r[Symbol.iterator];if(!a)return r;var t,n,i=a.call(r),o=[];try{for(;(void 0===e||e-- >0)&&!(t=i.next()).done;)o.push(t.value)}catch(r){n={error:r}}finally{try{t&&!t.done&&(a=i.return)&&a.call(i)}finally{if(n)throw n.error}}return o},__spreadArray=this&&this.__spreadArray||function(r,e,a){if(a||2===arguments.length)for(var t,n=0,i=e.length;n<i;n++)!t&&n in e||(t||(t=Array.prototype.slice.call(e,0,n)),t[n]=e[n]);return r.concat(t||Array.prototype.slice.call(e))},getDirective=function(r){var e=r.match(/^(([a-z]{2})(-([a-z]{2}|419))?)(;q=((1(\.0{0,3})?)|(0(\.\d{0,3})?)))?$/i);if(e){var a=e[2],t=e[4],n=e[6],i=a.toLowerCase(),o=t?t.toUpperCase():void 0;if("419"!==o||"es"===i){var s=void 0===n?1:Number.parseFloat(n);return{languageCode:i,countryCode:o,locale:o?"".concat(i,"-").concat(o):void 0,quality:s}}}};export var getDirectives=function(r){var e=[],a=0;r.split(",").forEach((function(r){var t=getDirective(r.trim());t&&(e.some((function(r){return r.languageCode===t.languageCode&&r.locale===t.locale}))||(e.push(__assign(__assign({},t),{headerPosition:a})),a++))}));var t=e.findIndex((function(r){return"es-419"===r.locale}));if(t>=0){var n=e[t],i=["es-AR","es-CL","es-CO","es-CR","es-HN","es-MX","es-PE","es-US","es-UY","es-VE"].map((function(r){return __assign(__assign({},n),{locale:r})}));e.splice.apply(e,__spreadArray([t,1],__read(i),!1))}return e.sort((function(r,e){var a=e.quality-r.quality;return a||r.headerPosition-e.headerPosition}))};
|
package/package.json
CHANGED
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "resolve-accept-language",
|
|
3
|
-
"version": "3.1.
|
|
4
|
-
"description": "Resolve the preferred locale based on the value of an `Accept-Language` HTTP header.",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"accept-language",
|
|
7
|
-
"RFC 4647",
|
|
8
|
-
"locale",
|
|
9
|
-
"language tags",
|
|
10
|
-
"RFC 4646",
|
|
11
|
-
"BCP 47",
|
|
12
|
-
"RFC 2616",
|
|
13
|
-
"resolve",
|
|
14
|
-
"detect",
|
|
15
|
-
"intl",
|
|
16
|
-
"i18n",
|
|
17
|
-
"internationalization"
|
|
18
|
-
],
|
|
19
|
-
"repository": {
|
|
20
|
-
"type": "git",
|
|
21
|
-
"url": "git+https://github.com/
|
|
22
|
-
},
|
|
23
|
-
"license": "MIT",
|
|
24
|
-
"author": "
|
|
25
|
-
"type": "module",
|
|
26
|
-
"exports": {
|
|
27
|
-
".": {
|
|
28
|
-
"import": {
|
|
29
|
-
"types": "./lib/esm/index.d.ts",
|
|
30
|
-
"default": "./lib/esm/index.js"
|
|
31
|
-
},
|
|
32
|
-
"require": {
|
|
33
|
-
"types": "./lib/cjs/index.d.ts",
|
|
34
|
-
"default": "./lib/cjs/index.js"
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
"main": "lib/cjs/index.js",
|
|
39
|
-
"module": "lib/esm/index.js",
|
|
40
|
-
"types": "lib/esm/index.d.ts",
|
|
41
|
-
"files": [
|
|
42
|
-
"lib"
|
|
43
|
-
],
|
|
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 && tsc -p tsconfig.build-scripts.json && node lib/build-scripts/build.js && npm run test",
|
|
46
|
-
"depcheck": "depcheck",
|
|
47
|
-
"lint-fix": "eslint --fix .",
|
|
48
|
-
"prettier": "prettier --write .",
|
|
49
|
-
"release": "dotenv -- release-it --only-version",
|
|
50
|
-
"test": "jest --coverage"
|
|
51
|
-
},
|
|
52
|
-
"devDependencies": {
|
|
53
|
-
"@eslint/js": "^
|
|
54
|
-
"@release-it/conventional-changelog": "8.0.1",
|
|
55
|
-
"@types/jest": "29.5.12",
|
|
56
|
-
"@types/node": "^
|
|
57
|
-
"@typescript-eslint/eslint-plugin": "
|
|
58
|
-
"@typescript-eslint/parser": "
|
|
59
|
-
"depcheck": "^1.4.7",
|
|
60
|
-
"dotenv-cli": "7.4.
|
|
61
|
-
"eslint": "
|
|
62
|
-
"eslint-config-prettier": "9.1.0",
|
|
63
|
-
"eslint-import-resolver-node": "0.3.9",
|
|
64
|
-
"eslint-import-resolver-typescript": "3.6.
|
|
65
|
-
"eslint-plugin-import": "
|
|
66
|
-
"eslint-plugin-jest": "
|
|
67
|
-
"eslint-plugin-jsdoc": "
|
|
68
|
-
"eslint-plugin-json-files": "4.1
|
|
69
|
-
"eslint-plugin-prefer-arrow-functions": "3.
|
|
70
|
-
"eslint-plugin-prettier": "5.1
|
|
71
|
-
"eslint-plugin-tsdoc": "0.
|
|
72
|
-
"eslint-plugin-unicorn": "
|
|
73
|
-
"globals": "15.
|
|
74
|
-
"jest": "29.7.0",
|
|
75
|
-
"jsonc-eslint-parser": "2.4.0",
|
|
76
|
-
"prettier": "3.
|
|
77
|
-
"prettier-plugin-organize-imports": "
|
|
78
|
-
"prettier-plugin-sh": "0.14.0",
|
|
79
|
-
"release-it": "17.
|
|
80
|
-
"terser": "^5.
|
|
81
|
-
"ts-jest": "29.
|
|
82
|
-
"typescript": "5.4
|
|
83
|
-
},
|
|
84
|
-
"engines": {
|
|
85
|
-
"node": "^14.18.1 || >=16.0.0"
|
|
86
|
-
}
|
|
87
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "resolve-accept-language",
|
|
3
|
+
"version": "3.1.6",
|
|
4
|
+
"description": "Resolve the preferred locale based on the value of an `Accept-Language` HTTP header.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"accept-language",
|
|
7
|
+
"RFC 4647",
|
|
8
|
+
"locale",
|
|
9
|
+
"language tags",
|
|
10
|
+
"RFC 4646",
|
|
11
|
+
"BCP 47",
|
|
12
|
+
"RFC 2616",
|
|
13
|
+
"resolve",
|
|
14
|
+
"detect",
|
|
15
|
+
"intl",
|
|
16
|
+
"i18n",
|
|
17
|
+
"internationalization"
|
|
18
|
+
],
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/resolve-accept-language/resolve-accept-language.git"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"author": "resolve-accept-language",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./lib/esm/index.d.ts",
|
|
30
|
+
"default": "./lib/esm/index.js"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./lib/cjs/index.d.ts",
|
|
34
|
+
"default": "./lib/cjs/index.js"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"main": "lib/cjs/index.js",
|
|
39
|
+
"module": "lib/esm/index.js",
|
|
40
|
+
"types": "lib/esm/index.d.ts",
|
|
41
|
+
"files": [
|
|
42
|
+
"lib"
|
|
43
|
+
],
|
|
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 && tsc -p tsconfig.build-scripts.json && node lib/build-scripts/build.js && npm run test",
|
|
46
|
+
"depcheck": "depcheck",
|
|
47
|
+
"lint-fix": "eslint --fix .",
|
|
48
|
+
"prettier": "prettier --write .",
|
|
49
|
+
"release": "dotenv -- release-it --only-version",
|
|
50
|
+
"test": "jest --coverage"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@eslint/js": "^9.9.1",
|
|
54
|
+
"@release-it/conventional-changelog": "8.0.1",
|
|
55
|
+
"@types/jest": "29.5.12",
|
|
56
|
+
"@types/node": "^22.5.2",
|
|
57
|
+
"@typescript-eslint/eslint-plugin": "8.3.0",
|
|
58
|
+
"@typescript-eslint/parser": "8.3.0",
|
|
59
|
+
"depcheck": "^1.4.7",
|
|
60
|
+
"dotenv-cli": "7.4.2",
|
|
61
|
+
"eslint": "9.9.1",
|
|
62
|
+
"eslint-config-prettier": "9.1.0",
|
|
63
|
+
"eslint-import-resolver-node": "0.3.9",
|
|
64
|
+
"eslint-import-resolver-typescript": "3.6.3",
|
|
65
|
+
"eslint-plugin-import-x": "^4.1.1",
|
|
66
|
+
"eslint-plugin-jest": "28.8.1",
|
|
67
|
+
"eslint-plugin-jsdoc": "50.2.2",
|
|
68
|
+
"eslint-plugin-json-files": "4.4.1",
|
|
69
|
+
"eslint-plugin-prefer-arrow-functions": "3.4.1",
|
|
70
|
+
"eslint-plugin-prettier": "5.2.1",
|
|
71
|
+
"eslint-plugin-tsdoc": "0.3.0",
|
|
72
|
+
"eslint-plugin-unicorn": "55.0.0",
|
|
73
|
+
"globals": "15.9.0",
|
|
74
|
+
"jest": "29.7.0",
|
|
75
|
+
"jsonc-eslint-parser": "2.4.0",
|
|
76
|
+
"prettier": "3.3.3",
|
|
77
|
+
"prettier-plugin-organize-imports": "4.0.0",
|
|
78
|
+
"prettier-plugin-sh": "0.14.0",
|
|
79
|
+
"release-it": "17.6.0",
|
|
80
|
+
"terser": "^5.31.6",
|
|
81
|
+
"ts-jest": "29.2.5",
|
|
82
|
+
"typescript": "5.5.4"
|
|
83
|
+
},
|
|
84
|
+
"engines": {
|
|
85
|
+
"node": "^14.18.1 || >=16.0.0"
|
|
86
|
+
}
|
|
87
|
+
}
|