@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.
- package/dist/data/bcp/overrides.json +21 -0
- package/dist/data/unsd/m49.json +3723 -0
- package/dist/index.browser.js +30 -0
- package/dist/index.js +28 -0
- package/dist/packlets/bcp47/bcp47Subtags/converters.js +32 -0
- package/dist/packlets/bcp47/bcp47Subtags/index.js +26 -0
- package/dist/packlets/bcp47/bcp47Subtags/model.js +23 -0
- package/dist/packlets/bcp47/bcp47Subtags/validate.js +48 -0
- package/dist/packlets/bcp47/common.js +53 -0
- package/dist/packlets/bcp47/helpers.js +96 -0
- package/dist/packlets/bcp47/index.js +32 -0
- package/dist/packlets/bcp47/languageRegistryData.js +202 -0
- package/dist/packlets/bcp47/languageTag.js +363 -0
- package/dist/packlets/bcp47/languageTagParser.js +275 -0
- package/dist/packlets/bcp47/match/chooser.js +88 -0
- package/dist/packlets/bcp47/match/common.js +49 -0
- package/dist/packlets/bcp47/match/index.js +26 -0
- package/dist/packlets/bcp47/match/similarity.js +205 -0
- package/dist/packlets/bcp47/normalization/baseNormalizer.js +89 -0
- package/dist/packlets/bcp47/normalization/canonicalNormalizer.js +86 -0
- package/dist/packlets/bcp47/normalization/common.js +77 -0
- package/dist/packlets/bcp47/normalization/index.js +27 -0
- package/dist/packlets/bcp47/normalization/normalizeTag.js +101 -0
- package/dist/packlets/bcp47/normalization/preferredTagNormalizer.js +177 -0
- package/dist/packlets/bcp47/overrides/converters.js +49 -0
- package/dist/packlets/bcp47/overrides/defaultRegistries.js +38 -0
- package/dist/packlets/bcp47/overrides/index.js +25 -0
- package/dist/packlets/bcp47/overrides/model.js +23 -0
- package/dist/packlets/bcp47/overrides/overridesRegistry.js +83 -0
- package/dist/packlets/bcp47/validation/baseValidator.js +86 -0
- package/dist/packlets/bcp47/validation/common.js +77 -0
- package/dist/packlets/bcp47/validation/index.js +29 -0
- package/dist/packlets/bcp47/validation/isCanonical.js +79 -0
- package/dist/packlets/bcp47/validation/isInPreferredForm.js +46 -0
- package/dist/packlets/bcp47/validation/isStrictlyValid.js +94 -0
- package/dist/packlets/bcp47/validation/isValid.js +92 -0
- package/dist/packlets/bcp47/validation/isWellFormed.js +75 -0
- package/dist/packlets/bcp47/validation/validateTag.js +153 -0
- package/dist/packlets/iana/common/converters.js +58 -0
- package/dist/packlets/iana/common/model.js +23 -0
- package/dist/packlets/iana/common/registeredItems.js +120 -0
- package/dist/packlets/iana/common/utils.js +30 -0
- package/dist/packlets/iana/common/validate.js +59 -0
- package/dist/packlets/iana/converters.js +25 -0
- package/dist/packlets/iana/defaultRegistries.js +38 -0
- package/dist/packlets/iana/iana-data-embedded.js +60 -0
- package/dist/packlets/iana/index.browser.js +34 -0
- package/dist/packlets/iana/index.js +34 -0
- package/dist/packlets/iana/jar/converters.js +25 -0
- package/dist/packlets/iana/jar/index.js +26 -0
- package/dist/packlets/iana/jar/jarConverters.js +60 -0
- package/dist/packlets/iana/jar/jarModel.js +23 -0
- package/dist/packlets/iana/jar/language-subtags/converters.js +25 -0
- package/dist/packlets/iana/jar/language-subtags/index.js +26 -0
- package/dist/packlets/iana/jar/language-subtags/model.js +25 -0
- package/dist/packlets/iana/jar/language-subtags/registry/converters.js +180 -0
- package/dist/packlets/iana/jar/language-subtags/registry/index.js +26 -0
- package/dist/packlets/iana/jar/language-subtags/registry/model.js +43 -0
- package/dist/packlets/iana/jar/language-subtags/tags/converters.js +101 -0
- package/dist/packlets/iana/jar/language-subtags/tags/index.js +27 -0
- package/dist/packlets/iana/jar/language-subtags/tags/model.js +23 -0
- package/dist/packlets/iana/jar/language-subtags/tags/tagValidation.js +66 -0
- package/dist/packlets/iana/jar/language-subtags/tags/validate.js +85 -0
- package/dist/packlets/iana/jar/model.js +25 -0
- package/dist/packlets/iana/language-subtags/common.js +23 -0
- package/dist/packlets/iana/language-subtags/converters.js +182 -0
- package/dist/packlets/iana/language-subtags/index.browser.js +30 -0
- package/dist/packlets/iana/language-subtags/index.js +29 -0
- package/dist/packlets/iana/language-subtags/jarConverters.js +288 -0
- package/dist/packlets/iana/language-subtags/model.js +23 -0
- package/dist/packlets/iana/language-subtags/scope.js +169 -0
- package/dist/packlets/iana/language-subtags/subtagRegistry.js +108 -0
- package/dist/packlets/iana/language-subtags/validate.js +23 -0
- package/dist/packlets/iana/language-tag-extensions/converters.js +59 -0
- package/dist/packlets/iana/language-tag-extensions/extensionsRegistry.js +64 -0
- package/dist/packlets/iana/language-tag-extensions/extensionsScope.js +50 -0
- package/dist/packlets/iana/language-tag-extensions/index.js +28 -0
- package/dist/packlets/iana/language-tag-extensions/jarConverters.js +143 -0
- package/dist/packlets/iana/language-tag-extensions/model.js +24 -0
- package/dist/packlets/iana/language-tag-extensions/validate.js +33 -0
- package/dist/packlets/iana/languageRegistries.js +80 -0
- package/dist/packlets/iana/languageRegistriesFileLoader.js +73 -0
- package/dist/packlets/iana/languageRegistriesLoader.js +113 -0
- package/dist/packlets/iana/model.js +25 -0
- package/dist/packlets/iana/validate.js +23 -0
- package/dist/packlets/unsd/areas.js +92 -0
- package/dist/packlets/unsd/common.js +23 -0
- package/dist/packlets/unsd/csv/converters.js +75 -0
- package/dist/packlets/unsd/csv/index.js +25 -0
- package/dist/packlets/unsd/csv/model.js +23 -0
- package/dist/packlets/unsd/defaultRegistries.js +38 -0
- package/dist/packlets/unsd/index.js +27 -0
- package/dist/packlets/unsd/regionCodes.js +115 -0
- package/dist/packlets/unsd/regions.js +71 -0
- package/dist/packlets/utils/index.js +24 -0
- package/dist/packlets/utils/jsonHelpers.js +25 -0
- package/dist/packlets/utils/public.js +24 -0
- package/dist/packlets/utils/validationHelpers.js +116 -0
- package/dist/test/unit/bcp47/commonTestCases.js +475 -0
- package/dist/test/unit/bcp47/languageTagHelpers.js +178 -0
- package/dist/test/unit/iana/testConstants.js +68 -0
- package/dist/tsdoc-metadata.json +1 -1
- package/package.json +36 -31
|
@@ -0,0 +1,115 @@
|
|
|
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 { allSucceed, succeed } from '@fgv/ts-utils';
|
|
23
|
+
import { loadM49csvFileSync, m49CsvFile } from './csv/converters';
|
|
24
|
+
// eslint-disable-next-line @rushstack/packlets/mechanics
|
|
25
|
+
import defaultRegions from '../../data/unsd/m49.json';
|
|
26
|
+
import { Areas } from './areas';
|
|
27
|
+
import { Regions } from './regions';
|
|
28
|
+
/**
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export class RegionCodes {
|
|
32
|
+
/**
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
constructor() {
|
|
36
|
+
this.regions = new Regions();
|
|
37
|
+
this.areas = new Areas();
|
|
38
|
+
}
|
|
39
|
+
static create(rows) {
|
|
40
|
+
const codes = new RegionCodes();
|
|
41
|
+
return codes._importRows(rows).onSuccess(() => {
|
|
42
|
+
return succeed(codes);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
static createFromJson(from) {
|
|
46
|
+
return m49CsvFile.convert(from).onSuccess(RegionCodes.create);
|
|
47
|
+
}
|
|
48
|
+
static loadDefault() {
|
|
49
|
+
return this.createFromJson(defaultRegions);
|
|
50
|
+
}
|
|
51
|
+
static loadCsv(path) {
|
|
52
|
+
return loadM49csvFileSync(path).onSuccess((rows) => {
|
|
53
|
+
return this.create(rows);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
tryGetRegionOrArea(code) {
|
|
57
|
+
var _a;
|
|
58
|
+
/* c8 ignore next - numeric area not allowed in bcp-47 so right side of ?? shouldn't be hit. */
|
|
59
|
+
return (_a = this.regions.tryGetRegion(code)) !== null && _a !== void 0 ? _a : this.areas.tryGetArea(code);
|
|
60
|
+
}
|
|
61
|
+
getIsContained(container, contained) {
|
|
62
|
+
let next = contained;
|
|
63
|
+
while (next) {
|
|
64
|
+
if (next === container) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
next = next.tier !== 'global' ? next.parent : undefined;
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Imports a single parsed row of UN M.49 region code data
|
|
73
|
+
* @param row - The parsed row to be imported.
|
|
74
|
+
* @returns `Success` with `true` if the row was successfully
|
|
75
|
+
* imported, or `Failure` with details if an error occurs.
|
|
76
|
+
* @internal
|
|
77
|
+
*/
|
|
78
|
+
_importRow(row) {
|
|
79
|
+
return this.regions
|
|
80
|
+
.getOrAddRegionChildRegion('region', this.regions.global, row.regionCode, row.regionName)
|
|
81
|
+
.onSuccess((region) => {
|
|
82
|
+
return this.regions.getOrAddRegionChildRegion('subRegion', region, row.subRegionCode, row.subRegionName);
|
|
83
|
+
})
|
|
84
|
+
.onSuccess((region) => {
|
|
85
|
+
return this.regions.getOrAddRegionChildRegion('intermediateRegion', region, row.intermediateRegionCode, row.intermediateRegionName);
|
|
86
|
+
})
|
|
87
|
+
.onSuccess((region) => {
|
|
88
|
+
return this.areas.addArea({
|
|
89
|
+
name: row.countryOrArea,
|
|
90
|
+
code: row.m49Code,
|
|
91
|
+
tier: 'area',
|
|
92
|
+
parent: region,
|
|
93
|
+
isoAlpha2: row.isoAlpha2RegionCode,
|
|
94
|
+
isoAlpha3: row.isoAlpha3RegionCode,
|
|
95
|
+
leastDevelopedCountry: row.leastDevelopedCountry,
|
|
96
|
+
landlockedDevelopingCountry: row.landLockedDevelopingCountry,
|
|
97
|
+
smallIslandDevelopingState: row.smallIslandDevelopingState
|
|
98
|
+
});
|
|
99
|
+
})
|
|
100
|
+
.onSuccess(() => {
|
|
101
|
+
return succeed(true);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Imports multiple parsed rows from UN M.49 region code data
|
|
106
|
+
* @param rows - The parsed rows to be imported.
|
|
107
|
+
* @returns `Success` with `true` if the rows were successfully
|
|
108
|
+
* imported, or `Failure` with details if an error occurs.
|
|
109
|
+
* @internal
|
|
110
|
+
*/
|
|
111
|
+
_importRows(rows) {
|
|
112
|
+
return allSucceed(rows.map((row) => this._importRow(row)), true);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=regionCodes.js.map
|
|
@@ -0,0 +1,71 @@
|
|
|
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 { fail, succeed } from '@fgv/ts-utils';
|
|
23
|
+
export const GlobalRegionName = 'World';
|
|
24
|
+
export const GlobalRegionCode = '001';
|
|
25
|
+
/**
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export class Regions {
|
|
29
|
+
constructor() {
|
|
30
|
+
this.global = { name: GlobalRegionName, code: GlobalRegionCode, tier: 'global', regions: [], areas: [] };
|
|
31
|
+
this._regions = new Map();
|
|
32
|
+
}
|
|
33
|
+
tryGetRegion(from) {
|
|
34
|
+
return this._regions.get(from);
|
|
35
|
+
}
|
|
36
|
+
getRegion(from) {
|
|
37
|
+
const got = this._regions.get(from);
|
|
38
|
+
return got ? succeed(got) : fail(`${from}: region not found`);
|
|
39
|
+
}
|
|
40
|
+
getOrAddRegionChildRegion(tier, parent, code, name) {
|
|
41
|
+
if (!code && !name) {
|
|
42
|
+
return succeed(parent);
|
|
43
|
+
}
|
|
44
|
+
else if (!code || !name) {
|
|
45
|
+
return fail(`${code}(${name}): code and name must both be present`);
|
|
46
|
+
}
|
|
47
|
+
const existing = this._regions.get(code);
|
|
48
|
+
if (existing) {
|
|
49
|
+
if (existing.name !== name) {
|
|
50
|
+
return fail(`${code}: cannot add ${name} - already exists as ${existing.name}`);
|
|
51
|
+
}
|
|
52
|
+
if (existing.tier !== tier) {
|
|
53
|
+
return fail(`${code}: cannot add ${name} as tier ${tier} - already exists as tier ${existing.tier}`);
|
|
54
|
+
}
|
|
55
|
+
if (existing.parent !== parent) {
|
|
56
|
+
const haveParent = `${existing.parent.code}/${existing.parent.name}`;
|
|
57
|
+
const wantParent = `${parent.code}/${parent.name}`;
|
|
58
|
+
return fail(`${code}: cannot add ${name} with parent ${wantParent} - already exists with parent ${haveParent}`);
|
|
59
|
+
}
|
|
60
|
+
return succeed(existing);
|
|
61
|
+
}
|
|
62
|
+
const added = { name, code, tier, parent, regions: [], areas: [] };
|
|
63
|
+
this._regions.set(code, added);
|
|
64
|
+
parent.regions.push(added);
|
|
65
|
+
return succeed(added);
|
|
66
|
+
}
|
|
67
|
+
getAll() {
|
|
68
|
+
return Array.from(this._regions.values());
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=regions.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
export * from './jsonHelpers';
|
|
23
|
+
export * from './validationHelpers';
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
export function sanitizeJson(from) {
|
|
23
|
+
return JSON.parse(JSON.stringify(from));
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=jsonHelpers.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
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 { ValidationHelpers } from './validationHelpers';
|
|
24
|
+
//# sourceMappingURL=public.js.map
|
|
@@ -0,0 +1,116 @@
|
|
|
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
|
+
import { Converters, fail, succeed } from '@fgv/ts-utils';
|
|
23
|
+
/**
|
|
24
|
+
A collection of validation and normalization helpers for constrained string
|
|
25
|
+
types.
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export class ValidationHelpers {
|
|
29
|
+
/**
|
|
30
|
+
* Constructs new {@link Utils.ValidationHelpers | validation helpers}
|
|
31
|
+
* from supplied initializers.
|
|
32
|
+
* @param init - The {@link Utils.ValidationHelpersConstructorParams | constructor params}
|
|
33
|
+
* used to initialize this {@link Utils.ValidationHelpers | validation helpers}.
|
|
34
|
+
*/
|
|
35
|
+
constructor(init) {
|
|
36
|
+
this.description = init.description;
|
|
37
|
+
this.isWellFormed = init.isWellFormed;
|
|
38
|
+
this.isCanonical = init.isCanonical;
|
|
39
|
+
this._toCanonical = init.toCanonical;
|
|
40
|
+
this.converter = Converters.isA(this.description, this.isWellFormed);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Converts a supplied `unknown` to the canonical form of the tag
|
|
44
|
+
* validated by these helpers.
|
|
45
|
+
* @param from - The `unknown` to be converted.
|
|
46
|
+
* @param context - Optional context used in the conversion.
|
|
47
|
+
* @returns `Success` with the corresponding canonical value,
|
|
48
|
+
* or `Failure` with details if an error occurs.
|
|
49
|
+
*/
|
|
50
|
+
toCanonical(from, context) {
|
|
51
|
+
if (this.isWellFormed(from, context)) {
|
|
52
|
+
if (this._toCanonical) {
|
|
53
|
+
return this._toCanonical(from, context);
|
|
54
|
+
}
|
|
55
|
+
/* c8 ignore next 3 - functional code tested but coverage intermittently missed */
|
|
56
|
+
if (this.isCanonical(from, context)) {
|
|
57
|
+
return succeed(from);
|
|
58
|
+
}
|
|
59
|
+
/* c8 ignore next 2 */
|
|
60
|
+
return fail(`cannot convert "${from}" to canonical ${this.description}`);
|
|
61
|
+
}
|
|
62
|
+
return fail(`invalid ${this.description} ("${JSON.stringify(from)}")`);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Determines if a supplied `unknown` is a well-formed representation
|
|
66
|
+
* of the tag validated by these helpers.
|
|
67
|
+
* @param from - The `unknown` to be validated.
|
|
68
|
+
* @param context - Optional context used in the validation.
|
|
69
|
+
* @returns `Success` with the validated value, or `Failure` with details
|
|
70
|
+
* if an error occurs.
|
|
71
|
+
*/
|
|
72
|
+
verifyIsWellFormed(from, context) {
|
|
73
|
+
if (this.isWellFormed(from, context)) {
|
|
74
|
+
return succeed(from);
|
|
75
|
+
}
|
|
76
|
+
return fail(`malformed ${this.description}`);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Determines if a supplied `unknown` is a well-formed, canonical representation
|
|
80
|
+
* of the tag validated by these helpers.
|
|
81
|
+
* @param from - The `unknown` to be validated.
|
|
82
|
+
* @param context - Optional context used in the validation.
|
|
83
|
+
* @returns `Success` with the validated canonical value, or `Failure` with
|
|
84
|
+
* details if an error occurs.
|
|
85
|
+
*/
|
|
86
|
+
verifyIsCanonical(from, context) {
|
|
87
|
+
if (this.isCanonical(from, context)) {
|
|
88
|
+
return succeed(from);
|
|
89
|
+
}
|
|
90
|
+
/* c8 ignore next 3 - should never occur and very hard to test */
|
|
91
|
+
if (!this.isWellFormed(from, context)) {
|
|
92
|
+
return fail(`malformed ${this.description}`);
|
|
93
|
+
}
|
|
94
|
+
return fail(`non-canonical ${this.description}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
export class RegExpValidationHelpers extends ValidationHelpers {
|
|
101
|
+
constructor(params) {
|
|
102
|
+
super({
|
|
103
|
+
description: params.description,
|
|
104
|
+
toCanonical: params.toCanonical,
|
|
105
|
+
isWellFormed: (from) => {
|
|
106
|
+
return typeof from === 'string' && this.wellFormed.test(from);
|
|
107
|
+
},
|
|
108
|
+
isCanonical: (from) => {
|
|
109
|
+
return typeof from === 'string' && this.canonical.test(from);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
this.wellFormed = params.wellFormed;
|
|
113
|
+
this.canonical = params.canonical;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=validationHelpers.js.map
|