@fgv/ts-bcp47 5.1.0-2 → 5.1.0-4

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.
Files changed (103) hide show
  1. package/dist/data/bcp/overrides.json +21 -0
  2. package/dist/data/unsd/m49.json +3723 -0
  3. package/dist/index.browser.js +30 -0
  4. package/dist/index.js +28 -0
  5. package/dist/packlets/bcp47/bcp47Subtags/converters.js +32 -0
  6. package/dist/packlets/bcp47/bcp47Subtags/index.js +26 -0
  7. package/dist/packlets/bcp47/bcp47Subtags/model.js +23 -0
  8. package/dist/packlets/bcp47/bcp47Subtags/validate.js +48 -0
  9. package/dist/packlets/bcp47/common.js +53 -0
  10. package/dist/packlets/bcp47/helpers.js +96 -0
  11. package/dist/packlets/bcp47/index.js +32 -0
  12. package/dist/packlets/bcp47/languageRegistryData.js +202 -0
  13. package/dist/packlets/bcp47/languageTag.js +363 -0
  14. package/dist/packlets/bcp47/languageTagParser.js +275 -0
  15. package/dist/packlets/bcp47/match/chooser.js +88 -0
  16. package/dist/packlets/bcp47/match/common.js +49 -0
  17. package/dist/packlets/bcp47/match/index.js +26 -0
  18. package/dist/packlets/bcp47/match/similarity.js +205 -0
  19. package/dist/packlets/bcp47/normalization/baseNormalizer.js +89 -0
  20. package/dist/packlets/bcp47/normalization/canonicalNormalizer.js +86 -0
  21. package/dist/packlets/bcp47/normalization/common.js +77 -0
  22. package/dist/packlets/bcp47/normalization/index.js +27 -0
  23. package/dist/packlets/bcp47/normalization/normalizeTag.js +101 -0
  24. package/dist/packlets/bcp47/normalization/preferredTagNormalizer.js +177 -0
  25. package/dist/packlets/bcp47/overrides/converters.js +49 -0
  26. package/dist/packlets/bcp47/overrides/defaultRegistries.js +38 -0
  27. package/dist/packlets/bcp47/overrides/index.js +25 -0
  28. package/dist/packlets/bcp47/overrides/model.js +23 -0
  29. package/dist/packlets/bcp47/overrides/overridesRegistry.js +83 -0
  30. package/dist/packlets/bcp47/validation/baseValidator.js +86 -0
  31. package/dist/packlets/bcp47/validation/common.js +77 -0
  32. package/dist/packlets/bcp47/validation/index.js +29 -0
  33. package/dist/packlets/bcp47/validation/isCanonical.js +79 -0
  34. package/dist/packlets/bcp47/validation/isInPreferredForm.js +46 -0
  35. package/dist/packlets/bcp47/validation/isStrictlyValid.js +94 -0
  36. package/dist/packlets/bcp47/validation/isValid.js +92 -0
  37. package/dist/packlets/bcp47/validation/isWellFormed.js +75 -0
  38. package/dist/packlets/bcp47/validation/validateTag.js +153 -0
  39. package/dist/packlets/iana/common/converters.js +58 -0
  40. package/dist/packlets/iana/common/model.js +23 -0
  41. package/dist/packlets/iana/common/registeredItems.js +120 -0
  42. package/dist/packlets/iana/common/utils.js +30 -0
  43. package/dist/packlets/iana/common/validate.js +59 -0
  44. package/dist/packlets/iana/converters.js +25 -0
  45. package/dist/packlets/iana/defaultRegistries.js +38 -0
  46. package/dist/packlets/iana/iana-data-embedded.js +60 -0
  47. package/dist/packlets/iana/index.browser.js +34 -0
  48. package/dist/packlets/iana/index.js +34 -0
  49. package/dist/packlets/iana/jar/converters.js +25 -0
  50. package/dist/packlets/iana/jar/index.js +26 -0
  51. package/dist/packlets/iana/jar/jarConverters.js +60 -0
  52. package/dist/packlets/iana/jar/jarModel.js +23 -0
  53. package/dist/packlets/iana/jar/language-subtags/converters.js +25 -0
  54. package/dist/packlets/iana/jar/language-subtags/index.js +26 -0
  55. package/dist/packlets/iana/jar/language-subtags/model.js +25 -0
  56. package/dist/packlets/iana/jar/language-subtags/registry/converters.js +180 -0
  57. package/dist/packlets/iana/jar/language-subtags/registry/index.js +26 -0
  58. package/dist/packlets/iana/jar/language-subtags/registry/model.js +43 -0
  59. package/dist/packlets/iana/jar/language-subtags/tags/converters.js +101 -0
  60. package/dist/packlets/iana/jar/language-subtags/tags/index.js +27 -0
  61. package/dist/packlets/iana/jar/language-subtags/tags/model.js +23 -0
  62. package/dist/packlets/iana/jar/language-subtags/tags/tagValidation.js +66 -0
  63. package/dist/packlets/iana/jar/language-subtags/tags/validate.js +85 -0
  64. package/dist/packlets/iana/jar/model.js +25 -0
  65. package/dist/packlets/iana/language-subtags/common.js +23 -0
  66. package/dist/packlets/iana/language-subtags/converters.js +182 -0
  67. package/dist/packlets/iana/language-subtags/index.browser.js +30 -0
  68. package/dist/packlets/iana/language-subtags/index.js +29 -0
  69. package/dist/packlets/iana/language-subtags/jarConverters.js +288 -0
  70. package/dist/packlets/iana/language-subtags/model.js +23 -0
  71. package/dist/packlets/iana/language-subtags/scope.js +169 -0
  72. package/dist/packlets/iana/language-subtags/subtagRegistry.js +108 -0
  73. package/dist/packlets/iana/language-subtags/validate.js +23 -0
  74. package/dist/packlets/iana/language-tag-extensions/converters.js +59 -0
  75. package/dist/packlets/iana/language-tag-extensions/extensionsRegistry.js +64 -0
  76. package/dist/packlets/iana/language-tag-extensions/extensionsScope.js +50 -0
  77. package/dist/packlets/iana/language-tag-extensions/index.js +28 -0
  78. package/dist/packlets/iana/language-tag-extensions/jarConverters.js +143 -0
  79. package/dist/packlets/iana/language-tag-extensions/model.js +24 -0
  80. package/dist/packlets/iana/language-tag-extensions/validate.js +33 -0
  81. package/dist/packlets/iana/languageRegistries.js +80 -0
  82. package/dist/packlets/iana/languageRegistriesFileLoader.js +73 -0
  83. package/dist/packlets/iana/languageRegistriesLoader.js +113 -0
  84. package/dist/packlets/iana/model.js +25 -0
  85. package/dist/packlets/iana/validate.js +23 -0
  86. package/dist/packlets/unsd/areas.js +92 -0
  87. package/dist/packlets/unsd/common.js +23 -0
  88. package/dist/packlets/unsd/csv/converters.js +75 -0
  89. package/dist/packlets/unsd/csv/index.js +25 -0
  90. package/dist/packlets/unsd/csv/model.js +23 -0
  91. package/dist/packlets/unsd/defaultRegistries.js +38 -0
  92. package/dist/packlets/unsd/index.js +27 -0
  93. package/dist/packlets/unsd/regionCodes.js +115 -0
  94. package/dist/packlets/unsd/regions.js +71 -0
  95. package/dist/packlets/utils/index.js +24 -0
  96. package/dist/packlets/utils/jsonHelpers.js +25 -0
  97. package/dist/packlets/utils/public.js +24 -0
  98. package/dist/packlets/utils/validationHelpers.js +116 -0
  99. package/dist/test/unit/bcp47/commonTestCases.js +475 -0
  100. package/dist/test/unit/bcp47/languageTagHelpers.js +178 -0
  101. package/dist/test/unit/iana/testConstants.js +68 -0
  102. package/dist/tsdoc-metadata.json +1 -1
  103. package/package.json +36 -31
