@fgv/ts-bcp47 5.0.0-21 → 5.0.0-23

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 (95) hide show
  1. package/package.json +8 -8
  2. package/src/data/bcp/overrides.json +0 -20
  3. package/src/data/iana/language-subtags.json +0 -57439
  4. package/src/data/iana/language-tag-extensions.json +0 -38
  5. package/src/data/unsd/m49.json +0 -3723
  6. package/src/index.ts +0 -29
  7. package/src/packlets/bcp47/bcp47Subtags/converters.ts +0 -33
  8. package/src/packlets/bcp47/bcp47Subtags/index.ts +0 -27
  9. package/src/packlets/bcp47/bcp47Subtags/model.ts +0 -46
  10. package/src/packlets/bcp47/bcp47Subtags/validate.ts +0 -55
  11. package/src/packlets/bcp47/common.ts +0 -85
  12. package/src/packlets/bcp47/helpers.ts +0 -117
  13. package/src/packlets/bcp47/index.ts +0 -38
  14. package/src/packlets/bcp47/languageRegistryData.ts +0 -304
  15. package/src/packlets/bcp47/languageTag.ts +0 -467
  16. package/src/packlets/bcp47/languageTagParser.ts +0 -307
  17. package/src/packlets/bcp47/match/chooser.ts +0 -164
  18. package/src/packlets/bcp47/match/common.ts +0 -57
  19. package/src/packlets/bcp47/match/index.ts +0 -26
  20. package/src/packlets/bcp47/match/similarity.ts +0 -243
  21. package/src/packlets/bcp47/normalization/baseNormalizer.ts +0 -146
  22. package/src/packlets/bcp47/normalization/canonicalNormalizer.ts +0 -102
  23. package/src/packlets/bcp47/normalization/common.ts +0 -84
  24. package/src/packlets/bcp47/normalization/index.ts +0 -27
  25. package/src/packlets/bcp47/normalization/normalizeTag.ts +0 -116
  26. package/src/packlets/bcp47/normalization/preferredTagNormalizer.ts +0 -213
  27. package/src/packlets/bcp47/overrides/converters.ts +0 -58
  28. package/src/packlets/bcp47/overrides/defaultRegistries.ts +0 -40
  29. package/src/packlets/bcp47/overrides/index.ts +0 -25
  30. package/src/packlets/bcp47/overrides/model.ts +0 -33
  31. package/src/packlets/bcp47/overrides/overridesRegistry.ts +0 -104
  32. package/src/packlets/bcp47/validation/baseValidator.ts +0 -132
  33. package/src/packlets/bcp47/validation/common.ts +0 -86
  34. package/src/packlets/bcp47/validation/index.ts +0 -29
  35. package/src/packlets/bcp47/validation/isCanonical.ts +0 -103
  36. package/src/packlets/bcp47/validation/isInPreferredForm.ts +0 -53
  37. package/src/packlets/bcp47/validation/isStrictlyValid.ts +0 -117
  38. package/src/packlets/bcp47/validation/isValid.ts +0 -122
  39. package/src/packlets/bcp47/validation/isWellFormed.ts +0 -102
  40. package/src/packlets/bcp47/validation/validateTag.ts +0 -175
  41. package/src/packlets/iana/common/converters.ts +0 -67
  42. package/src/packlets/iana/common/model.ts +0 -56
  43. package/src/packlets/iana/common/registeredItems.ts +0 -145
  44. package/src/packlets/iana/common/utils.ts +0 -32
  45. package/src/packlets/iana/common/validate.ts +0 -64
  46. package/src/packlets/iana/converters.ts +0 -26
  47. package/src/packlets/iana/defaultRegistries.ts +0 -40
  48. package/src/packlets/iana/index.ts +0 -33
  49. package/src/packlets/iana/jar/converters.ts +0 -26
  50. package/src/packlets/iana/jar/index.ts +0 -27
  51. package/src/packlets/iana/jar/jarConverters.ts +0 -70
  52. package/src/packlets/iana/jar/jarModel.ts +0 -34
  53. package/src/packlets/iana/jar/language-subtags/converters.ts +0 -26
  54. package/src/packlets/iana/jar/language-subtags/index.ts +0 -27
  55. package/src/packlets/iana/jar/language-subtags/model.ts +0 -26
  56. package/src/packlets/iana/jar/language-subtags/registry/converters.ts +0 -40
  57. package/src/packlets/iana/jar/language-subtags/registry/index.ts +0 -26
  58. package/src/packlets/iana/jar/language-subtags/registry/model.ts +0 -171
  59. package/src/packlets/iana/jar/language-subtags/tags/converters.ts +0 -120
  60. package/src/packlets/iana/jar/language-subtags/tags/index.ts +0 -28
  61. package/src/packlets/iana/jar/language-subtags/tags/model.ts +0 -71
  62. package/src/packlets/iana/jar/language-subtags/tags/tagValidation.ts +0 -67
  63. package/src/packlets/iana/jar/language-subtags/tags/validate.ts +0 -106
  64. package/src/packlets/iana/jar/model.ts +0 -26
  65. package/src/packlets/iana/language-subtags/common.ts +0 -46
  66. package/src/packlets/iana/language-subtags/converters.ts +0 -226
  67. package/src/packlets/iana/language-subtags/index.ts +0 -30
  68. package/src/packlets/iana/language-subtags/jarConverters.ts +0 -269
  69. package/src/packlets/iana/language-subtags/model.ts +0 -213
  70. package/src/packlets/iana/language-subtags/scope.ts +0 -222
  71. package/src/packlets/iana/language-subtags/subtagRegistry.ts +0 -136
  72. package/src/packlets/iana/language-subtags/validate.ts +0 -23
  73. package/src/packlets/iana/language-tag-extensions/converters.ts +0 -71
  74. package/src/packlets/iana/language-tag-extensions/extensionsRegistry.ts +0 -92
  75. package/src/packlets/iana/language-tag-extensions/extensionsScope.ts +0 -60
  76. package/src/packlets/iana/language-tag-extensions/index.ts +0 -29
  77. package/src/packlets/iana/language-tag-extensions/jarConverters.ts +0 -91
  78. package/src/packlets/iana/language-tag-extensions/model.ts +0 -67
  79. package/src/packlets/iana/language-tag-extensions/validate.ts +0 -36
  80. package/src/packlets/iana/languageRegistries.ts +0 -57
  81. package/src/packlets/iana/model.ts +0 -26
  82. package/src/packlets/iana/validate.ts +0 -23
  83. package/src/packlets/unsd/areas.ts +0 -105
  84. package/src/packlets/unsd/common.ts +0 -79
  85. package/src/packlets/unsd/csv/converters.ts +0 -82
  86. package/src/packlets/unsd/csv/index.ts +0 -26
  87. package/src/packlets/unsd/csv/model.ts +0 -44
  88. package/src/packlets/unsd/defaultRegistries.ts +0 -40
  89. package/src/packlets/unsd/index.ts +0 -28
  90. package/src/packlets/unsd/regionCodes.ts +0 -144
  91. package/src/packlets/unsd/regions.ts +0 -95
  92. package/src/packlets/utils/index.ts +0 -24
  93. package/src/packlets/utils/jsonHelpers.ts +0 -25
  94. package/src/packlets/utils/public.ts +0 -28
  95. package/src/packlets/utils/validationHelpers.ts +0 -180
