trousse 3.8.0 → 3.9.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/dist/arrays.d.ts +2 -1
- package/dist/coercions.js +1 -1
- package/dist/is.d.ts +5 -2
- package/dist/matching.js +37 -19
- package/dist/urls.js +38 -13
- package/package.json +1 -1
package/dist/arrays.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { Nullish } from "./types.js";
|
|
2
|
+
export declare const omitEmpty: <T>(array: Array<Nullish<T>>) => Array<T> | undefined;
|
package/dist/coercions.js
CHANGED
|
@@ -26,7 +26,7 @@ const coerceDate = (value) => {
|
|
|
26
26
|
if (isString(value) && nonYearDigitsRegex.test(value)) return;
|
|
27
27
|
if (isNumber(value) || isNonEmptyString(value)) {
|
|
28
28
|
const date = new Date(value);
|
|
29
|
-
return
|
|
29
|
+
return !isValidDate(date) ? void 0 : date;
|
|
30
30
|
}
|
|
31
31
|
};
|
|
32
32
|
const coerceSingular = (value) => {
|
package/dist/is.d.ts
CHANGED
|
@@ -3,10 +3,13 @@ export declare const isNumber: (value: unknown) => value is number;
|
|
|
3
3
|
export declare const isInteger: (value: unknown) => value is number;
|
|
4
4
|
export declare const isFiniteNumber: (value: unknown) => value is number;
|
|
5
5
|
export declare const isBoolean: (value: unknown) => value is boolean;
|
|
6
|
-
|
|
6
|
+
type AnyFunction = (...args: Array<never>) => unknown;
|
|
7
|
+
type FunctionOf<T> = unknown extends T ? (...args: Array<unknown>) => unknown : Extract<T, AnyFunction> extends never ? T & ((...args: Array<unknown>) => unknown) : Extract<T, AnyFunction>;
|
|
8
|
+
export declare const isFunction: <T>(value: T) => value is FunctionOf<T> & T;
|
|
7
9
|
export declare const isPresent: <T>(value: T) => value is NonNullable<T>;
|
|
8
10
|
export declare const isNullish: (value: unknown) => value is null | undefined;
|
|
9
11
|
export declare const isObject: (value: unknown) => value is object;
|
|
10
12
|
export declare const isPlainObject: (value: unknown) => value is Record<string, unknown>;
|
|
11
13
|
export declare const isNonEmptyString: (value: unknown) => value is string;
|
|
12
|
-
export declare const isValidDate: (value: unknown) => value is Date;
|
|
14
|
+
export declare const isValidDate: (value: unknown) => value is Date;
|
|
15
|
+
export {};
|
package/dist/matching.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isNullish } from "./is.js";
|
|
1
|
+
import { isNullish, isString } from "./is.js";
|
|
2
2
|
// src/matching.ts
|
|
3
3
|
const whitespaceRegex = /\s+/;
|
|
4
4
|
const regexSyntaxCharsRegex = /[.*+?^${}()|[\]\\/]/;
|
|
@@ -11,7 +11,7 @@ const testRegexAnyCase = (regex, lowerValue, value) => {
|
|
|
11
11
|
return testRegex(regex, lowerValue) || lowerValue !== value && testRegex(regex, value);
|
|
12
12
|
};
|
|
13
13
|
const toPatternList = (patterns) => {
|
|
14
|
-
return
|
|
14
|
+
return isString(patterns) || patterns instanceof RegExp ? [patterns] : patterns;
|
|
15
15
|
};
|
|
16
16
|
const escapeRegex = (value) => {
|
|
17
17
|
return value.replace(regexEscapableCharsRegex, (char) => {
|
|
@@ -22,11 +22,17 @@ const escapeRegex = (value) => {
|
|
|
22
22
|
const isAnyOf = (value, patterns, parser) => {
|
|
23
23
|
if (isNullish(value)) return false;
|
|
24
24
|
const parsedValue = parser ? parser(value) : value.toLowerCase().trim();
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
if (isString(patterns)) return parsedValue === (parser ? patterns : patterns.toLowerCase().trim());
|
|
26
|
+
let originalValue;
|
|
27
|
+
for (const pattern of toPatternList(patterns)) {
|
|
28
|
+
if (pattern instanceof RegExp) {
|
|
29
|
+
if (originalValue === void 0) originalValue = parser ? parsedValue : value.trim();
|
|
30
|
+
if (testRegexAnyCase(pattern, parsedValue, originalValue)) return true;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (parsedValue === (parser ? pattern : pattern.toLowerCase().trim())) return true;
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
30
36
|
};
|
|
31
37
|
const isSameText = (value, other) => {
|
|
32
38
|
if (isNullish(value) || isNullish(other)) return false;
|
|
@@ -43,26 +49,38 @@ const includesAnyOf = (value, patterns, parser) => {
|
|
|
43
49
|
if (isNullish(value)) return false;
|
|
44
50
|
const parsedValue = parser ? parser(value) : value.toLowerCase();
|
|
45
51
|
const originalValue = parser ? parsedValue : value;
|
|
46
|
-
|
|
47
|
-
if (pattern instanceof RegExp)
|
|
48
|
-
|
|
49
|
-
|
|
52
|
+
for (const pattern of toPatternList(patterns)) {
|
|
53
|
+
if (pattern instanceof RegExp) {
|
|
54
|
+
if (testRegexAnyCase(pattern, parsedValue, originalValue)) return true;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (pattern && parsedValue.includes(parser ? pattern : pattern.toLowerCase())) return true;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
50
60
|
};
|
|
51
61
|
const startsWithAnyOf = (value, patterns) => {
|
|
52
62
|
if (isNullish(value)) return false;
|
|
53
63
|
const lowerValue = value.toLowerCase();
|
|
54
|
-
|
|
55
|
-
if (pattern instanceof RegExp)
|
|
56
|
-
|
|
57
|
-
|
|
64
|
+
for (const pattern of toPatternList(patterns)) {
|
|
65
|
+
if (pattern instanceof RegExp) {
|
|
66
|
+
if (testRegexAnyCase(pattern, lowerValue, value)) return true;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (pattern && lowerValue.startsWith(pattern.toLowerCase())) return true;
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
58
72
|
};
|
|
59
73
|
const endsWithAnyOf = (value, patterns) => {
|
|
60
74
|
if (isNullish(value)) return false;
|
|
61
75
|
const lowerValue = value.toLowerCase();
|
|
62
|
-
|
|
63
|
-
if (pattern instanceof RegExp)
|
|
64
|
-
|
|
65
|
-
|
|
76
|
+
for (const pattern of toPatternList(patterns)) {
|
|
77
|
+
if (pattern instanceof RegExp) {
|
|
78
|
+
if (testRegexAnyCase(pattern, lowerValue, value)) return true;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (pattern && lowerValue.endsWith(pattern.toLowerCase())) return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
66
84
|
};
|
|
67
85
|
const anyWordMatchesAnyOf = (value, patterns) => {
|
|
68
86
|
const stringPatterns = [];
|
package/dist/urls.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isNullish } from "./is.js";
|
|
1
|
+
import { isNullish, isString } from "./is.js";
|
|
2
2
|
import { isAnyOf, toPatternList } from "./matching.js";
|
|
3
3
|
import { decodeHTMLStrict } from "entities";
|
|
4
4
|
// src/urls.ts
|
|
@@ -27,28 +27,48 @@ const getHostname = (url) => {
|
|
|
27
27
|
const nonAsciiRegex = /[\u0080-\uffff]/;
|
|
28
28
|
const normalizeDomain = (domain) => {
|
|
29
29
|
const trimmed = domain.trim();
|
|
30
|
-
const bare = trimmed.endsWith(".") ? trimmed.slice(0, -1) : trimmed;
|
|
30
|
+
const bare = trimmed.endsWith(".") ? trimmed.slice(0, -1).trimEnd() : trimmed;
|
|
31
31
|
if (!nonAsciiRegex.test(bare)) return bare;
|
|
32
32
|
return parseUrl(`http://${bare}`)?.hostname ?? bare;
|
|
33
33
|
};
|
|
34
|
-
const normalizeHostPattern = (pattern) => {
|
|
35
|
-
return pattern instanceof RegExp ? pattern : normalizeDomain(pattern);
|
|
36
|
-
};
|
|
37
34
|
const getParentDomains = (hostname) => {
|
|
38
35
|
const parents = [];
|
|
39
36
|
for (let index = hostname.indexOf("."); index !== -1; index = hostname.indexOf(".", index + 1)) parents.push(hostname.slice(index + 1));
|
|
40
37
|
return parents;
|
|
41
38
|
};
|
|
39
|
+
const keepValue = (value) => value;
|
|
40
|
+
const endsWithDomain = (lowerHostname, domain) => {
|
|
41
|
+
const lowerDomain = domain.toLowerCase();
|
|
42
|
+
if (lowerHostname.at(-lowerDomain.length - 1) !== ".") return false;
|
|
43
|
+
return !lowerDomain || isAnyOf(lowerHostname.slice(-lowerDomain.length), lowerDomain, keepValue);
|
|
44
|
+
};
|
|
42
45
|
const isHostOf = (url, hosts) => {
|
|
43
46
|
const hostname = getHostname(url);
|
|
44
47
|
if (!hostname) return false;
|
|
45
|
-
|
|
48
|
+
const lowerHostname = hostname.toLowerCase();
|
|
49
|
+
for (const host of toPatternList(hosts)) {
|
|
50
|
+
if (host instanceof RegExp) {
|
|
51
|
+
if (isAnyOf(hostname, host)) return true;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (isAnyOf(lowerHostname, normalizeDomain(host).toLowerCase(), keepValue)) return true;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
46
57
|
};
|
|
47
58
|
const isSubdomainOf = (url, domains) => {
|
|
48
59
|
const hostname = getHostname(url);
|
|
49
60
|
if (!hostname) return false;
|
|
50
|
-
const
|
|
51
|
-
|
|
61
|
+
const lowerHostname = hostname.toLowerCase();
|
|
62
|
+
const patterns = [];
|
|
63
|
+
for (const domain of toPatternList(domains)) {
|
|
64
|
+
if (domain instanceof RegExp) {
|
|
65
|
+
patterns.push(domain);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (endsWithDomain(lowerHostname, normalizeDomain(domain))) return true;
|
|
69
|
+
}
|
|
70
|
+
if (patterns.length === 0) return false;
|
|
71
|
+
return getParentDomains(hostname).some((parent) => isAnyOf(parent, patterns));
|
|
52
72
|
};
|
|
53
73
|
const isHttpUrl = (url) => {
|
|
54
74
|
const scheme = parseUrl(url)?.protocol;
|
|
@@ -61,10 +81,15 @@ const isHostOrSubdomainOf = (url, domains) => {
|
|
|
61
81
|
const getSubdomain = (url, domains) => {
|
|
62
82
|
const hostname = getHostname(url)?.toLowerCase();
|
|
63
83
|
if (!hostname) return;
|
|
64
|
-
|
|
84
|
+
let parents;
|
|
65
85
|
for (const domain of toPatternList(domains)) {
|
|
66
|
-
|
|
67
|
-
|
|
86
|
+
if (isString(domain)) {
|
|
87
|
+
const name = normalizeDomain(domain);
|
|
88
|
+
if (name && endsWithDomain(hostname, name)) return hostname.slice(0, -name.length - 1);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (!parents) parents = getParentDomains(hostname);
|
|
92
|
+
const parent = parents.find((candidate) => isAnyOf(candidate, domain));
|
|
68
93
|
if (parent) return hostname.slice(0, -parent.length - 1);
|
|
69
94
|
}
|
|
70
95
|
};
|
|
@@ -128,13 +153,13 @@ const readRawPairs = (text, prefix) => {
|
|
|
128
153
|
const readQuerySource = (source, options) => {
|
|
129
154
|
if (options?.decodeValues === false) return readRawPairs(source instanceof URL ? source.search : source?.toString(), "?");
|
|
130
155
|
if (source instanceof URL) return source.searchParams;
|
|
131
|
-
if (
|
|
156
|
+
if (isString(source)) return new URLSearchParams(source);
|
|
132
157
|
return source;
|
|
133
158
|
};
|
|
134
159
|
const readFragmentSource = (source, options) => {
|
|
135
160
|
if (options?.decodeValues === false) return readRawPairs(source instanceof URL ? source.hash : source?.toString(), "#");
|
|
136
161
|
if (source instanceof URL) return new URLSearchParams(source.hash.slice(1));
|
|
137
|
-
if (
|
|
162
|
+
if (isString(source)) return new URLSearchParams(source.startsWith("#") ? source.slice(1) : source);
|
|
138
163
|
return source;
|
|
139
164
|
};
|
|
140
165
|
const readParamValues = (params, name, options) => {
|
package/package.json
CHANGED