@@ -0,0 +1,101 @@
1
+ /*
2
+ * Copyright (c) 2022 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ import { succeed } from '@fgv/ts-utils';
23
+ import { compareNormalization } from './common';
24
+ import { CanonicalNormalizer } from './canonicalNormalizer';
25
+ import { PreferredNormalizer } from './preferredTagNormalizer';
26
+ /**
27
+ * Normalization helpers for BCP-47 language tags.
28
+ * @public
29
+ */
30
+ export class NormalizeTag {
31
+ /**
32
+ * Converts a BCP-47 language tag to canonical form. Canonical form uses the recommended capitalization rules
33
+ * specified in {@link https://www.rfc-editor.org/rfc/rfc5646.html#section-2.1.1 | RFC 5646} but are not
34
+ * otherwise modified.
35
+ *
36
+ * @param subtags - The individual {@link Bcp47.Subtags | subtags} to be normalized.
37
+ * @returns `Success` with the normalized equivalent {@link Bcp47.Subtags | subtags},
38
+ * or `Failure` with details if an error occurs.
39
+ */
40
+ static toCanonical(subtags) {
41
+ return this.normalizeSubtags(subtags, 'canonical');
42
+ }
43
+ /**
44
+ * Converts a BCP-47 language tag to preferred form. Preferred form uses the recommended capitalization rules
45
+ * specified in {@link https://www.rfc-editor.org/rfc/rfc5646.html#section-2.1.1 | RFC 5646} and also
46
+ * applies additional preferred values specified in the
47
+ * {@link https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry | language subtag registry}:
48
+ * extraneous (suppressed) script tags are removed, deprecated language, extlang, script or region tags are replaced
49
+ * with up-to-date preferred values, and grandfathered or redundant tags with a defined preferred-value are replaced
50
+ * in their entirety with the new preferred value.
51
+ * @param subtags - The individual {@link Bcp47.Subtags | subtags} to be normalized.
52
+ * @returns `Success` with the normalized equivalent {@link Bcp47.Subtags | subtags},
53
+ * or `Failure` with details if an error occurs.
54
+ */
55
+ static toPreferred(subtags) {
56
+ return this.normalizeSubtags(subtags, 'preferred');
57
+ }
58
+ /**
59
+ * Chooses an appropriate default tag normalizer given desired and optional current
60
+ * {@link Bcp47.TagNormalization | normalization level}.
61
+ * @param wantNormalization - The desired {@link Bcp47.TagNormalization | normalization level}.
62
+ * @param haveNormalization - (optional) The current {@link Bcp47.TagNormalization | normalization level}.
63
+ * @returns An appropriate {@link Bcp47.TagNormalizer | tag normalizer} or `undefined` if no additional
64
+ * normalization is necessary.
65
+ * @internal
66
+ */
67
+ static chooseNormalizer(wantNormalization, haveNormalization) {
68
+ if (haveNormalization && compareNormalization(haveNormalization, wantNormalization) >= 0) {
69
+ return undefined;
70
+ }
71
+ if (!this._normalizers) {
72
+ this._normalizers = {
73
+ unknown: undefined,
74
+ none: undefined,
75
+ canonical: new CanonicalNormalizer(),
76
+ preferred: new PreferredNormalizer()
77
+ };
78
+ }
79
+ return this._normalizers[wantNormalization];
80
+ }
81
+ /**
82
+ * Normalizes supplied {@link Bcp47.Subtags | subtags} to a requested
83
+ * {@link Bcp47.TagNormalization | normalization level}, if necessary. If
84
+ * no normalization is necessary, returns the supplied subtags.
85
+ * @param subtags - The {@link Bcp47.Subtags | subtags} to be normalized.
86
+ * @param wantNormalization - The desired {@link Bcp47.TagNormalization | normalization level}.
87
+ * @param haveNormalization - (optional) The current {@link Bcp47.TagNormalization | normalization level}.
88
+ * @returns `Success` with the normalized {@link Bcp47.Subtags | subtags}, or
89
+ * `Failure` with details if an error occurs.
90
+ */
91
+ static normalizeSubtags(subtags, wantNormalization, haveNormalization) {
92
+ var _a;
93
+ const normalizer = this.chooseNormalizer(wantNormalization, haveNormalization);
94
+ return (_a = normalizer === null || normalizer === void 0 ? void 0 : normalizer.processSubtags(subtags)) !== null && _a !== void 0 ? _a : succeed(subtags);
95
+ }
96
+ }
97
+ /**
98
+ * @internal
99
+ */
100
+ NormalizeTag._normalizers = undefined;
101
+ //# sourceMappingURL=normalizeTag.js.map
@@ -0,0 +1,177 @@
1
+ /*
2
+ * Copyright (c) 2022 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ import * as Iana from '../../iana';
23
+ import * as Bcp47Subtags from '../bcp47Subtags';
24
+ import { fail, mapResults, succeed } from '@fgv/ts-utils';
25
+ import { subtagsToString } from '../common';
26
+ import { sanitizeJson } from '../../utils';
27
+ import { LanguageTagParser } from '../languageTagParser';
28
+ import { TagNormalizerBase } from './baseNormalizer';
29
+ /**
30
+ * @public
31
+ */
32
+ export class PreferredNormalizer extends TagNormalizerBase {
33
+ constructor() {
34
+ super(...arguments);
35
+ this.normalization = 'preferred';
36
+ }
37
+ _processLanguage(subtags) {
38
+ var _a;
39
+ if (subtags.primaryLanguage) {
40
+ const language = this._iana.subtags.languages.tryGet(subtags.primaryLanguage);
41
+ if (!language) {
42
+ return fail(`invalid language subtag "${subtags.primaryLanguage}"`);
43
+ }
44
+ return succeed((_a = language.preferredValue) !== null && _a !== void 0 ? _a : language.subtag);
45
+ }
46
+ return succeed(undefined);
47
+ }
48
+ _processExtlangs(subtags) {
49
+ if (subtags.extlangs) {
50
+ return mapResults(subtags.extlangs.map((e) => this._iana.subtags.extlangs.toValidCanonical(e)));
51
+ }
52
+ return succeed(undefined);
53
+ }
54
+ _processScript(subtags) {
55
+ if (subtags.primaryLanguage && subtags.script) {
56
+ const language = this._iana.subtags.languages.tryGet(subtags.primaryLanguage);
57
+ /* c8 ignore next 3 - internal error difficult to test */
58
+ if (!language) {
59
+ return fail(`invalid primary language subtag "${subtags.primaryLanguage}.`);
60
+ }
61
+ const script = this._iana.subtags.scripts.toValidCanonical(subtags.script).orDefault();
62
+ if (!script) {
63
+ return fail(`invalid script subtag "${subtags.script}`);
64
+ }
65
+ if (language.suppressScript !== script) {
66
+ return succeed(script);
67
+ }
68
+ }
69
+ return succeed(undefined);
70
+ }
71
+ _processRegion(subtags) {
72
+ if (subtags.region) {
73
+ return this._iana.subtags.regions.get(subtags.region).onSuccess((region) => {
74
+ var _a;
75
+ return succeed((_a = region.preferredValue) !== null && _a !== void 0 ? _a : region.subtag);
76
+ });
77
+ }
78
+ return succeed(undefined);
79
+ }
80
+ _processVariants(subtags) {
81
+ if (subtags.variants) {
82
+ return mapResults(subtags.variants.map((v) => this._iana.subtags.variants.toValidCanonical(v))).onSuccess((v) => this._verifyUnique('variant', v, (v) => v));
83
+ }
84
+ return succeed(undefined);
85
+ }
86
+ _processExtensionSingleton(singleton) {
87
+ return this._iana.extensions.extensions.toValidCanonical(singleton);
88
+ }
89
+ _processExtensionSubtagValue(value) {
90
+ return Bcp47Subtags.Validate.extensionSubtag.toCanonical(value);
91
+ }
92
+ _processExtensions(subtags) {
93
+ return super._processExtensions(subtags).onSuccess((extensions) => {
94
+ return this._verifyUnique('extensions', extensions, (e) => e.singleton);
95
+ });
96
+ }
97
+ _processPrivateUseTags(subtags) {
98
+ if (subtags.privateUse) {
99
+ const merged = subtags.privateUse.join('-');
100
+ return Iana.LanguageSubtags.Validate.extendedLanguageRange.toCanonical(merged).onSuccess((canon) => {
101
+ return succeed(canon.split('-'));
102
+ });
103
+ }
104
+ return succeed(undefined);
105
+ }
106
+ _processGrandfatheredTags(subtags) {
107
+ if (subtags.grandfathered) {
108
+ return this._iana.subtags.grandfathered.toValidCanonical(subtags.grandfathered);
109
+ }
110
+ return succeed(undefined);
111
+ }
112
+ _postValidateGrandfatheredTag(subtags) {
113
+ if (subtags.grandfathered) {
114
+ return this._iana.subtags.grandfathered.get(subtags.grandfathered).onSuccess((grandfathered) => {
115
+ if (grandfathered.preferredValue) {
116
+ return LanguageTagParser.parse(grandfathered.preferredValue, this._iana)
117
+ .onSuccess((gfSubtags) => {
118
+ /* c8 ignore next 5 - would require a registry error too hard to test */
119
+ if (gfSubtags.grandfathered !== undefined) {
120
+ return fail(`preferred value ${grandfathered.preferredValue} of grandfathered tag ${subtags.grandfathered} is also grandfathered.`);
121
+ }
122
+ return this.processSubtags(gfSubtags);
123
+ })
124
+ .onFailure((message) => {
125
+ /* c8 ignore next 4 - would require a registry error too hard to test */
126
+ return fail(`grandfathered tag "${subtags.grandfathered}" has invalid preferred value "${grandfathered.preferredValue}":\n${message}`);
127
+ });
128
+ }
129
+ return succeed(subtags);
130
+ });
131
+ }
132
+ return succeed(subtags);
133
+ }
134
+ _postValidateRedundantTag(subtags) {
135
+ const tag = subtagsToString(subtags);
136
+ const redundant = this._iana.subtags.redundant.tryGetCanonical(tag);
137
+ if (redundant === null || redundant === void 0 ? void 0 : redundant.preferredValue) {
138
+ return LanguageTagParser.parse(redundant.preferredValue, this._iana);
139
+ }
140
+ return succeed(subtags);
141
+ }
142
+ _postValidateExtLangs(subtags) {
143
+ var _a;
144
+ if (subtags.extlangs) {
145
+ if (subtags.extlangs.length > 1) {
146
+ return fail(`${subtags.extlangs.join('-')}: multiple extlang subtags is invalid`);
147
+ }
148
+ if (subtags.extlangs.length > 0) {
149
+ const registry = this._iana.subtags.extlangs.tryGet(subtags.extlangs[0]);
150
+ if (registry) {
151
+ if (registry.preferredValue &&
152
+ registry.prefix === /* c8 ignore next */ ((_a = subtags.primaryLanguage) === null || _a === void 0 ? void 0 : _a.toLowerCase())) {
153
+ const preferred = this._iana.subtags.languages.tryGet(registry.preferredValue);
154
+ if (preferred) {
155
+ return succeed(sanitizeJson(Object.assign(Object.assign({}, subtags), { primaryLanguage: preferred.subtag, extlangs: undefined })));
156
+ }
157
+ }
158
+ }
159
+ }
160
+ }
161
+ return succeed(subtags);
162
+ }
163
+ _postValidate(subtags) {
164
+ return super
165
+ ._postValidate(subtags)
166
+ .onSuccess((subtags) => {
167
+ return this._postValidateExtLangs(subtags);
168
+ })
169
+ .onSuccess((subtags) => {
170
+ return this._postValidateGrandfatheredTag(subtags);
171
+ })
172
+ .onSuccess((subtags) => {
173
+ return this._postValidateRedundantTag(subtags);
174
+ });
175
+ }
176
+ }
177
+ //# sourceMappingURL=preferredTagNormalizer.js.map
@@ -0,0 +1,49 @@
1
+ /*
2
+ * Copyright (c) 2022 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ /* eslint-disable @rushstack/typedef-var */
23
+ import * as Iana from '../../iana';
24
+ import { JsonFile } from '@fgv/ts-json-base';
25
+ import { Converters } from '@fgv/ts-utils';
26
+ /**
27
+ * @internal
28
+ */
29
+ export const languageOverrideRecord = Converters.strictObject({
30
+ language: Iana.LanguageSubtags.Converters.Tags.languageSubtag,
31
+ preferredRegion: Iana.LanguageSubtags.Converters.Tags.regionSubtag,
32
+ defaultAffinity: Converters.string,
33
+ affinity: Converters.recordOf(Converters.arrayOf(Iana.LanguageSubtags.Converters.Tags.regionSubtag))
34
+ }, { optionalFields: ['affinity', 'defaultAffinity', 'preferredRegion'] });
35
+ /**
36
+ * @internal
37
+ */
38
+ export const languageOverridesFile = Converters.arrayOf(languageOverrideRecord);
39
+ /**
40
+ * Loads a language overrides JSON file.
41
+ * @internal
42
+ * @param path - Path from which overrides should be loaded.
43
+ * @returns `Success` with the loaded overrides or `Failure` with details if an
44
+ * error occurs.
45
+ */
46
+ export function loadLanguageOverridesFileSync(path) {
47
+ return JsonFile.convertJsonFileSync(path, languageOverridesFile);
48
+ }
49
+ //# sourceMappingURL=converters.js.map
@@ -0,0 +1,38 @@
1
+ /*
2
+ * Copyright (c) 2022 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ import { OverridesRegistry } from './overridesRegistry';
23
+ /**
24
+ * @public
25
+ */
26
+ export class DefaultRegistries {
27
+ static get overridesRegistry() {
28
+ if (!this._overridesRegistry) {
29
+ this._overridesRegistry = OverridesRegistry.loadDefault().orThrow();
30
+ }
31
+ return this._overridesRegistry;
32
+ }
33
+ }
34
+ /**
35
+ * @internal
36
+ */
37
+ DefaultRegistries._overridesRegistry = undefined;
38
+ //# sourceMappingURL=defaultRegistries.js.map
@@ -0,0 +1,25 @@
1
+ /*
2
+ * Copyright (c) 2022 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ // istanbul ignore file
23
+ export { OverridesRegistry } from './overridesRegistry';
24
+ export { DefaultRegistries } from './defaultRegistries';
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright (c) 2022 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ export {};
23
+ //# sourceMappingURL=model.js.map
@@ -0,0 +1,83 @@
1
+ /*
2
+ * Copyright (c) 2022 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ import * as Converters from './converters';
23
+ import { mapResults, succeed } from '@fgv/ts-utils';
24
+ // eslint-disable-next-line @rushstack/packlets/mechanics
25
+ import defaultOverrides from '../../../data/bcp/overrides.json';
26
+ /**
27
+ * @public
28
+ */
29
+ export class OverridesRegistry {
30
+ constructor() {
31
+ this.overrides = new Map();
32
+ }
33
+ static create(overrides) {
34
+ const registry = new OverridesRegistry();
35
+ overrides.forEach((o) => registry.overrides.set(o.language, o));
36
+ return succeed(registry);
37
+ }
38
+ static createFromJson(from) {
39
+ return Converters.languageOverridesFile
40
+ .convert(from)
41
+ .onSuccess((records) => {
42
+ return mapResults(records.map(OverridesRegistry._overrideFromRecord));
43
+ })
44
+ .onSuccess((overrides) => {
45
+ return OverridesRegistry.create(overrides);
46
+ });
47
+ }
48
+ static loadDefault() {
49
+ return this.createFromJson(defaultOverrides);
50
+ }
51
+ static loadJson(path) {
52
+ return Converters.loadLanguageOverridesFileSync(path).onSuccess(OverridesRegistry.createFromJson);
53
+ }
54
+ /**
55
+ * Converts a file {@link Bcp47.Overrides.Model.LanguageOverrideRecord | LanguageOverrideRecord }
56
+ * to a runtime {@link Bcp47.Overrides.LanguageOverride | LanguageOverride}.
57
+ * @param record - The {@link Bcp47.Overrides.Model.LanguageOverrideRecord | LanguageOverrideRecord } to
58
+ * be converted.
59
+ * @returns `Success` with the resulting {@link Bcp47.Overrides.LanguageOverride | LanguageOverride}
60
+ * or `Error` with details if an error occurs.
61
+ * @internal
62
+ */
63
+ static _overrideFromRecord(record) {
64
+ const override = { language: record.language };
65
+ if (record.preferredRegion) {
66
+ override.preferredRegion = record.preferredRegion;
67
+ }
68
+ if (record.defaultAffinity) {
69
+ override.defaultAffinity = record.defaultAffinity;
70
+ }
71
+ if (record.affinity) {
72
+ const affinity = new Map();
73
+ for (const kvp of Object.entries(record.affinity)) {
74
+ for (const region of kvp[1]) {
75
+ affinity.set(region, kvp[0]);
76
+ }
77
+ }
78
+ override.affinity = affinity;
79
+ }
80
+ return succeed(override);
81
+ }
82
+ }
83
+ //# sourceMappingURL=overridesRegistry.js.map
@@ -0,0 +1,86 @@
1
+ /*
2
+ * Copyright (c) 2022 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ import * as Iana from '../../iana';
23
+ import { allSucceed, fail, succeed } from '@fgv/ts-utils';
24
+ import { subtagsToString } from '../common';
25
+ /**
26
+ * @internal
27
+ */
28
+ export class TagValidatorBase {
29
+ constructor(iana) {
30
+ /* c8 ignore next */
31
+ this.iana = iana !== null && iana !== void 0 ? iana : Iana.DefaultRegistries.languageRegistries;
32
+ }
33
+ validateSubtags(subtags) {
34
+ return allSucceed([
35
+ this._checkLanguage(subtags),
36
+ this._checkExtlangs(subtags),
37
+ this._checkScript(subtags),
38
+ this._checkRegion(subtags),
39
+ this._checkVariants(subtags),
40
+ this._checkExtensions(subtags),
41
+ this._checkPrivateUseTags(subtags),
42
+ this._checkGrandfatheredTags(subtags)
43
+ ], true).onSuccess(() => {
44
+ return this._postValidate(subtags).onSuccess(() => succeed(true));
45
+ });
46
+ }
47
+ _basicPostValidation(subtags) {
48
+ if (subtags.primaryLanguage === undefined &&
49
+ subtags.grandfathered === undefined &&
50
+ subtags.privateUse === undefined) {
51
+ return fail(`${subtagsToString(subtags)}: missing primary language subtag.`);
52
+ }
53
+ if (subtags.extlangs && subtags.extlangs.length > 3) {
54
+ return fail(`${subtagsToString(subtags)}: too many extlang subtags`);
55
+ }
56
+ return succeed(subtags);
57
+ }
58
+ _postValidate(subtags) {
59
+ return this._basicPostValidation(subtags);
60
+ }
61
+ _checkExtensions(subtags) {
62
+ if (subtags.extensions) {
63
+ return allSucceed(subtags.extensions.map((ext) => {
64
+ return this._checkExtensionSingleton(ext.singleton).onSuccess(() => {
65
+ return this._checkExtensionSubtagValue(ext.value);
66
+ });
67
+ }), subtags.extensions);
68
+ }
69
+ return succeed(undefined);
70
+ }
71
+ _verifyUnique(description, items, getKey) {
72
+ if (items) {
73
+ const present = new Set();
74
+ return allSucceed(items.map((i) => {
75
+ const key = getKey(i);
76
+ if (present.has(key)) {
77
+ return fail(`${key}: duplicate ${description}`);
78
+ }
79
+ present.add(key);
80
+ return succeed(key);
81
+ }), items);
82
+ }
83
+ return succeed(items);
84
+ }
85
+ }
86
+ //# sourceMappingURL=baseValidator.js.map
@@ -0,0 +1,77 @@
1
+ /*
2
+ * Copyright (c) 2022 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ /**
23
+ * Ordered ranking of the degrees of validity, from `unknown` (least
24
+ * validation) to `strictly-valid` (fully validated).
25
+ * @public
26
+ */
27
+ const validityRank = {
28
+ /**
29
+ * No validation has been performed.
30
+ */
31
+ unknown: 0,
32
+ /**
33
+ * Tag is well-formed {@link https://www.rfc-editor.org/rfc/rfc5646.html#section-2.2.9 | according to RFC 5646}.
34
+ */
35
+ 'well-formed': 0.5,
36
+ /**
37
+ * Tag is valid {@link https://www.rfc-editor.org/rfc/rfc5646.html#section-2.2.9 | according to RFC 5646} -
38
+ * well-formed, and is a registered grandfathered tag or all subtags are registered,
39
+ * with no duplicate extensions or variant subtags.
40
+ */
41
+ valid: 0.9,
42
+ /**
43
+ * Tag is valid {@link https://www.rfc-editor.org/rfc/rfc5646.html#section-2.2.9 | according to RFC 5646} and meets
44
+ * other constraints described by the RFC - e.g. if present, any extlang or variant subtags have a
45
+ * {@link https://www.rfc-editor.org/rfc/rfc5646.html#section-3.1.8 | valid prefix} as specified by
46
+ * the registry.
47
+ */
48
+ 'strictly-valid': 1.0
49
+ };
50
+ /**
51
+ * Determines which of two validity ranks is most normalized.
52
+ * @param v1 - The first {@link TagValidity} to be compared.
53
+ * @param v2 - The second {@link TagValidity} to be compared.
54
+ * @returns `1` if `v1` is more normalized, `-1` if `v2` is more
55
+ * normalized, or `0` if they are identical.
56
+ * @public
57
+ */
58
+ export function compareValidity(v1, v2) {
59
+ if (validityRank[v1] > validityRank[v2]) {
60
+ return 1;
61
+ }
62
+ else if (validityRank[v1] < validityRank[v2]) {
63
+ return -1;
64
+ }
65
+ return 0;
66
+ }
67
+ /**
68
+ * Chooses the most valid of two normalization ranks.
69
+ * @param v1 - The first {@link TagValidity} to be compared.
70
+ * @param v2 - The second {@link TagValidity} to be compared.
71
+ * @returns The most validated of `v1` or `v2`.
72
+ * @public
73
+ */
74
+ export function mostValid(v1, v2) {
75
+ return validityRank[v1] >= validityRank[v2] ? v1 : v2;
76
+ }
77
+ //# sourceMappingURL=common.js.map