@trenskow/language 1.4.46 → 1.4.48
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.js +34 -3
- package/package.json +3 -3
- package/test/index.js +3 -0
package/index.js
CHANGED
|
@@ -66,14 +66,45 @@ const expand = (identifier) => {
|
|
|
66
66
|
|
|
67
67
|
};
|
|
68
68
|
|
|
69
|
-
const collapse = (identifier) => {
|
|
69
|
+
const collapse = (identifier, available) => {
|
|
70
|
+
|
|
71
|
+
let useLanguages = languages;
|
|
72
|
+
|
|
73
|
+
if (typeof available !== 'undefined') {
|
|
74
|
+
|
|
75
|
+
if (!Array.isArray(available) || !available.length) throw new Error('Available languages must be a non-empty array of language identifiers.');
|
|
76
|
+
if (available.some((item) => typeof item !== 'string')) throw new Error('Available languages must be a non-empty array of language identifiers.');
|
|
77
|
+
|
|
78
|
+
available = available.map((identifier) => expand(identifier));
|
|
79
|
+
|
|
80
|
+
useLanguages = {};
|
|
81
|
+
|
|
82
|
+
available.forEach((identifier) => {
|
|
83
|
+
const { languageIdentifier, scriptIdentifier, countryIdentifier } = entities(identifier);
|
|
84
|
+
if (!useLanguages[languageIdentifier]) {
|
|
85
|
+
useLanguages[languageIdentifier] = {
|
|
86
|
+
primaryScripts: languages[languageIdentifier].primaryScripts,
|
|
87
|
+
scripts: {}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
if (!useLanguages[languageIdentifier].scripts[scriptIdentifier]) {
|
|
91
|
+
useLanguages[languageIdentifier].scripts[scriptIdentifier] = {
|
|
92
|
+
countries: []
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
if (!useLanguages[languageIdentifier].scripts[scriptIdentifier].countries.includes(countryIdentifier)) {
|
|
96
|
+
useLanguages[languageIdentifier].scripts[scriptIdentifier].countries.push(countryIdentifier);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
}
|
|
70
101
|
|
|
71
102
|
const { languageIdentifier, scriptIdentifier, countryIdentifier } = entities(expand(identifier));
|
|
72
103
|
|
|
73
104
|
let parts = [languageIdentifier];
|
|
74
105
|
|
|
75
|
-
if (
|
|
76
|
-
if (
|
|
106
|
+
if (useLanguages[languageIdentifier].primaryScripts.length > 1) parts.push(scriptIdentifier);
|
|
107
|
+
if (useLanguages[languageIdentifier].scripts[scriptIdentifier].countries.length > 1) parts.push(countryIdentifier);
|
|
77
108
|
|
|
78
109
|
return parts.join('-');
|
|
79
110
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trenskow/language",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.48",
|
|
4
4
|
"description": "A small library for getting a full language identifier (RFC 5646).",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"@eslint/eslintrc": "^3.3.3",
|
|
22
22
|
"@eslint/js": "^9.13.0",
|
|
23
23
|
"chai": "^6.2.1",
|
|
24
|
-
"eslint": "^9.39.
|
|
24
|
+
"eslint": "^9.39.2",
|
|
25
25
|
"globals": "^16.5.0",
|
|
26
26
|
"mocha": "^11.7.5"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@trenskow/localization-data": "^1.4.
|
|
29
|
+
"@trenskow/localization-data": "^1.4.35"
|
|
30
30
|
}
|
|
31
31
|
}
|
package/test/index.js
CHANGED
|
@@ -14,6 +14,9 @@ describe('language', () => {
|
|
|
14
14
|
it ('should come back with `da` (from `da-DK`).', () => {
|
|
15
15
|
expect(language.collapse('da-DK')).to.equal('da');
|
|
16
16
|
});
|
|
17
|
+
it ('should come back with `en` when collapsing from a list of available languages.', () => {
|
|
18
|
+
expect(language.collapse('en-US', ['en-Latn-US', 'da'])).to.equal('en');
|
|
19
|
+
});
|
|
17
20
|
it ('should throw an error if country is missing (`fr`).', () => {
|
|
18
21
|
expect(() => { language.expand('fr'); }).to.throw('Country is ambiguous for identifier \'fr\'.');
|
|
19
22
|
});
|