@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,467 +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
- import { Result, captureResult, succeed } from '@fgv/ts-utils';
26
- import { ISubtags, UndeterminedLanguage, subtagsToString } from './common';
27
- import { TagNormalization, mostNormalized } from './normalization/common';
28
- import { TagValidity, mostValid } from './validation/common';
29
-
30
- import { LanguageRegistryData } from './languageRegistryData';
31
- import { LanguageTagParser } from './languageTagParser';
32
- import { NormalizeTag } from './normalization';
33
- import { ValidateTag } from './validation';
34
-
35
- /**
36
- * Initialization options for parsing or creation of {@link Bcp47.LanguageTag | language tag} objects.
37
- * @public
38
- */
39
- export interface ILanguageTagInitOptions {
40
- /**
41
- * Desired {@link Bcp47.TagValidity | validity level} (optional).
42
- */
43
- validity?: TagValidity;
44
- /**
45
- * Desired {@link Bcp47.TagNormalization | normalization level} (optional).
46
- */
47
- normalization?: TagNormalization;
48
- /**
49
- * The {@link Iana.LanguageRegistries | IANA language subtag and extension registries} to
50
- * be used for the request (optional).
51
- */
52
- iana?: Iana.LanguageRegistries;
53
- }
54
-
55
- /**
56
- * Represents a single BCP-47 language tag.
57
- * @public
58
- */
59
- export class LanguageTag {
60
- /**
61
- * The individual {@link Bcp47.Subtags | subtags} for
62
- * this language tag.
63
- */
64
- public readonly subtags: Readonly<ISubtags>;
65
-
66
- /**
67
- * A string representation of this language tag.
68
- */
69
- public readonly tag: string;
70
-
71
- /**
72
- * Details about this language tag from the IANA language
73
- * registries.
74
- */
75
- public readonly registry: LanguageRegistryData;
76
-
77
- /**
78
- * @internal
79
- */
80
- protected readonly _iana: Iana.LanguageRegistries;
81
-
82
- /**
83
- * @internal
84
- */
85
- protected _validity: TagValidity;
86
-
87
- /**
88
- * @internal
89
- */
90
- protected _normalization: TagNormalization;
91
-
92
- /**
93
- * @internal
94
- */
95
- protected _isValid: undefined | boolean;
96
-
97
- /**
98
- * @internal
99
- */
100
- protected _isStrictlyValid: undefined | boolean;
101
-
102
- /**
103
- * @internal
104
- */
105
- protected _isCanonical: undefined | boolean;
106
-
107
- /**
108
- * @internal
109
- */
110
- protected _isPreferred: undefined | boolean;
111
-
112
- /**
113
- * @internal
114
- */
115
- protected _suppressedScript: undefined | Iana.LanguageSubtags.ScriptSubtag | false;
116
-
117
- /**
118
- * Constructs a {@link Bcp47.LanguageTag | LanguageTag }.
119
- * @param subtags - The {@link Bcp47.Subtags | subtags } from
120
- * which the tag is constructed.
121
- * @param validity - Known {@link Bcp47.TagValidity | validation level} of the
122
- * supplied subtags.
123
- * @param normalization - Known {@link Bcp47.TagNormalization | normalization level}
124
- * of the supplied subtags.
125
- * @param iana - The {@link Iana.LanguageRegistries} used to validate and normalize
126
- * this tag.
127
- * @internal
128
- */
129
- protected constructor(
130
- subtags: ISubtags,
131
- validity: TagValidity,
132
- normalization: TagNormalization,
133
- iana: Iana.LanguageRegistries
134
- ) {
135
- this.subtags = Object.freeze({ ...subtags });
136
- this._normalization = normalization;
137
- this._validity = validity;
138
- this.tag = subtagsToString(subtags);
139
- this._iana = iana;
140
- this.registry = new LanguageRegistryData(this.subtags, iana);
141
-
142
- if (validity === 'strictly-valid') {
143
- this._isStrictlyValid = true;
144
- this._isValid = true;
145
- } else if (validity === 'valid') {
146
- this._isValid = true;
147
- }
148
-
149
- if (normalization === 'preferred') {
150
- this._isPreferred = true;
151
- this._isCanonical = true;
152
- } else if (normalization === 'canonical') {
153
- this._isCanonical = true;
154
- }
155
- }
156
-
157
- /**
158
- * The effective script of this language tag, if known.
159
- * The effective script is the script subtag in the tag itself,
160
- * if present, or the `Suppress-Script` defined in the
161
- * {@link https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry | IANA subtag registry}
162
- * for the primary language of this tag. Can be `undefined`
163
- * if neither the tag nor the IANA registry define a script.
164
- */
165
- public get effectiveScript(): Iana.LanguageSubtags.ScriptSubtag | undefined {
166
- return this.subtags.script ?? this.getSuppressedScript();
167
- }
168
-
169
- /**
170
- * Determines if this tag represents the special `undetermined` language.
171
- */
172
- public get isUndetermined(): boolean {
173
- /* c8 ignore next */
174
- return this.subtags.primaryLanguage?.toLowerCase() === UndeterminedLanguage;
175
- }
176
-
177
- /**
178
- * Whether this language tag is valid.
179
- */
180
- public get isValid(): boolean {
181
- if (this._isValid === undefined) {
182
- this._isValid = ValidateTag.isValid(this.subtags);
183
- if (this._isValid) {
184
- this._validity = 'valid';
185
- }
186
- }
187
- return this._isValid === true;
188
- }
189
-
190
- /**
191
- * Whether if this language tag is strictly valid.
192
- */
193
- public get isStrictlyValid(): boolean {
194
- if (this._isStrictlyValid === undefined) {
195
- this._isStrictlyValid = ValidateTag.isStrictlyValid(this.subtags);
196
- if (this._isStrictlyValid) {
197
- this._validity = 'strictly-valid';
198
- }
199
- }
200
- return this._isStrictlyValid === true;
201
- }
202
-
203
- /**
204
- * Whether this language tag is in canonical form.
205
- */
206
- public get isCanonical(): boolean {
207
- if (this._isCanonical === undefined) {
208
- this._isCanonical = ValidateTag.isCanonical(this.subtags);
209
- if (this._isCanonical) {
210
- this._normalization = 'canonical';
211
- }
212
- }
213
- return this._isCanonical === true;
214
- }
215
-
216
- /**
217
- * Whether this language tag is in preferred form.
218
- */
219
- public get isPreferred(): boolean {
220
- if (this._isPreferred === undefined) {
221
- this._isPreferred = ValidateTag.isInPreferredForm(this.subtags);
222
- if (this._isPreferred) {
223
- this._normalization = 'preferred';
224
- }
225
- }
226
- return this._isPreferred === true;
227
- }
228
-
229
- /**
230
- * Whether this language tag is a grandfathered tag.
231
- */
232
- public get isGrandfathered(): boolean {
233
- return this.subtags.grandfathered !== undefined;
234
- }
235
-
236
- /**
237
- * Gets a text description of this tag.
238
- */
239
- public get description(): string {
240
- const parts: string[] = [];
241
- if (!this.subtags.grandfathered) {
242
- if (this.registry.primaryLanguage || this.subtags.primaryLanguage) {
243
- parts.push((this.registry.primaryLanguage?.description[0] ?? this.subtags.primaryLanguage)!);
244
- }
245
- if (this.registry.extlangs) {
246
- for (const e of this.registry.extlangs) {
247
- parts.push(`/ ${e.registry?.description[0] ?? e.subtag}`);
248
- }
249
- }
250
- if (this.subtags.script) {
251
- parts.push(`in ${this.registry.script?.description[0] ?? this.subtags.script} script`);
252
- }
253
- if (this.subtags.region) {
254
- parts.push(`as spoken in ${this.registry.region?.description[0] ?? this.subtags.region}`);
255
- }
256
- if (this.registry.variants && this.registry.variants.length > 0) {
257
- for (const e of this.registry.variants) {
258
- parts.push(`(${e.registry?.description[0] ?? e.subtag})`);
259
- }
260
- }
261
- if (this.registry.extensions && this.registry.extensions.length > 0) {
262
- for (const e of this.registry.extensions) {
263
- parts.push(`(${e.registry?.description[0] ?? `-${e.singleton}`} "${e.value}")`);
264
- }
265
- }
266
- if (this.subtags.privateUse && this.subtags.privateUse.length > 0) {
267
- parts.push(`(-x "${this.subtags.privateUse.join('-')}")`);
268
- }
269
- } else {
270
- parts.push(`${this.tag} (grandfathered)`);
271
- }
272
- return parts.join(' ');
273
- }
274
-
275
- /**
276
- * Creates a new {@link Bcp47.LanguageTag | language tag} from a supplied `string` tag
277
- * using optional configuration, if supplied.
278
- * @param tag - The `string` tag from which the {@link Bcp47.LanguageTag | language tag}
279
- * is te be constructed.
280
- * @param options - (optional) set of {@link Bcp47.ILanguageTagInitOptions | init options}
281
- * to guide the validation and normalization of this tag.
282
- * @returns `Success` with the new {@link Bcp47.LanguageTag | language tag} or `Failure`
283
- * with details if an error occurs.
284
- */
285
- public static createFromTag(tag: string, options?: ILanguageTagInitOptions): Result<LanguageTag> {
286
- options = this._getOptions(options);
287
-
288
- return LanguageTagParser.parse(tag, options.iana).onSuccess((subtags) => {
289
- return this._createTransformed(subtags, 'unknown', 'unknown', options);
290
- });
291
- }
292
-
293
- /**
294
- * Creates a new {@link Bcp47.LanguageTag | language tag} from a supplied
295
- * {@link Bcp47.Subtags | subtags} using optional configuration,
296
- * if supplied.
297
- * @param tag - The {@link Bcp47.Subtags | subtags} from which the
298
- * {@link Bcp47.LanguageTag | language tag} is te be constructed.
299
- * @param options - (optional) set of {@link Bcp47.ILanguageTagInitOptions | init options}
300
- * to guide the validation and normalization of this tag.
301
- * @returns `Success` with the new {@link Bcp47.LanguageTag | language tag} or `Failure`
302
- * with details if an error occurs.
303
- */
304
- public static createFromSubtags(subtags: ISubtags, options?: ILanguageTagInitOptions): Result<LanguageTag> {
305
- return this._createTransformed(subtags, 'unknown', 'unknown', options);
306
- }
307
-
308
- /**
309
- * Creates a new {@link Bcp47.LanguageTag | language tag} from a supplied `string`
310
- * tag or {@link Bcp47.Subtags | subtags} using optional configuration,
311
- * if supplied.
312
- * @param from - The `string` tag or {@link Bcp47.Subtags | subtags} from
313
- * which the {@link Bcp47.LanguageTag | language tag} is te be constructed.
314
- * @param options - (optional) set of {@link Bcp47.ILanguageTagInitOptions | init options}
315
- * to guide the validation and normalization of this tag.
316
- * @returns `Success` with the new {@link Bcp47.LanguageTag | language tag} or `Failure`
317
- * with details if an error occurs.
318
- */
319
- public static create(from: string | ISubtags, options?: ILanguageTagInitOptions): Result<LanguageTag> {
320
- if (typeof from === 'string') {
321
- return this.createFromTag(from, options);
322
- } else {
323
- return this.createFromSubtags(from, options);
324
- }
325
- }
326
-
327
- /**
328
- * Constructs a new {@link Bcp47.LanguageTag | language tag} by applying appropriate transformations
329
- * to as supplied {@link Bcp47.Subtags | Bcp47.Subtags}.
330
- * @param subtags - The {@link Bcp47.Subtags | subtags} which represent the tag.
331
- * @param fromValidity - The {@link Bcp47.TagValidity | validation level} of the supplied subtags.
332
- * @param fromNormalization - The {@link Bcp47.TagNormalization | normalization level} fo the
333
- * supplied subtags.
334
- * @param partialOptions - Any {@link Bcp47.ILanguageTagInitOptions | initialization options}.
335
- * @returns `Success` with the corresponding {@link Bcp47.LanguageTag | language tag} or `Failure`
336
- * with details if an error occurs.
337
- * @internal
338
- */
339
- protected static _createTransformed(
340
- subtags: ISubtags,
341
- fromValidity: TagValidity,
342
- fromNormalization: TagNormalization,
343
- partialOptions?: ILanguageTagInitOptions
344
- ): Result<LanguageTag> {
345
- const options = this._getOptions(partialOptions);
346
- return ValidateTag.validateSubtags(subtags, options.validity, fromValidity)
347
- .onSuccess(() => {
348
- return NormalizeTag.normalizeSubtags(subtags, options.normalization, fromNormalization);
349
- })
350
- .onSuccess((normalized) => {
351
- const validity = mostValid(fromValidity, options.validity);
352
- const normalization = mostNormalized(fromNormalization, options.normalization);
353
- return captureResult(() => new LanguageTag(normalized, validity, normalization, options.iana));
354
- });
355
- }
356
-
357
- /**
358
- * Gets a fully-specified {@link Bcp47.ILanguageTagInitOptions} from partial or undefined
359
- * options, substituting defaults as appropriate.
360
- * @param options - The {@link Bcp47.ILanguageTagInitOptions} to be expanded, or `undefined`
361
- * for default options.
362
- * @returns Fully-specified {@link Bcp47.ILanguageTagInitOptions | init options}.
363
- * @internal
364
- */
365
- protected static _getOptions(options?: ILanguageTagInitOptions): Required<ILanguageTagInitOptions> {
366
- return {
367
- iana: options?.iana ?? Iana.DefaultRegistries.languageRegistries,
368
- validity: options?.validity ?? 'well-formed',
369
- normalization: options?.normalization ?? 'none'
370
- };
371
- }
372
-
373
- /**
374
- * Returns the `Suppress-Script` value defined for the primary language of this tag,
375
- * regardless of whether a different script is defined in this subtag.
376
- * @returns The suppress-script defined for the primary language, or undefined if
377
- * the primary language is invalid or has no defined suppressed script.
378
- */
379
- public getSuppressedScript(): Iana.LanguageSubtags.ScriptSubtag | undefined {
380
- if (this._suppressedScript === undefined) {
381
- this._suppressedScript = false;
382
- if (this.subtags.primaryLanguage) {
383
- const language = this._iana.subtags.languages.tryGet(this.subtags.primaryLanguage);
384
- if (language?.suppressScript !== undefined) {
385
- this._suppressedScript = language.suppressScript;
386
- }
387
- }
388
- }
389
- return this._suppressedScript ? this._suppressedScript : undefined;
390
- }
391
-
392
- /**
393
- * Gets a confirmed valid representation of this language tag.
394
- * @returns `Success` with a valid representation of this {@link Bcp47.LanguageTag | language tag},
395
- * or `Failure` with details if the tag cannot be validated.
396
- */
397
- public toValid(): Result<LanguageTag> {
398
- if (this.isValid) {
399
- return succeed(this);
400
- }
401
- const options: ILanguageTagInitOptions = {
402
- iana: this._iana,
403
- validity: 'valid',
404
- normalization: this._normalization
405
- };
406
- return LanguageTag._createTransformed(this.subtags, this._validity, this._normalization, options);
407
- }
408
-
409
- /**
410
- * Gets a confirmed strictly valid representation of this language tag.
411
- * @returns `Success` with a strictly valid representation of this {@link Bcp47.LanguageTag | language tag},
412
- * or `Failure` with details if the tag cannot be strictly validated.
413
- */
414
- public toStrictlyValid(): Result<LanguageTag> {
415
- if (this.isStrictlyValid) {
416
- return succeed(this);
417
- }
418
- const options: ILanguageTagInitOptions = {
419
- iana: this._iana,
420
- validity: 'strictly-valid',
421
- normalization: this._normalization
422
- };
423
- return LanguageTag._createTransformed(this.subtags, this._validity, this._normalization, options);
424
- }
425
-
426
- /**
427
- * Gets a confirmed canonical representation of this language tag.
428
- * @returns `Success` with a canonical representation of this {@link Bcp47.LanguageTag | language tag},
429
- * or `Failure` with details if the tag cannot be normalized to canonical form.
430
- */
431
- public toCanonical(): Result<LanguageTag> {
432
- if (this.isCanonical) {
433
- return succeed(this);
434
- }
435
- const options: ILanguageTagInitOptions = {
436
- iana: this._iana,
437
- validity: this._validity,
438
- normalization: 'canonical'
439
- };
440
- return LanguageTag._createTransformed(this.subtags, this._validity, this._normalization, options);
441
- }
442
-
443
- /**
444
- * Gets a confirmed preferred representation of this language tag.
445
- * @returns `Success` with a preferred representation of this {@link Bcp47.LanguageTag | language tag},
446
- * or `Failure` with details if the tag cannot be normalized to preferred form.
447
- */
448
- public toPreferred(): Result<LanguageTag> {
449
- if (this.isPreferred) {
450
- return succeed(this);
451
- }
452
- const options: ILanguageTagInitOptions = {
453
- iana: this._iana,
454
- validity: 'valid', // preferred requires validity
455
- normalization: 'preferred'
456
- };
457
- return LanguageTag._createTransformed(this.subtags, this._validity, this._normalization, options);
458
- }
459
-
460
- /**
461
- * Gets a string representation of this language tag.
462
- * @returns A string representation of this language tag.
463
- */
464
- public toString(): string {
465
- return this.tag;
466
- }
467
- }