urlmason 1.0.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.
@@ -0,0 +1,92 @@
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 };
@@ -0,0 +1,2 @@
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>;