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
package/dist/types.d.mts
CHANGED
|
@@ -1,39 +1,46 @@
|
|
|
1
1
|
export type UrlLenient = {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
valueCase?: Array<string> | undefined;
|
|
3
|
+
fixedCase?: boolean | undefined;
|
|
4
|
+
trailingSlash?: boolean | undefined;
|
|
5
|
+
ampersandEntity?: boolean | undefined;
|
|
6
|
+
semicolonSeparator?: boolean | undefined;
|
|
7
|
+
pipeEscape?: boolean | undefined;
|
|
4
8
|
};
|
|
5
9
|
export type UrlRow<Key extends string = string> = {
|
|
6
10
|
key: Key;
|
|
7
11
|
main: string;
|
|
8
|
-
aliases?: Array<string
|
|
9
|
-
isBuildOnly?: boolean;
|
|
10
|
-
variant?: string;
|
|
11
|
-
checks?: Record<string, UrlRowCheck | undefined
|
|
12
|
+
aliases?: Array<string> | undefined;
|
|
13
|
+
isBuildOnly?: boolean | undefined;
|
|
14
|
+
variant?: string | undefined;
|
|
15
|
+
checks?: Record<string, UrlRowCheck | undefined> | undefined;
|
|
16
|
+
lenient?: Pick<UrlLenient, 'trailingSlash' | 'pipeEscape'> | undefined;
|
|
12
17
|
};
|
|
13
|
-
export type UrlMap = Record<string, string
|
|
18
|
+
export type UrlMap = Record<string, string | Array<string>>;
|
|
14
19
|
export type UrlValue = string | Array<string> | UrlMap;
|
|
15
20
|
export type UrlValues = Record<string, UrlValue | undefined>;
|
|
16
21
|
export type UrlHostCheck = (host: string) => boolean;
|
|
17
22
|
export type UrlHosts = {
|
|
18
|
-
exact?: Array<string | RegExp | UrlHostCheck
|
|
19
|
-
tenant?: Array<string | RegExp | UrlHostCheck
|
|
23
|
+
exact?: Array<string | RegExp | UrlHostCheck> | undefined;
|
|
24
|
+
tenant?: Array<string | RegExp | UrlHostCheck> | undefined;
|
|
25
|
+
subdomain?: Array<string | RegExp | UrlHostCheck> | undefined;
|
|
20
26
|
};
|
|
21
27
|
export type UrlCheck = ((value: string) => boolean) | RegExp;
|
|
22
|
-
export type UrlRowCheck = ((value: string, values: Record<string, string>) => boolean) | RegExp;
|
|
28
|
+
export type UrlRowCheck = ((value: string, values: Record<string, string | undefined>) => boolean) | RegExp;
|
|
23
29
|
export type UrlTableOptions = {
|
|
24
|
-
hosts?: UrlHosts;
|
|
25
|
-
isWwwTenant?: boolean;
|
|
26
|
-
isWwwTenantPrefix?: boolean;
|
|
27
|
-
checks?: Record<string, UrlCheck
|
|
28
|
-
isQueryKept?: boolean;
|
|
29
|
-
lenient?: UrlLenient;
|
|
30
|
-
isFallThrough?: boolean;
|
|
30
|
+
hosts?: UrlHosts | undefined;
|
|
31
|
+
isWwwTenant?: boolean | undefined;
|
|
32
|
+
isWwwTenantPrefix?: boolean | undefined;
|
|
33
|
+
checks?: Record<string, UrlCheck> | undefined;
|
|
34
|
+
isQueryKept?: boolean | undefined;
|
|
35
|
+
lenient?: UrlLenient | undefined;
|
|
36
|
+
isFallThrough?: boolean | undefined;
|
|
31
37
|
};
|
|
32
38
|
export type UrlReadOptions = {
|
|
33
|
-
base?:
|
|
34
|
-
isAnyHost?: boolean;
|
|
39
|
+
base?: string | Array<string> | undefined;
|
|
40
|
+
isAnyHost?: boolean | undefined;
|
|
35
41
|
};
|
|
36
|
-
type
|
|
42
|
+
type QueryNames<List extends string> = List extends `${infer Name},${infer Rest}` ? `?${Name}` | QueryNames<Rest> : `?${List}`;
|
|
43
|
+
type TemplateNames<Template extends string> = Template extends `${string}{${infer Name}}${infer Rest}` ? (Name extends `${'?' | '&'}${infer List}` ? QueryNames<List> : Name) | TemplateNames<Rest> : never;
|
|
37
44
|
type RowTemplates<Row> = Row extends {
|
|
38
45
|
main: infer Main extends string;
|
|
39
46
|
} ? Main | (Row extends {
|
|
@@ -42,29 +49,42 @@ type RowTemplates<Row> = Row extends {
|
|
|
42
49
|
type CommonNames<Template extends string> = (Template extends string ? (names: TemplateNames<Template>) => void : never) extends ((names: infer Names) => void) ? Names : never;
|
|
43
50
|
type TailName = '/tail*';
|
|
44
51
|
type BaseName = '/base*';
|
|
45
|
-
type
|
|
46
|
-
type
|
|
47
|
-
type
|
|
52
|
+
type QueryName = `?${string}`;
|
|
53
|
+
type ValueName<Name> = Name extends `${'/' | '?'}${infer Bare extends string}*` ? Bare : Name extends `${'?' | '+' | '#'}${infer Bare extends string}` ? Bare : Name extends string ? Name : never;
|
|
54
|
+
type NameValue<Name> = Name extends `?${string}*` ? UrlMap : Name extends `${string}*` ? Array<string> : string;
|
|
55
|
+
type TemplateValues<Template extends string> = string extends Template ? Record<string, UrlValue | undefined> : { [Name in Exclude<CommonNames<Template>, TailName | BaseName | QueryName> as ValueName<Name>]: NameValue<Name>; } & { [Name in Exclude<TemplateNames<Template>, TailName | Exclude<CommonNames<Template>, BaseName | QueryName>> as ValueName<Name>]?: NameValue<Name>; };
|
|
48
56
|
type KeyRows<Key, Row> = [Extract<Row, {
|
|
49
57
|
key: Key;
|
|
50
58
|
}>] extends [never] ? Row : Extract<Row, {
|
|
51
59
|
key: Key;
|
|
52
60
|
}>;
|
|
53
|
-
type
|
|
54
|
-
|
|
61
|
+
type ReadRows<Key, Row> = Exclude<KeyRows<Key, Row>, {
|
|
62
|
+
isBuildOnly: true;
|
|
63
|
+
}>;
|
|
64
|
+
type KeyValues<Key, Row> = TemplateValues<RowTemplates<ReadRows<Key, Row>>>;
|
|
65
|
+
export type UrlMatch<Key extends string, Row> = Key extends Key ? [ReadRows<Key, Row>] extends [never] ? never : UrlKeyMatch<Key, Row> : never;
|
|
55
66
|
export type UrlKeyMatch<Key extends string, Row> = {
|
|
56
67
|
key: Key;
|
|
57
68
|
values: KeyValues<Key, Row>;
|
|
69
|
+
row: ReadRows<Key, Row>;
|
|
70
|
+
isAliasMatch: boolean;
|
|
71
|
+
isExactMatch: boolean;
|
|
72
|
+
isLenientMatch: boolean;
|
|
73
|
+
};
|
|
74
|
+
export type UrlHiddenRow<Row> = {
|
|
75
|
+
key: string;
|
|
58
76
|
row: Row;
|
|
59
|
-
|
|
60
|
-
|
|
77
|
+
template: string;
|
|
78
|
+
hiddenRow: Row;
|
|
79
|
+
hiddenTemplate: string;
|
|
80
|
+
url: string;
|
|
61
81
|
};
|
|
62
82
|
export type UrlEncoding = 'pretty' | 'strict';
|
|
63
83
|
export type UrlWriteOptions = {
|
|
64
|
-
encoding?: UrlEncoding;
|
|
84
|
+
encoding?: UrlEncoding | undefined;
|
|
65
85
|
};
|
|
66
86
|
export type UrlBuildOptions<Row> = UrlWriteOptions & {
|
|
67
|
-
select?: (row: Row) => boolean;
|
|
87
|
+
select?: ((row: Row) => boolean) | undefined;
|
|
68
88
|
};
|
|
69
89
|
export type UrlTable<Key extends string, Row> = {
|
|
70
90
|
rows: Array<Row>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { decodePercent, getSequenceLength, toPercentEscape } from "../node_modules/trousse/dist/urls.mjs";
|
|
2
|
+
// src/utils/escapes.ts
|
|
3
|
+
const tripletRegex = /^%[0-9a-f]{2}$/i;
|
|
4
|
+
const singleEscapeRegex = /(%[0-9a-f]{2})/i;
|
|
5
|
+
const escapeRunRegex = /((?:%[0-9a-f]{2})+)/i;
|
|
6
|
+
const subDelimiterRegex = /[!'()*]/g;
|
|
7
|
+
const textEncoder = new TextEncoder();
|
|
8
|
+
const textDecoder = new TextDecoder();
|
|
9
|
+
const strictTextDecoder = new TextDecoder("utf-8", { fatal: true });
|
|
10
|
+
const toTextBytes = (text) => {
|
|
11
|
+
const bytes = text.split(singleEscapeRegex).flatMap((piece, index) => {
|
|
12
|
+
return index % 2 === 1 ? [Number.parseInt(piece.slice(1), 16)] : [...textEncoder.encode(piece)];
|
|
13
|
+
});
|
|
14
|
+
return new Uint8Array(bytes);
|
|
15
|
+
};
|
|
16
|
+
const toDecodedText = (text) => {
|
|
17
|
+
return textDecoder.decode(toTextBytes(text));
|
|
18
|
+
};
|
|
19
|
+
const isUtf8Text = (text) => {
|
|
20
|
+
try {
|
|
21
|
+
strictTextDecoder.decode(toTextBytes(text));
|
|
22
|
+
return true;
|
|
23
|
+
} catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const toLowerCaseValue = (value) => {
|
|
28
|
+
return value.split(singleEscapeRegex).map((piece, index) => index % 2 === 1 ? piece : piece.toLowerCase()).join("");
|
|
29
|
+
};
|
|
30
|
+
const toDecodedUnit = (segment, index) => {
|
|
31
|
+
const triplet = segment.slice(index, index + 3);
|
|
32
|
+
if (!tripletRegex.test(triplet)) return segment[index];
|
|
33
|
+
const length = getSequenceLength(Number.parseInt(triplet.slice(1), 16)) * 3;
|
|
34
|
+
const sequence = segment.slice(index, index + length);
|
|
35
|
+
const decoded = decodePercent(sequence);
|
|
36
|
+
return length === 0 || decoded === sequence || [...decoded].length > 1 ? triplet : sequence;
|
|
37
|
+
};
|
|
38
|
+
const encodeEscapeRun = (run, encode) => {
|
|
39
|
+
let written = "";
|
|
40
|
+
let index = 0;
|
|
41
|
+
while (index < run.length) {
|
|
42
|
+
const unit = toDecodedUnit(run, index);
|
|
43
|
+
const decoded = decodePercent(unit);
|
|
44
|
+
written += decoded === unit ? unit : encode(decoded);
|
|
45
|
+
index += unit.length;
|
|
46
|
+
}
|
|
47
|
+
return written;
|
|
48
|
+
};
|
|
49
|
+
const encodeDecoded = (value, encode) => {
|
|
50
|
+
return value.split(escapeRunRegex).map((piece, index) => index % 2 === 0 ? encode(piece) : encodeEscapeRun(piece, encode)).join("");
|
|
51
|
+
};
|
|
52
|
+
const encodeStrictValue = (value) => {
|
|
53
|
+
return encodeURIComponent(value).replace(subDelimiterRegex, toPercentEscape);
|
|
54
|
+
};
|
|
55
|
+
export { encodeDecoded, encodeStrictValue, isUtf8Text, singleEscapeRegex, toDecodedText, toDecodedUnit, toLowerCaseValue };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { getRegistrableDomain } from "../node_modules/trousse/dist/urls.mjs";
|
|
2
|
+
// src/utils/hosts.ts
|
|
3
|
+
const nonAsciiLabelRegex = /\P{ASCII}/u;
|
|
4
|
+
const isApexHost = (host) => {
|
|
5
|
+
return getRegistrableDomain(`https://${host}`) === host;
|
|
6
|
+
};
|
|
7
|
+
export { isApexHost, nonAsciiLabelRegex };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { decodePath } from "../node_modules/trousse/dist/urls.mjs";
|
|
2
|
+
import { isString } from "../node_modules/trousse/dist/is.mjs";
|
|
3
|
+
import { toDecodedUnit } from "./escapes.mjs";
|
|
4
|
+
// src/utils/paths.ts
|
|
5
|
+
const trailingSlashRegex = /\/$/;
|
|
6
|
+
const dotSegmentRegex = /^(?:\.|%2e){1,2}$/i;
|
|
7
|
+
const toSegments = (value) => {
|
|
8
|
+
return (isString(value) ? value.split("/") : value ?? []).filter((segment) => segment);
|
|
9
|
+
};
|
|
10
|
+
const toWrittenLength = (segment, length) => {
|
|
11
|
+
if (!segment.includes("%")) return length;
|
|
12
|
+
let written = 0;
|
|
13
|
+
let decoded = 0;
|
|
14
|
+
while (decoded < length && written < segment.length) {
|
|
15
|
+
const unit = toDecodedUnit(segment, written);
|
|
16
|
+
decoded += decodePath(unit).length;
|
|
17
|
+
written += unit.length;
|
|
18
|
+
}
|
|
19
|
+
if (decoded !== length) return;
|
|
20
|
+
return written;
|
|
21
|
+
};
|
|
22
|
+
const toWrittenOffset = (path, offset) => {
|
|
23
|
+
if (!path.includes("%")) return offset;
|
|
24
|
+
let written = 0;
|
|
25
|
+
let decoded = 0;
|
|
26
|
+
for (const segment of path.split("/")) {
|
|
27
|
+
const decodedLength = decodePath(segment).length;
|
|
28
|
+
if (offset <= decoded + decodedLength) {
|
|
29
|
+
const length = toWrittenLength(segment, offset - decoded);
|
|
30
|
+
if (length === void 0) return;
|
|
31
|
+
return written + length;
|
|
32
|
+
}
|
|
33
|
+
written += segment.length + 1;
|
|
34
|
+
decoded += decodedLength + 1;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
export { dotSegmentRegex, toSegments, toWrittenOffset, trailingSlashRegex };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { composeQuery, decodeFormPart } from "../node_modules/trousse/dist/urls.mjs";
|
|
2
|
+
import { isAnyOf } from "../node_modules/trousse/dist/matching.mjs";
|
|
3
|
+
import { encodeDecoded, encodeStrictValue } from "./escapes.mjs";
|
|
4
|
+
// src/utils/queries.ts
|
|
5
|
+
const entityPrefix = "amp;";
|
|
6
|
+
const leadingQuestionMarkRegex = /^\?/;
|
|
7
|
+
const pairSeparatorRegex = /=(.*)/s;
|
|
8
|
+
const nonAsciiRegex = /[^\p{ASCII}\p{Cs}]+|\p{Cc}+/gu;
|
|
9
|
+
const pathlessBaseRegex = /^[^/]+:\/\/[^/]+$/;
|
|
10
|
+
const cutHref = (url) => {
|
|
11
|
+
const [beforeHash] = url.href.split("#", 1);
|
|
12
|
+
const [base] = beforeHash.split("?", 1);
|
|
13
|
+
return {
|
|
14
|
+
search: beforeHash.slice(base.length),
|
|
15
|
+
hash: url.href.slice(beforeHash.length)
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
const encodeText = (text) => {
|
|
19
|
+
const pair = composeQuery({ "": text });
|
|
20
|
+
return pair.slice(pair.indexOf("=") + 1).replace(nonAsciiRegex, encodeURIComponent);
|
|
21
|
+
};
|
|
22
|
+
const isNamed = (pair, names) => {
|
|
23
|
+
return isAnyOf(pair.name, names);
|
|
24
|
+
};
|
|
25
|
+
const toPair = (text) => {
|
|
26
|
+
const [rawName, rawValue] = text.split(pairSeparatorRegex);
|
|
27
|
+
return {
|
|
28
|
+
text,
|
|
29
|
+
rawName,
|
|
30
|
+
rawValue,
|
|
31
|
+
name: decodeFormPart(rawName)
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
const isEntityPair = (pair, index) => {
|
|
35
|
+
return index > 0 && pair.text.startsWith("amp;");
|
|
36
|
+
};
|
|
37
|
+
const toFlagNames = (pairs) => {
|
|
38
|
+
return pairs.map((pair) => pair.rawValue ? void 0 : pair.name);
|
|
39
|
+
};
|
|
40
|
+
const readAmpersandEntities = (pairs) => {
|
|
41
|
+
return pairs.map((pair, index) => {
|
|
42
|
+
if (!isEntityPair(pair, index)) return pair;
|
|
43
|
+
return toPair(pair.text.slice(4));
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
const readSemicolons = (pairs) => {
|
|
47
|
+
return pairs.flatMap((pair) => pair.text.split(";").map(toPair));
|
|
48
|
+
};
|
|
49
|
+
const appendValue = (pair, valueText) => {
|
|
50
|
+
return {
|
|
51
|
+
...pair,
|
|
52
|
+
text: `${pair.text}${valueText}`,
|
|
53
|
+
rawValue: `${pair.rawValue ?? ""}${valueText}`
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
const parseQuery = (search) => {
|
|
57
|
+
if (!search) return [];
|
|
58
|
+
return search.replace(leadingQuestionMarkRegex, "").split("&").map(toPair);
|
|
59
|
+
};
|
|
60
|
+
const writeQuery = (pairs) => {
|
|
61
|
+
if (pairs.length === 0) return "";
|
|
62
|
+
return `?${pairs.map((pair) => pair.text).join("&")}`;
|
|
63
|
+
};
|
|
64
|
+
const toSearchParams = (pairs) => {
|
|
65
|
+
const searchParams = new URLSearchParams();
|
|
66
|
+
for (const pair of pairs) if (pair.text) searchParams.append(pair.name, decodeFormPart(pair.rawValue ?? ""));
|
|
67
|
+
return searchParams;
|
|
68
|
+
};
|
|
69
|
+
const toUrlQuery = (pairs, hash) => {
|
|
70
|
+
return {
|
|
71
|
+
pairs,
|
|
72
|
+
searchParams: toSearchParams(pairs),
|
|
73
|
+
hash
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
const encodeStrictQueryText = (text) => {
|
|
77
|
+
return text.split("+").map((piece) => encodeDecoded(piece, encodeStrictValue)).join("+");
|
|
78
|
+
};
|
|
79
|
+
const toStrictPair = (pair) => {
|
|
80
|
+
const separator = pair.text.indexOf("=");
|
|
81
|
+
if (separator === -1) return toPair(encodeStrictQueryText(pair.text));
|
|
82
|
+
const name = encodeStrictQueryText(pair.text.slice(0, separator));
|
|
83
|
+
const value = encodeStrictQueryText(pair.text.slice(separator + 1));
|
|
84
|
+
return toPair(`${name}=${value}`);
|
|
85
|
+
};
|
|
86
|
+
const writeBase = (base, pairs) => {
|
|
87
|
+
return pairs.length > 0 && pathlessBaseRegex.test(base) ? `${base}/` : base;
|
|
88
|
+
};
|
|
89
|
+
export { appendValue, cutHref, encodeText, entityPrefix, isEntityPair, isNamed, nonAsciiRegex, parseQuery, readAmpersandEntities, readSemicolons, toFlagNames, toPair, toSearchParams, toStrictPair, toUrlQuery, writeBase, writeQuery };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { escapeRegex } from "../node_modules/trousse/dist/matching.mjs";
|
|
2
|
+
import { singleEscapeRegex } from "./escapes.mjs";
|
|
3
|
+
// src/utils/regexes.ts
|
|
4
|
+
const groupReferenceRegex = /(?<!\\)(?:\\\\)*\\(?:[1-9]|k<)|\(\?<(?![=!])/;
|
|
5
|
+
const hexLetterRegex = /[a-f]/i;
|
|
6
|
+
const splitAlternatives = (source) => {
|
|
7
|
+
const alternatives = [];
|
|
8
|
+
let alternative = "";
|
|
9
|
+
let depth = 0;
|
|
10
|
+
let isInClass = false;
|
|
11
|
+
for (let index = 0; index < source.length; index++) {
|
|
12
|
+
const character = source[index];
|
|
13
|
+
const piece = character === "\\" ? source.slice(index, index + 2) : character;
|
|
14
|
+
index += piece.length - 1;
|
|
15
|
+
if (character === "|" && depth === 0 && !isInClass) {
|
|
16
|
+
alternatives.push(alternative);
|
|
17
|
+
alternative = "";
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
alternative += piece;
|
|
21
|
+
if (isInClass) {
|
|
22
|
+
isInClass = character !== "]";
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
if (character === "[") isInClass = true;
|
|
26
|
+
if (character === "(") depth += 1;
|
|
27
|
+
if (character === ")") depth -= 1;
|
|
28
|
+
}
|
|
29
|
+
return [...alternatives, alternative];
|
|
30
|
+
};
|
|
31
|
+
const toLengthGuard = (maxLength, stops) => {
|
|
32
|
+
if (!stops) return `(?=[^]{0,${maxLength}}$)`;
|
|
33
|
+
return `(?=[^${stops}]{0,${maxLength}}(?:[${stops}]|$))`;
|
|
34
|
+
};
|
|
35
|
+
const toHexDigitSource = (digit) => {
|
|
36
|
+
return hexLetterRegex.test(digit) ? `[${digit.toUpperCase()}${digit.toLowerCase()}]` : digit;
|
|
37
|
+
};
|
|
38
|
+
const toLiteralSource = (text) => {
|
|
39
|
+
return text.split(singleEscapeRegex).map((piece, index) => {
|
|
40
|
+
if (index % 2 === 0) return escapeRegex(piece);
|
|
41
|
+
return `%${toHexDigitSource(piece[1])}${toHexDigitSource(piece[2])}`;
|
|
42
|
+
}).join("");
|
|
43
|
+
};
|
|
44
|
+
export { groupReferenceRegex, splitAlternatives, toLengthGuard, toLiteralSource };
|
package/package.json
CHANGED
|
@@ -40,11 +40,11 @@
|
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/bun": "^1.4.4",
|
|
43
|
-
"kvalita": "^1.
|
|
44
|
-
"trousse": "3.
|
|
43
|
+
"kvalita": "^1.43.0",
|
|
44
|
+
"trousse": "3.20.0",
|
|
45
45
|
"tsdown": "0.23.7",
|
|
46
46
|
"uritemplate-test": "github:uri-templates/uritemplate-test#4171dac22aa6",
|
|
47
47
|
"url-template": "github:bramstein/url-template#e5edf15bfc86"
|
|
48
48
|
},
|
|
49
|
-
"version": "1.
|
|
49
|
+
"version": "1.1.0"
|
|
50
50
|
}
|
package/dist/queries.mjs
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { composeQuery, decodePercent, getQueryParam } from "./node_modules/trousse/dist/urls.mjs";
|
|
2
|
-
// src/queries.ts
|
|
3
|
-
const entityPrefix = "amp;";
|
|
4
|
-
const leadingQuestionMarkRegex = /^\?/;
|
|
5
|
-
const pairSeparatorRegex = /=(.*)/s;
|
|
6
|
-
const plainNameRegex = /^[\w.~-]*$/;
|
|
7
|
-
const nonAsciiRegex = /[^\p{ASCII}\p{Cs}]+|\p{Cc}+/gu;
|
|
8
|
-
const cutHref = (url) => {
|
|
9
|
-
const [beforeHash] = url.href.split("#", 1);
|
|
10
|
-
const [base] = beforeHash.split("?", 1);
|
|
11
|
-
return {
|
|
12
|
-
base,
|
|
13
|
-
search: beforeHash.slice(base.length),
|
|
14
|
-
hash: url.href.slice(beforeHash.length)
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
const encodeText = (text) => {
|
|
18
|
-
const pair = composeQuery({ "": text });
|
|
19
|
-
return pair.slice(pair.indexOf("=") + 1).replace(nonAsciiRegex, encodeURIComponent);
|
|
20
|
-
};
|
|
21
|
-
const decodeQueryValue = (value) => {
|
|
22
|
-
return getQueryParam(`=${value}`, "", {
|
|
23
|
-
preserveEmptyValues: true,
|
|
24
|
-
trimValues: false
|
|
25
|
-
}) ?? "";
|
|
26
|
-
};
|
|
27
|
-
const toPair = (text) => {
|
|
28
|
-
const [rawName, rawValue] = text.split(pairSeparatorRegex);
|
|
29
|
-
if (plainNameRegex.test(rawName)) return {
|
|
30
|
-
text,
|
|
31
|
-
rawName,
|
|
32
|
-
rawValue,
|
|
33
|
-
name: rawName,
|
|
34
|
-
sortName: rawName
|
|
35
|
-
};
|
|
36
|
-
const [[name]] = new URLSearchParams(`&${rawName}`);
|
|
37
|
-
return {
|
|
38
|
-
text,
|
|
39
|
-
rawName,
|
|
40
|
-
rawValue,
|
|
41
|
-
name,
|
|
42
|
-
sortName: decodePercent(rawName.replaceAll("+", " "))
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
|
-
const isEntityPair = (pair, index) => {
|
|
46
|
-
return index > 0 && pair.text.startsWith("amp;");
|
|
47
|
-
};
|
|
48
|
-
const readAmpersandEntities = (pairs) => {
|
|
49
|
-
return pairs.map((pair, index) => {
|
|
50
|
-
if (!isEntityPair(pair, index)) return pair;
|
|
51
|
-
return toPair(pair.text.slice(4));
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
const appendValue = (pair, valueText) => {
|
|
55
|
-
return {
|
|
56
|
-
...pair,
|
|
57
|
-
text: `${pair.text}${valueText}`,
|
|
58
|
-
rawValue: `${pair.rawValue ?? ""}${valueText}`
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
const parseQuery = (search) => {
|
|
62
|
-
if (!search) return [];
|
|
63
|
-
const [query] = search.split("#");
|
|
64
|
-
return query.replace(leadingQuestionMarkRegex, "").split("&").map(toPair);
|
|
65
|
-
};
|
|
66
|
-
const sortPairs = (pairs) => {
|
|
67
|
-
return pairs.filter((pair) => pair.text).sort((left, right) => {
|
|
68
|
-
const leftName = left.sortName.toLowerCase();
|
|
69
|
-
const rightName = right.sortName.toLowerCase();
|
|
70
|
-
if (leftName === rightName) return 0;
|
|
71
|
-
return leftName < rightName ? -1 : 1;
|
|
72
|
-
});
|
|
73
|
-
};
|
|
74
|
-
const writeQuery = (pairs) => {
|
|
75
|
-
if (pairs.length === 0) return "";
|
|
76
|
-
return `?${pairs.map((pair) => pair.text).join("&")}`;
|
|
77
|
-
};
|
|
78
|
-
const toSearchParams = (pairs) => {
|
|
79
|
-
return new URLSearchParams(writeQuery(pairs));
|
|
80
|
-
};
|
|
81
|
-
const toUrlQuery = (pairs, hash) => {
|
|
82
|
-
return {
|
|
83
|
-
pairs,
|
|
84
|
-
searchParams: toSearchParams(pairs),
|
|
85
|
-
hash
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
const readUrlQuery = (url) => {
|
|
89
|
-
const { search, hash } = cutHref(url);
|
|
90
|
-
return toUrlQuery(parseQuery(search), hash);
|
|
91
|
-
};
|
|
92
|
-
export { appendValue, cutHref, decodeQueryValue, encodeText, entityPrefix, isEntityPair, nonAsciiRegex, parseQuery, readAmpersandEntities, readUrlQuery, sortPairs, toPair, toSearchParams, toUrlQuery, writeQuery };
|