@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.
- package/package.json +8 -8
- package/src/data/bcp/overrides.json +0 -20
- package/src/data/iana/language-subtags.json +0 -57439
- package/src/data/iana/language-tag-extensions.json +0 -38
- package/src/data/unsd/m49.json +0 -3723
- package/src/index.ts +0 -29
- package/src/packlets/bcp47/bcp47Subtags/converters.ts +0 -33
- package/src/packlets/bcp47/bcp47Subtags/index.ts +0 -27
- package/src/packlets/bcp47/bcp47Subtags/model.ts +0 -46
- package/src/packlets/bcp47/bcp47Subtags/validate.ts +0 -55
- package/src/packlets/bcp47/common.ts +0 -85
- package/src/packlets/bcp47/helpers.ts +0 -117
- package/src/packlets/bcp47/index.ts +0 -38
- package/src/packlets/bcp47/languageRegistryData.ts +0 -304
- package/src/packlets/bcp47/languageTag.ts +0 -467
- package/src/packlets/bcp47/languageTagParser.ts +0 -307
- package/src/packlets/bcp47/match/chooser.ts +0 -164
- package/src/packlets/bcp47/match/common.ts +0 -57
- package/src/packlets/bcp47/match/index.ts +0 -26
- package/src/packlets/bcp47/match/similarity.ts +0 -243
- package/src/packlets/bcp47/normalization/baseNormalizer.ts +0 -146
- package/src/packlets/bcp47/normalization/canonicalNormalizer.ts +0 -102
- package/src/packlets/bcp47/normalization/common.ts +0 -84
- package/src/packlets/bcp47/normalization/index.ts +0 -27
- package/src/packlets/bcp47/normalization/normalizeTag.ts +0 -116
- package/src/packlets/bcp47/normalization/preferredTagNormalizer.ts +0 -213
- package/src/packlets/bcp47/overrides/converters.ts +0 -58
- package/src/packlets/bcp47/overrides/defaultRegistries.ts +0 -40
- package/src/packlets/bcp47/overrides/index.ts +0 -25
- package/src/packlets/bcp47/overrides/model.ts +0 -33
- package/src/packlets/bcp47/overrides/overridesRegistry.ts +0 -104
- package/src/packlets/bcp47/validation/baseValidator.ts +0 -132
- package/src/packlets/bcp47/validation/common.ts +0 -86
- package/src/packlets/bcp47/validation/index.ts +0 -29
- package/src/packlets/bcp47/validation/isCanonical.ts +0 -103
- package/src/packlets/bcp47/validation/isInPreferredForm.ts +0 -53
- package/src/packlets/bcp47/validation/isStrictlyValid.ts +0 -117
- package/src/packlets/bcp47/validation/isValid.ts +0 -122
- package/src/packlets/bcp47/validation/isWellFormed.ts +0 -102
- package/src/packlets/bcp47/validation/validateTag.ts +0 -175
- package/src/packlets/iana/common/converters.ts +0 -67
- package/src/packlets/iana/common/model.ts +0 -56
- package/src/packlets/iana/common/registeredItems.ts +0 -145
- package/src/packlets/iana/common/utils.ts +0 -32
- package/src/packlets/iana/common/validate.ts +0 -64
- package/src/packlets/iana/converters.ts +0 -26
- package/src/packlets/iana/defaultRegistries.ts +0 -40
- package/src/packlets/iana/index.ts +0 -33
- package/src/packlets/iana/jar/converters.ts +0 -26
- package/src/packlets/iana/jar/index.ts +0 -27
- package/src/packlets/iana/jar/jarConverters.ts +0 -70
- package/src/packlets/iana/jar/jarModel.ts +0 -34
- package/src/packlets/iana/jar/language-subtags/converters.ts +0 -26
- package/src/packlets/iana/jar/language-subtags/index.ts +0 -27
- package/src/packlets/iana/jar/language-subtags/model.ts +0 -26
- package/src/packlets/iana/jar/language-subtags/registry/converters.ts +0 -40
- package/src/packlets/iana/jar/language-subtags/registry/index.ts +0 -26
- package/src/packlets/iana/jar/language-subtags/registry/model.ts +0 -171
- package/src/packlets/iana/jar/language-subtags/tags/converters.ts +0 -120
- package/src/packlets/iana/jar/language-subtags/tags/index.ts +0 -28
- package/src/packlets/iana/jar/language-subtags/tags/model.ts +0 -71
- package/src/packlets/iana/jar/language-subtags/tags/tagValidation.ts +0 -67
- package/src/packlets/iana/jar/language-subtags/tags/validate.ts +0 -106
- package/src/packlets/iana/jar/model.ts +0 -26
- package/src/packlets/iana/language-subtags/common.ts +0 -46
- package/src/packlets/iana/language-subtags/converters.ts +0 -226
- package/src/packlets/iana/language-subtags/index.ts +0 -30
- package/src/packlets/iana/language-subtags/jarConverters.ts +0 -269
- package/src/packlets/iana/language-subtags/model.ts +0 -213
- package/src/packlets/iana/language-subtags/scope.ts +0 -222
- package/src/packlets/iana/language-subtags/subtagRegistry.ts +0 -136
- package/src/packlets/iana/language-subtags/validate.ts +0 -23
- package/src/packlets/iana/language-tag-extensions/converters.ts +0 -71
- package/src/packlets/iana/language-tag-extensions/extensionsRegistry.ts +0 -92
- package/src/packlets/iana/language-tag-extensions/extensionsScope.ts +0 -60
- package/src/packlets/iana/language-tag-extensions/index.ts +0 -29
- package/src/packlets/iana/language-tag-extensions/jarConverters.ts +0 -91
- package/src/packlets/iana/language-tag-extensions/model.ts +0 -67
- package/src/packlets/iana/language-tag-extensions/validate.ts +0 -36
- package/src/packlets/iana/languageRegistries.ts +0 -57
- package/src/packlets/iana/model.ts +0 -26
- package/src/packlets/iana/validate.ts +0 -23
- package/src/packlets/unsd/areas.ts +0 -105
- package/src/packlets/unsd/common.ts +0 -79
- package/src/packlets/unsd/csv/converters.ts +0 -82
- package/src/packlets/unsd/csv/index.ts +0 -26
- package/src/packlets/unsd/csv/model.ts +0 -44
- package/src/packlets/unsd/defaultRegistries.ts +0 -40
- package/src/packlets/unsd/index.ts +0 -28
- package/src/packlets/unsd/regionCodes.ts +0 -144
- package/src/packlets/unsd/regions.ts +0 -95
- package/src/packlets/utils/index.ts +0 -24
- package/src/packlets/utils/jsonHelpers.ts +0 -25
- package/src/packlets/utils/public.ts +0 -28
- package/src/packlets/utils/validationHelpers.ts +0 -180
|
@@ -1,95 +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, fail, succeed } from '@fgv/ts-utils';
|
|
24
|
-
import * as Iana from '../iana';
|
|
25
|
-
import { IGlobalRegion, IntermediateRegionTier, Region } from './common';
|
|
26
|
-
|
|
27
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
28
|
-
export const GlobalRegionName: string = 'World';
|
|
29
|
-
export const GlobalRegionCode: Iana.Model.UnM49RegionCode = '001' as Iana.Model.UnM49RegionCode;
|
|
30
|
-
/* eslint-enable @typescript-eslint/naming-convention */
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @public
|
|
34
|
-
*/
|
|
35
|
-
export class Regions {
|
|
36
|
-
public readonly global: IGlobalRegion;
|
|
37
|
-
/**
|
|
38
|
-
* @internal
|
|
39
|
-
*/
|
|
40
|
-
protected readonly _regions: Map<Iana.Model.UnM49RegionCode, Region>;
|
|
41
|
-
|
|
42
|
-
public constructor() {
|
|
43
|
-
this.global = { name: GlobalRegionName, code: GlobalRegionCode, tier: 'global', regions: [], areas: [] };
|
|
44
|
-
this._regions = new Map();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public tryGetRegion(from: Iana.Model.UnM49RegionCode): Region | undefined {
|
|
48
|
-
return this._regions.get(from);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public getRegion(from: Iana.Model.UnM49RegionCode): Result<Region> {
|
|
52
|
-
const got = this._regions.get(from);
|
|
53
|
-
return got ? succeed(got) : fail(`${from}: region not found`);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public getOrAddRegionChildRegion(
|
|
57
|
-
tier: IntermediateRegionTier,
|
|
58
|
-
parent: Region,
|
|
59
|
-
code: Iana.Model.UnM49RegionCode | undefined,
|
|
60
|
-
name: string | undefined
|
|
61
|
-
): Result<Region> {
|
|
62
|
-
if (!code && !name) {
|
|
63
|
-
return succeed(parent);
|
|
64
|
-
} else if (!code || !name) {
|
|
65
|
-
return fail(`${code}(${name}): code and name must both be present`);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const existing = this._regions.get(code);
|
|
69
|
-
if (existing) {
|
|
70
|
-
if (existing.name !== name) {
|
|
71
|
-
return fail(`${code}: cannot add ${name} - already exists as ${existing.name}`);
|
|
72
|
-
}
|
|
73
|
-
if (existing.tier !== tier) {
|
|
74
|
-
return fail(`${code}: cannot add ${name} as tier ${tier} - already exists as tier ${existing.tier}`);
|
|
75
|
-
}
|
|
76
|
-
if (existing.parent !== parent) {
|
|
77
|
-
const haveParent = `${existing.parent.code}/${existing.parent.name}`;
|
|
78
|
-
const wantParent = `${parent.code}/${parent.name}`;
|
|
79
|
-
return fail(
|
|
80
|
-
`${code}: cannot add ${name} with parent ${wantParent} - already exists with parent ${haveParent}`
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
return succeed(existing);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const added = { name, code, tier, parent, regions: [], areas: [] };
|
|
87
|
-
this._regions.set(code, added);
|
|
88
|
-
parent.regions.push(added);
|
|
89
|
-
return succeed(added);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
public getAll(): Region[] {
|
|
93
|
-
return Array.from(this._regions.values());
|
|
94
|
-
}
|
|
95
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2021 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
|
-
export * from './jsonHelpers';
|
|
24
|
-
export * from './validationHelpers';
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2021 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
|
-
export function sanitizeJson<T>(from: T): T {
|
|
24
|
-
return JSON.parse(JSON.stringify(from)) as T;
|
|
25
|
-
}
|
|
@@ -1,28 +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 {
|
|
25
|
-
Normalizer,
|
|
26
|
-
ValidationHelpers,
|
|
27
|
-
IValidationHelpersConstructorParams as ValidationHelpersConstructorParams
|
|
28
|
-
} from './validationHelpers';
|
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2021 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 { Converter, Converters, Result, Validation, fail, succeed } from '@fgv/ts-utils';
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* A function which accepts a value of the expected type and reformats it to match
|
|
27
|
-
* the canonical presentation form.
|
|
28
|
-
* @public
|
|
29
|
-
*/
|
|
30
|
-
export type Normalizer<T extends string, TC = unknown> = (val: T, context?: TC) => Result<T>;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Initializer for {@link Utils.ValidationHelpers | validation helpers}.
|
|
34
|
-
* @public
|
|
35
|
-
*/
|
|
36
|
-
export interface IValidationHelpersConstructorParams<T extends string, TC = unknown> {
|
|
37
|
-
description: string;
|
|
38
|
-
isWellFormed: Validation.TypeGuardWithContext<T, TC>;
|
|
39
|
-
isCanonical: Validation.TypeGuardWithContext<T, TC>;
|
|
40
|
-
toCanonical?: Normalizer<T, TC>;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
A collection of validation and normalization helpers for constrained string
|
|
45
|
-
types.
|
|
46
|
-
* @public
|
|
47
|
-
*/
|
|
48
|
-
export class ValidationHelpers<T extends string, TC = unknown> {
|
|
49
|
-
/**
|
|
50
|
-
* Describes the group of tags validated by these helpers.
|
|
51
|
-
*/
|
|
52
|
-
public readonly description: string;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* A `Converter` which converts `unknown` to the tag type
|
|
56
|
-
* validated by these helpers, if possible.
|
|
57
|
-
*/
|
|
58
|
-
public readonly converter: Converter<T, TC>;
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Determines is a supplied tag is well-formed according to the
|
|
62
|
-
* lexical rules defined for the tag validated by these helpers.
|
|
63
|
-
*/
|
|
64
|
-
public readonly isWellFormed: Validation.TypeGuardWithContext<T, TC>;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Determines is a supplied tag is well-formed and uses canonical
|
|
68
|
-
* formatting, according to the lexical rules defined for the tag
|
|
69
|
-
* validated by these helpers.
|
|
70
|
-
*/
|
|
71
|
-
public readonly isCanonical: Validation.TypeGuardWithContext<T, TC>;
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* @internal
|
|
75
|
-
*/
|
|
76
|
-
protected readonly _toCanonical?: Normalizer<T, TC>;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Constructs new {@link Utils.ValidationHelpers | validation helpers}
|
|
80
|
-
* from supplied initializers.
|
|
81
|
-
* @param init - The {@link Utils.ValidationHelpersConstructorParams | constructor params}
|
|
82
|
-
* used to initialize this {@link Utils.ValidationHelpers | validation helpers}.
|
|
83
|
-
*/
|
|
84
|
-
public constructor(init: IValidationHelpersConstructorParams<T, TC>) {
|
|
85
|
-
this.description = init.description;
|
|
86
|
-
this.isWellFormed = init.isWellFormed;
|
|
87
|
-
this.isCanonical = init.isCanonical;
|
|
88
|
-
this._toCanonical = init.toCanonical;
|
|
89
|
-
this.converter = Converters.isA(this.description, this.isWellFormed);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Converts a supplied `unknown` to the canonical form of the tag
|
|
94
|
-
* validated by these helpers.
|
|
95
|
-
* @param from - The `unknown` to be converted.
|
|
96
|
-
* @param context - Optional context used in the conversion.
|
|
97
|
-
* @returns `Success` with the corresponding canonical value,
|
|
98
|
-
* or `Failure` with details if an error occurs.
|
|
99
|
-
*/
|
|
100
|
-
public toCanonical(from: unknown, context?: TC): Result<T> {
|
|
101
|
-
if (this.isWellFormed(from, context)) {
|
|
102
|
-
if (this._toCanonical) {
|
|
103
|
-
return this._toCanonical(from as T, context);
|
|
104
|
-
}
|
|
105
|
-
if (this.isCanonical(from, context)) {
|
|
106
|
-
return succeed(from);
|
|
107
|
-
}
|
|
108
|
-
/* c8 ignore next 2 */
|
|
109
|
-
return fail(`cannot convert "${from}" to canonical ${this.description}`);
|
|
110
|
-
}
|
|
111
|
-
return fail(`invalid ${this.description} ("${JSON.stringify(from)}")`);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Determines if a supplied `unknown` is a well-formed representation
|
|
116
|
-
* of the tag validated by these helpers.
|
|
117
|
-
* @param from - The `unknown` to be validated.
|
|
118
|
-
* @param context - Optional context used in the validation.
|
|
119
|
-
* @returns `Success` with the validated value, or `Failure` with details
|
|
120
|
-
* if an error occurs.
|
|
121
|
-
*/
|
|
122
|
-
public verifyIsWellFormed(from: unknown, context?: TC): Result<T> {
|
|
123
|
-
if (this.isWellFormed(from, context)) {
|
|
124
|
-
return succeed(from);
|
|
125
|
-
}
|
|
126
|
-
return fail(`malformed ${this.description}`);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Determines if a supplied `unknown` is a well-formed, canonical representation
|
|
131
|
-
* of the tag validated by these helpers.
|
|
132
|
-
* @param from - The `unknown` to be validated.
|
|
133
|
-
* @param context - Optional context used in the validation.
|
|
134
|
-
* @returns `Success` with the validated canonical value, or `Failure` with
|
|
135
|
-
* details if an error occurs.
|
|
136
|
-
*/
|
|
137
|
-
public verifyIsCanonical(from: unknown, context?: TC): Result<T> {
|
|
138
|
-
if (this.isCanonical(from, context)) {
|
|
139
|
-
return succeed(from);
|
|
140
|
-
}
|
|
141
|
-
/* c8 ignore next 3 - should never occur and very hard to test */
|
|
142
|
-
if (!this.isWellFormed(from, context)) {
|
|
143
|
-
return fail(`malformed ${this.description}`);
|
|
144
|
-
}
|
|
145
|
-
return fail(`non-canonical ${this.description}`);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* @internal
|
|
151
|
-
*/
|
|
152
|
-
export interface IRegExpValidationHelperConstructorParams<T extends string, TC = unknown> {
|
|
153
|
-
description: string;
|
|
154
|
-
wellFormed: RegExp;
|
|
155
|
-
canonical: RegExp;
|
|
156
|
-
toCanonical?: Normalizer<T, TC>;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* @internal
|
|
161
|
-
*/
|
|
162
|
-
export class RegExpValidationHelpers<T extends string, TC = unknown> extends ValidationHelpers<T, TC> {
|
|
163
|
-
public readonly wellFormed: RegExp;
|
|
164
|
-
public readonly canonical: RegExp;
|
|
165
|
-
|
|
166
|
-
public constructor(params: IRegExpValidationHelperConstructorParams<T, TC>) {
|
|
167
|
-
super({
|
|
168
|
-
description: params.description,
|
|
169
|
-
toCanonical: params.toCanonical,
|
|
170
|
-
isWellFormed: (from: unknown): from is T => {
|
|
171
|
-
return typeof from === 'string' && this.wellFormed.test(from);
|
|
172
|
-
},
|
|
173
|
-
isCanonical: (from: unknown): from is T => {
|
|
174
|
-
return typeof from === 'string' && this.canonical.test(from);
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
this.wellFormed = params.wellFormed;
|
|
178
|
-
this.canonical = params.canonical;
|
|
179
|
-
}
|
|
180
|
-
}
|