urlmason 1.0.0 → 1.1.0
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/README.md +327 -40
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +1 -1
- package/dist/locales.json.mjs +22 -5
- package/dist/node_modules/trousse/dist/arrays.mjs +8 -3
- package/dist/node_modules/trousse/dist/coercions.mjs +7 -0
- package/dist/node_modules/trousse/dist/internal/blank.mjs +3 -0
- package/dist/node_modules/trousse/dist/internal.mjs +16 -0
- package/dist/node_modules/trousse/dist/is.mjs +15 -1
- package/dist/node_modules/trousse/dist/matching.mjs +36 -19
- package/dist/node_modules/trousse/dist/objects.mjs +10 -3
- package/dist/node_modules/trousse/dist/strings.mjs +14 -0
- package/dist/node_modules/trousse/dist/urls.mjs +38 -13
- package/dist/rules.mjs +257 -0
- package/dist/tables.d.mts +3 -2
- package/dist/tables.mjs +1011 -381
- package/dist/templates.mjs +625 -220
- package/dist/types.d.mts +49 -29
- package/dist/utils/escapes.mjs +55 -0
- package/dist/utils/hosts.mjs +7 -0
- package/dist/utils/paths.mjs +37 -0
- package/dist/utils/queries.mjs +89 -0
- package/dist/utils/regexes.mjs +44 -0
- package/dist/utils/texts.mjs +6 -0
- package/package.json +3 -3
- package/dist/queries.mjs +0 -92
|
@@ -1,16 +1,21 @@
|
|
|
1
|
+
import { toPatternList } from "./internal.mjs";
|
|
1
2
|
import { isNullish, isString } from "./is.mjs";
|
|
3
|
+
import { trimString } from "./strings.mjs";
|
|
2
4
|
// node_modules/trousse/dist/matching.js
|
|
3
5
|
const regexSyntaxCharsRegex = /[.*+?^${}()|[\]\\/]/;
|
|
4
6
|
const regexEscapableCharsRegex = /[.*+?^${}()|[\]\\/&!#%,:;<=>@`~-]/g;
|
|
7
|
+
const foldText = (value) => {
|
|
8
|
+
return (trimString(value) ?? "").toLowerCase();
|
|
9
|
+
};
|
|
5
10
|
const testRegex = (regex, value) => {
|
|
6
11
|
regex.lastIndex = 0;
|
|
7
12
|
return regex.test(value);
|
|
8
13
|
};
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
return
|
|
14
|
+
const testRegexPattern = (regex, parsedValue, value, parser) => {
|
|
15
|
+
if (testRegex(regex, parsedValue)) return true;
|
|
16
|
+
if (parser !== foldText) return false;
|
|
17
|
+
const originalValue = trimString(value) ?? "";
|
|
18
|
+
return originalValue !== parsedValue && testRegex(regex, originalValue);
|
|
14
19
|
};
|
|
15
20
|
const escapeRegex = (value) => {
|
|
16
21
|
return value.replace(regexEscapableCharsRegex, (char) => {
|
|
@@ -18,35 +23,47 @@ const escapeRegex = (value) => {
|
|
|
18
23
|
return `\\x${char.charCodeAt(0).toString(16)}`;
|
|
19
24
|
});
|
|
20
25
|
};
|
|
21
|
-
const isAnyOf = (value, patterns, parser) => {
|
|
26
|
+
const isAnyOf = (value, patterns, parser = foldText) => {
|
|
22
27
|
if (isNullish(value)) return false;
|
|
23
|
-
const parsedValue = parser
|
|
24
|
-
if (isString(patterns)) return parsedValue === (
|
|
25
|
-
let originalValue;
|
|
28
|
+
const parsedValue = parser(value);
|
|
29
|
+
if (isString(patterns)) return parsedValue === parser(patterns);
|
|
26
30
|
for (const pattern of toPatternList(patterns)) {
|
|
27
31
|
if (pattern instanceof RegExp) {
|
|
28
|
-
if (
|
|
29
|
-
if (testRegexAnyCase(pattern, parsedValue, originalValue)) return true;
|
|
32
|
+
if (testRegexPattern(pattern, parsedValue, value, parser)) return true;
|
|
30
33
|
continue;
|
|
31
34
|
}
|
|
32
|
-
if (parsedValue === (
|
|
35
|
+
if (parsedValue === parser(pattern)) return true;
|
|
33
36
|
}
|
|
34
37
|
return false;
|
|
35
38
|
};
|
|
36
|
-
const
|
|
39
|
+
const isLooseMatch = (value, other) => {
|
|
37
40
|
if (isNullish(value) || isNullish(other)) return false;
|
|
38
|
-
return value
|
|
41
|
+
return foldText(value) === foldText(other);
|
|
42
|
+
};
|
|
43
|
+
const startsWithAnyOf = (value, patterns, parser = foldText) => {
|
|
44
|
+
if (isNullish(value)) return false;
|
|
45
|
+
const parsedValue = parser(value);
|
|
46
|
+
for (const pattern of toPatternList(patterns)) {
|
|
47
|
+
if (pattern instanceof RegExp) {
|
|
48
|
+
if (testRegexPattern(pattern, parsedValue, value, parser)) return true;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const parsedPattern = parser(pattern);
|
|
52
|
+
if (parsedPattern && parsedValue.startsWith(parsedPattern)) return true;
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
39
55
|
};
|
|
40
|
-
const
|
|
56
|
+
const endsWithAnyOf = (value, patterns, parser = foldText) => {
|
|
41
57
|
if (isNullish(value)) return false;
|
|
42
|
-
const
|
|
58
|
+
const parsedValue = parser(value);
|
|
43
59
|
for (const pattern of toPatternList(patterns)) {
|
|
44
60
|
if (pattern instanceof RegExp) {
|
|
45
|
-
if (
|
|
61
|
+
if (testRegexPattern(pattern, parsedValue, value, parser)) return true;
|
|
46
62
|
continue;
|
|
47
63
|
}
|
|
48
|
-
|
|
64
|
+
const parsedPattern = parser(pattern);
|
|
65
|
+
if (parsedPattern && parsedValue.endsWith(parsedPattern)) return true;
|
|
49
66
|
}
|
|
50
67
|
return false;
|
|
51
68
|
};
|
|
52
|
-
export { escapeRegex, isAnyOf,
|
|
69
|
+
export { endsWithAnyOf, escapeRegex, isAnyOf, isLooseMatch, startsWithAnyOf };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createTrimPredicate } from "./internal.mjs";
|
|
2
|
+
import { isFunction, isPresent } from "./is.mjs";
|
|
2
3
|
// node_modules/trousse/dist/objects.js
|
|
3
4
|
const setKey = (object, key, value) => {
|
|
4
5
|
if (key === "__proto__") {
|
|
@@ -12,7 +13,13 @@ const setKey = (object, key, value) => {
|
|
|
12
13
|
}
|
|
13
14
|
object[key] = value;
|
|
14
15
|
};
|
|
15
|
-
const
|
|
16
|
+
const omit = (object, keys) => {
|
|
17
|
+
const result = { ...object };
|
|
18
|
+
for (const key of keys) delete result[key];
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
const trimObject = (object, options = {}) => {
|
|
22
|
+
const keep = createTrimPredicate(isFunction(options) ? { keep: options } : options, isPresent);
|
|
16
23
|
let hasKept = false;
|
|
17
24
|
let hasDropped = false;
|
|
18
25
|
for (const key in object) {
|
|
@@ -29,4 +36,4 @@ const trimObject = (object, keep = isPresent) => {
|
|
|
29
36
|
}
|
|
30
37
|
return result;
|
|
31
38
|
};
|
|
32
|
-
export { trimObject };
|
|
39
|
+
export { omit, setKey, trimObject };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { blankCharacterClass } from "./internal/blank.mjs";
|
|
2
|
+
import { isString } from "./is.mjs";
|
|
3
|
+
// node_modules/trousse/dist/strings.js
|
|
4
|
+
const blankCharRegex = new RegExp(blankCharacterClass, "u");
|
|
5
|
+
const trimString = (value) => {
|
|
6
|
+
if (!isString(value)) return;
|
|
7
|
+
let start = 0;
|
|
8
|
+
let end = value.length;
|
|
9
|
+
while (start < end && blankCharRegex.test(value.charAt(start))) start++;
|
|
10
|
+
while (end > start && blankCharRegex.test(value.charAt(end - 1))) end--;
|
|
11
|
+
if (start === end) return;
|
|
12
|
+
return value.slice(start, end);
|
|
13
|
+
};
|
|
14
|
+
export { trimString };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { toPatternList } from "./internal.mjs";
|
|
1
2
|
import { isNullish, isString } from "./is.mjs";
|
|
2
|
-
import { isAnyOf
|
|
3
|
+
import { isAnyOf } from "./matching.mjs";
|
|
3
4
|
// node_modules/trousse/dist/urls.js
|
|
4
5
|
const httpSchemes = ["http:", "https:"];
|
|
5
6
|
const parseUrl = (value, base) => {
|
|
@@ -23,6 +24,17 @@ const normalizeDomain = (domain) => {
|
|
|
23
24
|
return parseUrl(`http://${bare}`)?.hostname ?? bare;
|
|
24
25
|
};
|
|
25
26
|
const keepValue = (value) => value;
|
|
27
|
+
const lowerDomainCacheLimit = 1e3;
|
|
28
|
+
const lowerDomainCache = /* @__PURE__ */ new Map();
|
|
29
|
+
const toLowerDomain = (domain) => {
|
|
30
|
+
let lowerDomain = lowerDomainCache.get(domain);
|
|
31
|
+
if (lowerDomain === void 0) {
|
|
32
|
+
lowerDomain = normalizeDomain(domain).toLowerCase();
|
|
33
|
+
if (lowerDomainCache.size >= lowerDomainCacheLimit) lowerDomainCache.clear();
|
|
34
|
+
lowerDomainCache.set(domain, lowerDomain);
|
|
35
|
+
}
|
|
36
|
+
return lowerDomain;
|
|
37
|
+
};
|
|
26
38
|
const isHostOf = (url, hosts) => {
|
|
27
39
|
const hostname = getHostname(url);
|
|
28
40
|
if (!hostname) return false;
|
|
@@ -32,7 +44,7 @@ const isHostOf = (url, hosts) => {
|
|
|
32
44
|
if (isAnyOf(hostname, host)) return true;
|
|
33
45
|
continue;
|
|
34
46
|
}
|
|
35
|
-
if (isAnyOf(lowerHostname,
|
|
47
|
+
if (isAnyOf(lowerHostname, toLowerDomain(host), keepValue)) return true;
|
|
36
48
|
}
|
|
37
49
|
return false;
|
|
38
50
|
};
|
|
@@ -41,6 +53,8 @@ const isHttpUrl = (url) => {
|
|
|
41
53
|
if (!scheme) return false;
|
|
42
54
|
return httpSchemes.includes(scheme);
|
|
43
55
|
};
|
|
56
|
+
const c0ControlOrSpaceClass = "[\\x00-\\x20]+";
|
|
57
|
+
new RegExp(`^${c0ControlOrSpaceClass}|${c0ControlOrSpaceClass}$`, "g");
|
|
44
58
|
const escapeRunRegex = /(?:%[0-9A-Fa-f]{2})+/g;
|
|
45
59
|
const getSequenceLength = (byte) => {
|
|
46
60
|
if (byte < 128) return 1;
|
|
@@ -72,11 +86,24 @@ const decodeEscapeRun = (run) => {
|
|
|
72
86
|
const decodePercent = (value) => {
|
|
73
87
|
return value.replace(escapeRunRegex, decodeEscapeRun);
|
|
74
88
|
};
|
|
89
|
+
const toPercentEscape = (character) => {
|
|
90
|
+
return `%${character.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}`;
|
|
91
|
+
};
|
|
92
|
+
const decodePathSegment = (segment) => {
|
|
93
|
+
return decodePercent(segment).replaceAll("%", "%25").replaceAll("/", "%2F");
|
|
94
|
+
};
|
|
95
|
+
const decodePath = (path) => {
|
|
96
|
+
if (!path.includes("%")) return path;
|
|
97
|
+
return path.split("/").map(decodePathSegment).join("/");
|
|
98
|
+
};
|
|
75
99
|
const pathSegmentEscapeRegex = /[/?#\\\s]/g;
|
|
76
100
|
const encodePathSegment = (value) => {
|
|
77
101
|
return value.replace(pathSegmentEscapeRegex, (character) => encodeURIComponent(character));
|
|
78
102
|
};
|
|
79
|
-
const
|
|
103
|
+
const decodeFormPart = (part) => {
|
|
104
|
+
return decodePercent(part.replaceAll("+", " "));
|
|
105
|
+
};
|
|
106
|
+
const readPairs = (text, prefix, options) => {
|
|
80
107
|
if (text === void 0) return;
|
|
81
108
|
const pairs = [];
|
|
82
109
|
const body = text.startsWith(prefix) ? text.slice(1) : text;
|
|
@@ -84,18 +111,19 @@ const readRawPairs = (text, prefix) => {
|
|
|
84
111
|
if (!pair) continue;
|
|
85
112
|
const separator = pair.indexOf("=");
|
|
86
113
|
if (separator === -1) {
|
|
87
|
-
pairs.push([
|
|
114
|
+
pairs.push([decodeFormPart(pair), ""]);
|
|
88
115
|
continue;
|
|
89
116
|
}
|
|
90
|
-
const name =
|
|
91
|
-
|
|
117
|
+
const name = decodeFormPart(pair.slice(0, separator));
|
|
118
|
+
const value = pair.slice(separator + 1);
|
|
119
|
+
pairs.push([name, options?.decodeValues === false ? value : decodeFormPart(value)]);
|
|
92
120
|
}
|
|
93
121
|
return pairs;
|
|
94
122
|
};
|
|
95
123
|
const readQuerySource = (source, options) => {
|
|
96
|
-
if (options?.decodeValues === false) return
|
|
97
|
-
if (source instanceof URL) return source.
|
|
98
|
-
if (isString(source)) return
|
|
124
|
+
if (options?.decodeValues === false) return readPairs(source instanceof URL ? source.search : source?.toString(), "?", options);
|
|
125
|
+
if (source instanceof URL) return readPairs(source.search, "?", options);
|
|
126
|
+
if (isString(source)) return readPairs(source, "?", options);
|
|
99
127
|
return source;
|
|
100
128
|
};
|
|
101
129
|
const readParamValues = (params, name, options) => {
|
|
@@ -112,9 +140,6 @@ const readParamValues = (params, name, options) => {
|
|
|
112
140
|
const getQueryParamValues = (source, name, options) => {
|
|
113
141
|
return readParamValues(readQuerySource(source, options), name, options);
|
|
114
142
|
};
|
|
115
|
-
const getQueryParam = (source, name, options) => {
|
|
116
|
-
return getQueryParamValues(source, name, options)[0];
|
|
117
|
-
};
|
|
118
143
|
const queryPartEscapeRegex = /[&#%+\s]/g;
|
|
119
144
|
const composePairs = (params) => {
|
|
120
145
|
const escapePart = (part) => {
|
|
@@ -215,4 +240,4 @@ const getRegistrableDomain = (url, options) => {
|
|
|
215
240
|
const labels = topLevel.length === 2 && genericSecondLevels.includes(secondLevel) ? 3 : 2;
|
|
216
241
|
return parts.slice(-labels).join(".");
|
|
217
242
|
};
|
|
218
|
-
export { composeQuery, decodePercent, encodePathSegment,
|
|
243
|
+
export { composeQuery, decodeFormPart, decodePath, decodePercent, encodePathSegment, getQueryParamValues, getRegistrableDomain, getSequenceLength, isHostOf, isHttpUrl, isIpAddress, parseUrl, stripWww, toPercentEscape };
|
package/dist/rules.mjs
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { t } from "./node_modules/trousse/dist/locales.mjs";
|
|
2
|
+
import { errors, nameShapes } from "./locales.json.mjs";
|
|
3
|
+
import { isMultiSegment, openTailSuffix, placeholderRegex, splitPath, toBareName, toValueParts } from "./templates.mjs";
|
|
4
|
+
import { nonAsciiLabelRegex } from "./utils/hosts.mjs";
|
|
5
|
+
import { dotSegmentRegex } from "./utils/paths.mjs";
|
|
6
|
+
import { entityPrefix } from "./utils/queries.mjs";
|
|
7
|
+
import { groupReferenceRegex } from "./utils/regexes.mjs";
|
|
8
|
+
// src/rules.ts
|
|
9
|
+
const keywords = ["base", "tail"];
|
|
10
|
+
const expressionRegex = /\{([^{}]*)\}/g;
|
|
11
|
+
const braceRegex = /[{}]/;
|
|
12
|
+
const oldListRegex = /(?<!\/)\/\{(\w*)\*\}/;
|
|
13
|
+
const plainExpressionRegex = /^(?:\w+|\/\w+\*)$/;
|
|
14
|
+
const optionalQueryExpressionRegex = /^[?&][\w-]+(?:,[\w-]+)*\*?$/;
|
|
15
|
+
const reservedExpressionRegex = /^\+\w+$/;
|
|
16
|
+
const reservedAnywhereRegex = /\{\+[^{}]*\}/;
|
|
17
|
+
const fragmentExpressionRegex = /^#\w+$/;
|
|
18
|
+
const fragmentRefusedRegex = /\{[/?&#]/;
|
|
19
|
+
const adjacentExpressionRegex = /\{(?<body>[^{}]*)\}(?=\{(?<next>[^{}]*)\})/g;
|
|
20
|
+
const partExpressionRegex = /^[?&#]/;
|
|
21
|
+
const operatorRegex = /^[/+#?&]/;
|
|
22
|
+
const hostTextRegex = /[:@]/;
|
|
23
|
+
const oldTail = "{*}";
|
|
24
|
+
const oldQuery = "?{query}";
|
|
25
|
+
const throwInvalidTemplate = (template) => {
|
|
26
|
+
throw new Error(t(errors.invalidTemplate, { template }));
|
|
27
|
+
};
|
|
28
|
+
const throwOldSpelling = (template, expression, spelling) => {
|
|
29
|
+
throw new Error(t(errors.oldTemplateSpelling, {
|
|
30
|
+
template,
|
|
31
|
+
expression,
|
|
32
|
+
spelling
|
|
33
|
+
}));
|
|
34
|
+
};
|
|
35
|
+
const validateOldSpellings = (template) => {
|
|
36
|
+
const oldList = oldListRegex.exec(template);
|
|
37
|
+
if (oldList) throwOldSpelling(template, oldList[0], `{/${oldList[1] || "tail"}*}`);
|
|
38
|
+
if (template.includes(oldQuery)) throwOldSpelling(template, oldQuery, "{?query*}");
|
|
39
|
+
};
|
|
40
|
+
const validateBraces = (template) => {
|
|
41
|
+
if (braceRegex.test(template.replace(expressionRegex, ""))) throw new Error(t(errors.unbalancedBrace, { template }));
|
|
42
|
+
};
|
|
43
|
+
const validateExpressions = (template) => {
|
|
44
|
+
for (const [expression, body] of template.matchAll(expressionRegex)) {
|
|
45
|
+
if (expression === oldTail) throwOldSpelling(template, oldTail, openTailSuffix);
|
|
46
|
+
if (plainExpressionRegex.test(body) || optionalQueryExpressionRegex.test(body)) continue;
|
|
47
|
+
if (reservedExpressionRegex.test(body) || fragmentExpressionRegex.test(body)) continue;
|
|
48
|
+
throw new Error(t(errors.unsupportedTemplateExpression, {
|
|
49
|
+
template,
|
|
50
|
+
expression
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const validateFragment = (template, body, fragment) => {
|
|
55
|
+
const isFragmentRefused = fragment !== void 0 && (!fragment || fragmentRefusedRegex.test(fragment));
|
|
56
|
+
const isCutExpression = braceRegex.test(body.replace(expressionRegex, ""));
|
|
57
|
+
if (isFragmentRefused || isCutExpression || body.includes("#")) throwInvalidTemplate(template);
|
|
58
|
+
};
|
|
59
|
+
const validateAdjacentValues = (template) => {
|
|
60
|
+
for (const match of template.matchAll(adjacentExpressionRegex)) {
|
|
61
|
+
const [expression] = match;
|
|
62
|
+
const { body, next } = match.groups ?? {};
|
|
63
|
+
if (partExpressionRegex.test(next)) continue;
|
|
64
|
+
const isSlashTaken = body.startsWith("+") || body.startsWith("/");
|
|
65
|
+
if (next.startsWith("/") && (!isSlashTaken || expression === "{/base*}")) continue;
|
|
66
|
+
throw new Error(t(errors.adjacentValues, {
|
|
67
|
+
template,
|
|
68
|
+
expression: `${expression}{${next}}`
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const validateKeywords = (template, parts) => {
|
|
73
|
+
for (const [expression, body] of parts.join("").matchAll(expressionRegex)) if (body.replace(operatorRegex, "").split(",").some((name) => keywords.includes(toBareName(name)))) throw new Error(t(errors.misplacedKeyword, {
|
|
74
|
+
template,
|
|
75
|
+
expression
|
|
76
|
+
}));
|
|
77
|
+
};
|
|
78
|
+
const validateBasePlacement = (template, hostName, path) => {
|
|
79
|
+
if (path.startsWith("{/base*}") && hostName === void 0) throw new Error(t(errors.misplacedBase, { template }));
|
|
80
|
+
};
|
|
81
|
+
const validateHostText = (template, host) => {
|
|
82
|
+
if (hostTextRegex.test(host)) throw new Error(t(errors.hostPortOrUser, { template }));
|
|
83
|
+
};
|
|
84
|
+
const validateHostLabels = (template, host) => {
|
|
85
|
+
if (host.split(".").some((label) => {
|
|
86
|
+
return label.includes("{") && nonAsciiLabelRegex.test(label);
|
|
87
|
+
})) throw new Error(t(errors.nonAsciiValueLabel, { template }));
|
|
88
|
+
};
|
|
89
|
+
const validateDotSegments = (template, path) => {
|
|
90
|
+
const segment = splitPath(path).find((segment) => dotSegmentRegex.test(segment));
|
|
91
|
+
if (segment !== void 0) throw new Error(t(errors.dotSegment, {
|
|
92
|
+
template,
|
|
93
|
+
segment
|
|
94
|
+
}));
|
|
95
|
+
};
|
|
96
|
+
const validateListPairs = (template, path) => {
|
|
97
|
+
let previous;
|
|
98
|
+
let text = "";
|
|
99
|
+
for (const [index, piece] of path.split(placeholderRegex).entries()) {
|
|
100
|
+
if (index % 2 === 0) {
|
|
101
|
+
text += piece;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (!isMultiSegment(piece) || `{/${piece}}` === "{/base*}") continue;
|
|
105
|
+
if (previous !== void 0 && !text.replaceAll("/", "")) {
|
|
106
|
+
const first = `{/${previous}}`;
|
|
107
|
+
const second = `{/${piece}}`;
|
|
108
|
+
throw new Error(t(errors.adjacentLists, {
|
|
109
|
+
template,
|
|
110
|
+
first,
|
|
111
|
+
second
|
|
112
|
+
}));
|
|
113
|
+
}
|
|
114
|
+
previous = piece;
|
|
115
|
+
text = "";
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const validateTemplateMatch = (template, base, match) => {
|
|
119
|
+
if (!match || base.includes("{?") || base.includes("{&")) throwInvalidTemplate(template);
|
|
120
|
+
};
|
|
121
|
+
const validateReservedPlacement = (placement) => {
|
|
122
|
+
const hasListBeside = [...placement.base.matchAll(placeholderRegex)].some(([, name]) => {
|
|
123
|
+
return isMultiSegment(name) && !placement.besideNames.includes(name);
|
|
124
|
+
});
|
|
125
|
+
const searchedText = placement.reservedNames.length > 0 && hasListBeside ? placement.template : placement.ownText;
|
|
126
|
+
const misplaced = reservedAnywhereRegex.exec(searchedText);
|
|
127
|
+
if (misplaced) {
|
|
128
|
+
const expression = misplaced[0];
|
|
129
|
+
throw new Error(t(errors.misplacedReserved, {
|
|
130
|
+
template: placement.template,
|
|
131
|
+
expression
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
const validateOptionalPlacement = (template, operator, search) => {
|
|
136
|
+
if (operator !== void 0 && operator === "?" === Boolean(search)) throwInvalidTemplate(template);
|
|
137
|
+
};
|
|
138
|
+
const validateQueryPlaceholders = (template, nameFlags, valueEntries, search) => {
|
|
139
|
+
const countPlaceholders = (text) => {
|
|
140
|
+
return [...text.matchAll(placeholderRegex)].length;
|
|
141
|
+
};
|
|
142
|
+
const isEscapedPlaceholder = [...nameFlags, ...valueEntries.flat()].reduce((count, text) => count + countPlaceholders(text), 0) !== countPlaceholders(search ?? "");
|
|
143
|
+
const hasNamedValue = valueEntries.some(([name]) => name.includes("{"));
|
|
144
|
+
if (isEscapedPlaceholder || hasNamedValue || search?.includes("{/")) throwInvalidTemplate(template);
|
|
145
|
+
};
|
|
146
|
+
const validateQueryValueNames = (names) => {
|
|
147
|
+
const otherNames = /* @__PURE__ */ new Set([...names.placeholderNames, ...names.reservedNames]);
|
|
148
|
+
if (names.queryValueNames.some((name) => otherNames.has(name))) throwInvalidTemplate(names.template);
|
|
149
|
+
};
|
|
150
|
+
const validateParamNames = (template, paramNames) => {
|
|
151
|
+
const names = paramNames.map((name) => name.toLowerCase());
|
|
152
|
+
if (new Set(names).size !== names.length) throwInvalidTemplate(template);
|
|
153
|
+
};
|
|
154
|
+
const validatePrototypeNames = (names) => {
|
|
155
|
+
const name = [
|
|
156
|
+
...names.placeholderNames,
|
|
157
|
+
...names.reservedNames,
|
|
158
|
+
...names.queryValueNames
|
|
159
|
+
].find((name) => Object.hasOwn(Object.prototype, name));
|
|
160
|
+
if (name !== void 0) throw new Error(t(errors.prototypeName, {
|
|
161
|
+
template: names.template,
|
|
162
|
+
name
|
|
163
|
+
}));
|
|
164
|
+
};
|
|
165
|
+
const validateReservedNames = (names) => {
|
|
166
|
+
const placeholderNames = new Set(names.placeholderNames);
|
|
167
|
+
if (new Set(names.reservedNames).size !== names.reservedNames.length || names.reservedNames.some((name) => placeholderNames.has(name))) throwInvalidTemplate(names.template);
|
|
168
|
+
};
|
|
169
|
+
const validateCheckRegexFlags = (name, check) => {
|
|
170
|
+
if (check.global || check.sticky) throw new Error(t(errors.statefulCheckRegex, { name }));
|
|
171
|
+
};
|
|
172
|
+
const validateCheckRegexAnchors = (name, check) => {
|
|
173
|
+
if (!check.source.startsWith("^") || !check.source.endsWith("$")) throw new Error(t(errors.unanchoredCheckRegex, { name }));
|
|
174
|
+
};
|
|
175
|
+
const validatePatternGroups = (name, pattern) => {
|
|
176
|
+
if (groupReferenceRegex.test(pattern.source)) throw new Error(t(errors.checkRegexGroup, { name }));
|
|
177
|
+
};
|
|
178
|
+
const validateCheckNames = (names) => {
|
|
179
|
+
const tableNames = new Set(names.templates.flatMap((template) => template.names));
|
|
180
|
+
const caseName = names.caseNames.find((name) => !tableNames.has(name));
|
|
181
|
+
if (caseName !== void 0) throw new Error(t(errors.unknownCaseName, { name: caseName }));
|
|
182
|
+
const templatesByRow = Map.groupBy(names.templates, (template) => template.rowIndex);
|
|
183
|
+
for (const [rowIndex, rowCheckNames] of names.rowCheckNames.entries()) {
|
|
184
|
+
const rowTemplates = templatesByRow.get(rowIndex) ?? [];
|
|
185
|
+
const rowNames = rowTemplates.flatMap((template) => template.names);
|
|
186
|
+
const rowName = rowCheckNames.find((name) => !rowNames.includes(name));
|
|
187
|
+
if (rowName !== void 0) {
|
|
188
|
+
const key = rowTemplates[0]?.key ?? "";
|
|
189
|
+
throw new Error(t(errors.unknownRowCheckName, {
|
|
190
|
+
key,
|
|
191
|
+
name: rowName
|
|
192
|
+
}));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const validateListSplit = (key, listSplit, checkNames) => {
|
|
197
|
+
if (!listSplit) return;
|
|
198
|
+
if (!checkNames.includes(listSplit.list) && !checkNames.includes(listSplit.reserved)) throw new Error(t(errors.uncheckedListSplit, {
|
|
199
|
+
key,
|
|
200
|
+
...listSplit
|
|
201
|
+
}));
|
|
202
|
+
};
|
|
203
|
+
const validateEntityNames = (key, parsed, isMarked) => {
|
|
204
|
+
if (!isMarked) return;
|
|
205
|
+
const name = [...parsed.params.map((param) => param.name), ...parsed.nameFlags].find((name) => name.startsWith(entityPrefix));
|
|
206
|
+
if (name !== void 0) throw new Error(t(errors.ampersandEntityName, {
|
|
207
|
+
key,
|
|
208
|
+
name
|
|
209
|
+
}));
|
|
210
|
+
};
|
|
211
|
+
const validateSemicolons = (key, search, lenient) => {
|
|
212
|
+
if (lenient?.semicolonSeparator === true && search?.includes(";")) throw new Error(t(errors.semicolonInQuery, { key }));
|
|
213
|
+
};
|
|
214
|
+
const validateTenantHost = (key, parentName, tenantCount) => {
|
|
215
|
+
if (parentName !== void 0 && tenantCount === 0) throw new Error(t(errors.missingHosts, { key }));
|
|
216
|
+
};
|
|
217
|
+
const validateOpenTail = (key, parsed) => {
|
|
218
|
+
if (!parsed.isOpen) return;
|
|
219
|
+
const multiSegmentName = [...parsed.path.matchAll(placeholderRegex)].map(([, name]) => name).find(isMultiSegment);
|
|
220
|
+
if (multiSegmentName !== void 0) {
|
|
221
|
+
const name = toBareName(multiSegmentName);
|
|
222
|
+
throw new Error(t(errors.openTailBesideMultiSegment, {
|
|
223
|
+
key,
|
|
224
|
+
name
|
|
225
|
+
}));
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
const validateCaseMark = (key, parsed, lenient) => {
|
|
229
|
+
const name = (parsed.names.filter(isMultiSegment).length > 1 ? [parsed.text] : toValueParts(parsed)).map((piece) => [...piece.matchAll(placeholderRegex)].map(([, name]) => toBareName(name))).filter((pieceNames) => pieceNames.length > 1).flat().find((name) => lenient?.valueCase?.includes(name));
|
|
230
|
+
if (name !== void 0) throw new Error(t(errors.sharedCaseMark, {
|
|
231
|
+
key,
|
|
232
|
+
name
|
|
233
|
+
}));
|
|
234
|
+
};
|
|
235
|
+
const validateListNames = (templates) => {
|
|
236
|
+
const shapes = {};
|
|
237
|
+
for (const template of templates) {
|
|
238
|
+
const namedShapes = [...template.names.map((name) => {
|
|
239
|
+
return [toBareName(name), isMultiSegment(name) ? "list" : "value"];
|
|
240
|
+
}), ...(template.optionalNames ?? []).map((name) => [name, "value"])];
|
|
241
|
+
if (template.passedName !== void 0) namedShapes.push([template.passedName, "map"]);
|
|
242
|
+
for (const [name, shape] of namedShapes) {
|
|
243
|
+
const otherShape = shapes[name];
|
|
244
|
+
if (otherShape === void 0) {
|
|
245
|
+
shapes[name] = shape;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
if (otherShape !== shape) throw new Error(t(errors.mixedListName, {
|
|
249
|
+
key: template.key,
|
|
250
|
+
name,
|
|
251
|
+
shape: nameShapes[shape],
|
|
252
|
+
otherShape: nameShapes[otherShape]
|
|
253
|
+
}));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
export { validateAdjacentValues, validateBasePlacement, validateBraces, validateCaseMark, validateCheckNames, validateCheckRegexAnchors, validateCheckRegexFlags, validateDotSegments, validateEntityNames, validateExpressions, validateFragment, validateHostLabels, validateHostText, validateKeywords, validateListNames, validateListPairs, validateListSplit, validateOldSpellings, validateOpenTail, validateOptionalPlacement, validateParamNames, validatePatternGroups, validatePrototypeNames, validateQueryPlaceholders, validateQueryValueNames, validateReservedNames, validateReservedPlacement, validateSemicolons, validateTemplateMatch, validateTenantHost };
|
package/dist/tables.d.mts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import { UrlRow, UrlTable, UrlTableOptions } from "./types.mjs";
|
|
2
|
-
export declare const defineTable: <const Row extends UrlRow>(givenRows: Array<Row>, options?: UrlTableOptions) => UrlTable<Row['key'], Row>;
|
|
1
|
+
import { UrlHiddenRow, UrlRow, UrlTable, UrlTableOptions } from "./types.mjs";
|
|
2
|
+
export declare const defineTable: <const Row extends UrlRow>(givenRows: Array<Row>, options?: UrlTableOptions) => UrlTable<Row['key'], Row>;
|
|
3
|
+
export declare const findHiddenRows: <Key extends string, Row>(table: UrlTable<Key, Row>) => Array<UrlHiddenRow<Row>>;
|