@@ -1,116 +0,0 @@
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
- import { Result, succeed } from '@fgv/ts-utils';
24
- import { TagNormalization, compareNormalization } from './common';
25
-
26
- import { ISubtags } from '../common';
27
- import { ITagNormalizer } from './baseNormalizer';
28
- import { CanonicalNormalizer } from './canonicalNormalizer';
29
- import { PreferredNormalizer } from './preferredTagNormalizer';
30
-
31
- /**
32
- * Normalization helpers for BCP-47 language tags.
33
- * @public
34
- */
35
- export class NormalizeTag {
36
- /**
37
- * @internal
38
- */
39
- private static _normalizers: Record<TagNormalization, ITagNormalizer | undefined> | undefined = undefined;
40
-
41
- /**
42
- * Converts a BCP-47 language tag to canonical form. Canonical form uses the recommended capitalization rules
43
- * specified in {@link https://www.rfc-editor.org/rfc/rfc5646.html#section-2.1.1 | RFC 5646} but are not
44
- * otherwise modified.
45
- *
46
- * @param subtags - The individual {@link Bcp47.Subtags | subtags} to be normalized.
47
- * @returns `Success` with the normalized equivalent {@link Bcp47.Subtags | subtags},
48
- * or `Failure` with details if an error occurs.
49
- */
50
- public static toCanonical(subtags: ISubtags): Result<ISubtags> {
51
- return this.normalizeSubtags(subtags, 'canonical');
52
- }
53
-
54
- /**
55
- * Converts a BCP-47 language tag to preferred form. Preferred form uses the recommended capitalization rules
56
- * specified in {@link https://www.rfc-editor.org/rfc/rfc5646.html#section-2.1.1 | RFC 5646} and also
57
- * applies additional preferred values specified in the
58
- * {@link https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry | language subtag registry}:
59
- * extraneous (suppressed) script tags are removed, deprecated language, extlang, script or region tags are replaced
60
- * with up-to-date preferred values, and grandfathered or redundant tags with a defined preferred-value are replaced
61
- * in their entirety with the new preferred value.
62
- * @param subtags - The individual {@link Bcp47.Subtags | subtags} to be normalized.
63
- * @returns `Success` with the normalized equivalent {@link Bcp47.Subtags | subtags},
64
- * or `Failure` with details if an error occurs.
65
- */
66
- public static toPreferred(subtags: ISubtags): Result<ISubtags> {
67
- return this.normalizeSubtags(subtags, 'preferred');
68
- }
69
-
70
- /**
71
- * Chooses an appropriate default tag normalizer given desired and optional current
72
- * {@link Bcp47.TagNormalization | normalization level}.
73
- * @param wantNormalization - The desired {@link Bcp47.TagNormalization | normalization level}.
74
- * @param haveNormalization - (optional) The current {@link Bcp47.TagNormalization | normalization level}.
75
- * @returns An appropriate {@link Bcp47.TagNormalizer | tag normalizer} or `undefined` if no additional
76
- * normalization is necessary.
77
- * @internal
78
- */
79
- public static chooseNormalizer(
80
- wantNormalization: TagNormalization,
81
- haveNormalization?: TagNormalization
82
- ): ITagNormalizer | undefined {
83
- if (haveNormalization && compareNormalization(haveNormalization, wantNormalization) >= 0) {
84
- return undefined;
85
- }
86
-
87
- if (!this._normalizers) {
88
- this._normalizers = {
89
- unknown: undefined,
90
- none: undefined,
91
- canonical: new CanonicalNormalizer(),
92
- preferred: new PreferredNormalizer()
93
- };
94
- }
95
- return this._normalizers![wantNormalization];
96
- }
97
-
98
- /**
99
- * Normalizes supplied {@link Bcp47.Subtags | subtags} to a requested
100
- * {@link Bcp47.TagNormalization | normalization level}, if necessary. If
101
- * no normalization is necessary, returns the supplied subtags.
102
- * @param subtags - The {@link Bcp47.Subtags | subtags} to be normalized.
103
- * @param wantNormalization - The desired {@link Bcp47.TagNormalization | normalization level}.
104
- * @param haveNormalization - (optional) The current {@link Bcp47.TagNormalization | normalization level}.
105
- * @returns `Success` with the normalized {@link Bcp47.Subtags | subtags}, or
106
- * `Failure` with details if an error occurs.
107
- */
108
- public static normalizeSubtags(
109
- subtags: ISubtags,
110
- wantNormalization: TagNormalization,
111
- haveNormalization?: TagNormalization
112
- ): Result<ISubtags> {
113
- const normalizer = this.chooseNormalizer(wantNormalization, haveNormalization);
114
- return normalizer?.processSubtags(subtags) ?? succeed(subtags);
115
- }
116
- }
@@ -1,213 +0,0 @@
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
- import * as Iana from '../../iana';
24
- import * as Bcp47Subtags from '../bcp47Subtags';
25
-
26
- import { Result, fail, mapResults, succeed } from '@fgv/ts-utils';
27
- import { IExtensionSubtagValue, ISubtags, subtagsToString } from '../common';
28
-
29
- import { sanitizeJson } from '../../utils';
30
- import { LanguageTagParser } from '../languageTagParser';
31
- import { TagNormalizerBase } from './baseNormalizer';
32
- import { TagNormalization } from './common';
33
-
34
- /**
35
- * @public
36
- */
37
- export class PreferredNormalizer extends TagNormalizerBase {
38
- public readonly normalization: TagNormalization = 'preferred';
39
-
40
- protected _processLanguage(subtags: ISubtags): Result<Iana.LanguageSubtags.LanguageSubtag | undefined> {
41
- if (subtags.primaryLanguage) {
42
- const language = this._iana.subtags.languages.tryGet(subtags.primaryLanguage);
43
- if (!language) {
44
- return fail(`invalid language subtag "${subtags.primaryLanguage}"`);
45
- }
46
- return succeed(language.preferredValue ?? language.subtag);
47
- }
48
- return succeed(undefined);
49
- }
50
-
51
- protected _processExtlangs(subtags: ISubtags): Result<Iana.LanguageSubtags.ExtLangSubtag[] | undefined> {
52
- if (subtags.extlangs) {
53
- return mapResults(subtags.extlangs.map((e) => this._iana.subtags.extlangs.toValidCanonical(e)));
54
- }
55
- return succeed(undefined);
56
- }
57
-
58
- protected _processScript(subtags: ISubtags): Result<Iana.LanguageSubtags.ScriptSubtag | undefined> {
59
- if (subtags.primaryLanguage && subtags.script) {
60
- const language = this._iana.subtags.languages.tryGet(subtags.primaryLanguage);
61
- /* c8 ignore next 3 - internal error difficult to test */
62
- if (!language) {
63
- return fail(`invalid primary language subtag "${subtags.primaryLanguage}.`);
64
- }
65
-
66
- const script = this._iana.subtags.scripts.toValidCanonical(subtags.script).orDefault();
67
- if (!script) {
68
- return fail(`invalid script subtag "${subtags.script}`);
69
- }
70
-
71
- if (language.suppressScript !== script) {
72
- return succeed(script);
73
- }
74
- }
75
- return succeed(undefined);
76
- }
77
-
78
- protected _processRegion(subtags: ISubtags): Result<Iana.LanguageSubtags.RegionSubtag | undefined> {
79
- if (subtags.region) {
80
- return this._iana.subtags.regions.get(subtags.region).onSuccess((region) => {
81
- return succeed(region.preferredValue ?? region.subtag);
82
- });
83
- }
84
- return succeed(undefined);
85
- }
86
-
87
- protected _processVariants(subtags: ISubtags): Result<Iana.LanguageSubtags.VariantSubtag[] | undefined> {
88
- if (subtags.variants) {
89
- return mapResults(
90
- subtags.variants.map((v) => this._iana.subtags.variants.toValidCanonical(v))
91
- ).onSuccess((v) => this._verifyUnique('variant', v, (v) => v));
92
- }
93
- return succeed(undefined);
94
- }
95
-
96
- protected _processExtensionSingleton(
97
- singleton: Bcp47Subtags.Model.ExtensionSingleton
98
- ): Result<Bcp47Subtags.Model.ExtensionSingleton> {
99
- return this._iana.extensions.extensions.toValidCanonical(singleton);
100
- }
101
-
102
- protected _processExtensionSubtagValue(
103
- value: Bcp47Subtags.Model.ExtensionSubtag
104
- ): Result<Bcp47Subtags.Model.ExtensionSubtag> {
105
- return Bcp47Subtags.Validate.extensionSubtag.toCanonical(value);
106
- }
107
-
108
- protected _processExtensions(subtags: ISubtags): Result<IExtensionSubtagValue[] | undefined> {
109
- return super._processExtensions(subtags).onSuccess((extensions) => {
110
- return this._verifyUnique('extensions', extensions, (e) => e.singleton);
111
- });
112
- }
113
-
114
- protected _processPrivateUseTags(
115
- subtags: ISubtags
116
- ): Result<Iana.LanguageSubtags.ExtendedLanguageRange[] | undefined> {
117
- if (subtags.privateUse) {
118
- const merged = subtags.privateUse.join('-');
119
- return Iana.LanguageSubtags.Validate.extendedLanguageRange.toCanonical(merged).onSuccess((canon) => {
120
- return succeed(canon.split('-') as Iana.LanguageSubtags.ExtendedLanguageRange[]);
121
- });
122
- }
123
- return succeed(undefined);
124
- }
125
-
126
- protected _processGrandfatheredTags(
127
- subtags: ISubtags
128
- ): Result<Iana.LanguageSubtags.GrandfatheredTag | undefined> {
129
- if (subtags.grandfathered) {
130
- return this._iana.subtags.grandfathered.toValidCanonical(subtags.grandfathered);
131
- }
132
- return succeed(undefined);
133
- }
134
-
135
- protected _postValidateGrandfatheredTag(subtags: ISubtags): Result<ISubtags> {
136
- if (subtags.grandfathered) {
137
- return this._iana.subtags.grandfathered.get(subtags.grandfathered).onSuccess((grandfathered) => {
138
- if (grandfathered.preferredValue) {
139
- return LanguageTagParser.parse(grandfathered.preferredValue, this._iana)
140
- .onSuccess((gfSubtags) => {
141
- /* c8 ignore next 5 - would require a registry error too hard to test */
142
- if (gfSubtags.grandfathered !== undefined) {
143
- return fail<ISubtags>(
144
- `preferred value ${grandfathered.preferredValue} of grandfathered tag ${subtags.grandfathered} is also grandfathered.`
145
- );
146
- }
147
- return this.processSubtags(gfSubtags);
148
- })
149
- .onFailure((message) => {
150
- /* c8 ignore next 4 - would require a registry error too hard to test */
151
- return fail(
152
- `grandfathered tag "${subtags.grandfathered}" has invalid preferred value "${grandfathered.preferredValue}":\n${message}`
153
- );
154
- });
155
- }
156
- return succeed(subtags);
157
- });
158
- }
159
- return succeed(subtags);
160
- }
161
-
162
- protected _postValidateRedundantTag(subtags: ISubtags): Result<ISubtags> {
163
- const tag = subtagsToString(subtags);
164
- const redundant = this._iana.subtags.redundant.tryGetCanonical(tag);
165
- if (redundant?.preferredValue) {
166
- return LanguageTagParser.parse(redundant.preferredValue, this._iana);
167
- }
168
- return succeed(subtags);
169
- }
170
-
171
- protected _postValidateExtLangs(subtags: ISubtags): Result<ISubtags> {
172
- if (subtags.extlangs) {
173
- if (subtags.extlangs.length > 1) {
174
- return fail(`${subtags.extlangs.join('-')}: multiple extlang subtags is invalid`);
175
- }
176
- if (subtags.extlangs.length > 0) {
177
- const registry = this._iana.subtags.extlangs.tryGet(subtags.extlangs[0]);
178
- if (registry) {
179
- if (
180
- registry.preferredValue &&
181
- registry.prefix === /* c8 ignore next */ subtags.primaryLanguage?.toLowerCase()
182
- ) {
183
- const preferred = this._iana.subtags.languages.tryGet(registry.preferredValue);
184
- if (preferred) {
185
- return succeed(
186
- sanitizeJson({
187
- ...subtags,
188
- primaryLanguage: preferred.subtag,
189
- extlangs: undefined
190
- })
191
- );
192
- }
193
- }
194
- }
195
- }
196
- }
197
- return succeed(subtags);
198
- }
199
-
200
- protected _postValidate(subtags: ISubtags): Result<ISubtags> {
201
- return super
202
- ._postValidate(subtags)
203
- .onSuccess((subtags) => {
204
- return this._postValidateExtLangs(subtags);
205
- })
206
- .onSuccess((subtags) => {
207
- return this._postValidateGrandfatheredTag(subtags);
208
- })
209
- .onSuccess((subtags) => {
210
- return this._postValidateRedundantTag(subtags);
211
- });
212
- }
213
- }
@@ -1,58 +0,0 @@
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
-
24
- import * as Iana from '../../iana';
25
- import * as Model from './model';
26
-
27
- import { JsonFile } from '@fgv/ts-json-base';
28
- import { Converter, Converters, Result } from '@fgv/ts-utils';
29
-
30
- /**
31
- * @internal
32
- */
33
- export const languageOverrideRecord: Converter<Model.ILanguageOverrideRecord, unknown> =
34
- Converters.strictObject<Model.ILanguageOverrideRecord>(
35
- {
36
- language: Iana.LanguageSubtags.Converters.Tags.languageSubtag,
37
- preferredRegion: Iana.LanguageSubtags.Converters.Tags.regionSubtag,
38
- defaultAffinity: Converters.string,
39
- affinity: Converters.recordOf(Converters.arrayOf(Iana.LanguageSubtags.Converters.Tags.regionSubtag))
40
- },
41
- { optionalFields: ['affinity', 'defaultAffinity', 'preferredRegion'] }
42
- );
43
-
44
- /**
45
- * @internal
46
- */
47
- export const languageOverridesFile = Converters.arrayOf(languageOverrideRecord);
48
-
49
- /**
50
- * Loads a language overrides JSON file.
51
- * @internal
52
- * @param path - Path from which overrides should be loaded.
53
- * @returns `Success` with the loaded overrides or `Failure` with details if an
54
- * error occurs.
55
- */
56
- export function loadLanguageOverridesFileSync(path: string): Result<Model.ILanguageOverrideRecord[]> {
57
- return JsonFile.convertJsonFileSync(path, languageOverridesFile);
58
- }
@@ -1,40 +0,0 @@
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
- import { OverridesRegistry } from './overridesRegistry';
24
-
25
- /**
26
- * @public
27
- */
28
- export class DefaultRegistries {
29
- /**
30
- * @internal
31
- */
32
- private static _overridesRegistry?: OverridesRegistry = undefined;
33
-
34
- public static get overridesRegistry(): OverridesRegistry {
35
- if (!this._overridesRegistry) {
36
- this._overridesRegistry = OverridesRegistry.loadDefault().orThrow();
37
- }
38
- return this._overridesRegistry;
39
- }
40
- }
@@ -1,25 +0,0 @@
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
-
24
- export { OverridesRegistry } from './overridesRegistry';
25
- export { DefaultRegistries } from './defaultRegistries';
@@ -1,33 +0,0 @@
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
- import * as Iana from '../../iana';
24
-
25
- /**
26
- * @internal
27
- */
28
- export interface ILanguageOverrideRecord {
29
- language: Iana.LanguageSubtags.LanguageSubtag;
30
- preferredRegion?: Iana.LanguageSubtags.RegionSubtag;
31
- defaultAffinity?: string;
32
- affinity?: Record<string, Iana.LanguageSubtags.RegionSubtag[]>;
33
- }
@@ -1,104 +0,0 @@
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
- import * as Iana from '../../iana';
24
- import * as Converters from './converters';
25
- import * as Model from './model';
26
-
27
- import { Result, mapResults, succeed } from '@fgv/ts-utils';
28
- // eslint-disable-next-line @rushstack/packlets/mechanics
29
- import defaultOverrides from '../../../data/bcp/overrides.json';
30
-
31
- /**
32
- * @public
33
- */
34
- export interface ILanguageOverride {
35
- language: Iana.LanguageSubtags.LanguageSubtag;
36
- preferredRegion?: Iana.LanguageSubtags.RegionSubtag;
37
- defaultAffinity?: string;
38
- affinity?: Map<Iana.LanguageSubtags.RegionSubtag, string>;
39
- }
40
-
41
- /**
42
- * @public
43
- */
44
- export class OverridesRegistry {
45
- public readonly overrides: Map<Iana.LanguageSubtags.LanguageSubtag, ILanguageOverride>;
46
-
47
- protected constructor() {
48
- this.overrides = new Map();
49
- }
50
-
51
- public static create(overrides: ILanguageOverride[]): Result<OverridesRegistry> {
52
- const registry = new OverridesRegistry();
53
- overrides.forEach((o) => registry.overrides.set(o.language, o));
54
- return succeed(registry);
55
- }
56
-
57
- public static createFromJson(from: unknown): Result<OverridesRegistry> {
58
- return Converters.languageOverridesFile
59
- .convert(from)
60
- .onSuccess((records) => {
61
- return mapResults(records.map(OverridesRegistry._overrideFromRecord));
62
- })
63
- .onSuccess((overrides) => {
64
- return OverridesRegistry.create(overrides);
65
- });
66
- }
67
-
68
- public static loadDefault(): Result<OverridesRegistry> {
69
- return this.createFromJson(defaultOverrides);
70
- }
71
-
72
- public static loadJson(path: string): Result<OverridesRegistry> {
73
- return Converters.loadLanguageOverridesFileSync(path).onSuccess(OverridesRegistry.createFromJson);
74
- }
75
-
76
- /**
77
- * Converts a file {@link Bcp47.Overrides.Model.LanguageOverrideRecord | LanguageOverrideRecord }
78
- * to a runtime {@link Bcp47.Overrides.LanguageOverride | LanguageOverride}.
79
- * @param record - The {@link Bcp47.Overrides.Model.LanguageOverrideRecord | LanguageOverrideRecord } to
80
- * be converted.
81
- * @returns `Success` with the resulting {@link Bcp47.Overrides.LanguageOverride | LanguageOverride}
82
- * or `Error` with details if an error occurs.
83
- * @internal
84
- */
85
- protected static _overrideFromRecord(record: Model.ILanguageOverrideRecord): Result<ILanguageOverride> {
86
- const override: ILanguageOverride = { language: record.language };
87
- if (record.preferredRegion) {
88
- override.preferredRegion = record.preferredRegion;
89
- }
90
- if (record.defaultAffinity) {
91
- override.defaultAffinity = record.defaultAffinity;
92
- }
93
- if (record.affinity) {
94
- const affinity = new Map<Iana.LanguageSubtags.RegionSubtag, string>();
95
- for (const kvp of Object.entries(record.affinity)) {
96
- for (const region of kvp[1]) {
97
- affinity.set(region, kvp[0]);
98
- }
99
- }
100
- override.affinity = affinity;
101
- }
102
- return succeed(override);
103
- }
104
- }