trousse 3.19.2 → 3.20.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/index.d.ts +4 -4
- package/dist/index.js +3 -3
- package/dist/matching.d.ts +3 -3
- package/dist/matching.js +38 -33
- package/dist/objects.d.ts +1 -0
- package/dist/objects.js +1 -1
- package/dist/types.d.ts +4 -0
- package/dist/urls.d.ts +10 -1
- package/dist/urls.js +48 -6
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -6,9 +6,9 @@ import { isJsonLike, isParseableJson, parseJson } from "./json.js";
|
|
|
6
6
|
import { escapeHtml, isLocale, isLocaleOrRegion, isLocaleSegment, isRegion, isRegionSegment, t, tx } from "./locales.js";
|
|
7
7
|
import { anyWordMatchesAnyOf, endsWithAnyOf, escapeRegex, getAnyOf, includesAnyOf, isAnyOf, isLooseMatch, isSameText, startsWithAnyOf } from "./matching.js";
|
|
8
8
|
import { createAcceptHeader, feedMimeTypes, genericFeedMimeTypes, genericHtmlMimeTypes, htmlMimeTypes } from "./mimes.js";
|
|
9
|
-
import { omit, pick, toMap, trimObject } from "./objects.js";
|
|
9
|
+
import { omit, pick, setKey, toMap, trimObject } from "./objects.js";
|
|
10
10
|
import { trimString } from "./strings.js";
|
|
11
11
|
import { sleep } from "./timers.js";
|
|
12
|
-
import { AnyOf, DeepOmit, IsPlainObject, LocaleOptions, LocaleSegmentOptions, MaybePromise, NormalizeOptions, Nullish, ParamOptions, ParamSource, PartialNullish, Pattern, QueryParamPredicate, RegistrableDomainOptions, TrimOptions, TrimPredicate, WeightedMimeType } from "./types.js";
|
|
13
|
-
import { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodePercent, decodeSegment, encodePathSegment, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, isBrandDomainOf, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isIpAddress, isSubdomainOf, isUtf8Binary, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripWww, trimUrlValues, upgradeProtocol, upgradeScheme } from "./urls.js";
|
|
14
|
-
export { type AnyOf, type DeepOmit, type IsPlainObject, type LocaleOptions, type LocaleSegmentOptions, type MaybePromise, type NormalizeOptions, type Nullish, type ParamOptions, type ParamSource, type PartialNullish, type Pattern, type QueryParamPredicate, type RegistrableDomainOptions, type TrimOptions, type TrimPredicate, type WeightedMimeType, addMissingProtocol, addMissingScheme, anyWordMatchesAnyOf, archiveExtensions, audioExtensions, codeExtensions, coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString, composeFragment, composeQuery, createAcceptHeader, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodePercent, decodeSegment, documentExtensions, encodePathSegment, endsWithAnyOf, escapeHtml, escapeRegex, feedExtensions, feedMimeTypes, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, flashExtensions, fontExtensions, genericFeedMimeTypes, genericHtmlMimeTypes, getAnyOf, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, htmlMimeTypes, imageExtensions, includesAnyOf, installerExtensions, isAnyOf, isBoolean, isBrandDomainOf, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isInteger, isIpAddress, isJsonLike, isLocale, isLocaleOrRegion, isLocaleSegment, isLooseMatch, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isRegion, isRegionSegment, isSameText, isString, isSubdomainOf, isUtf8Binary, isValidDate, normalizeUrl, omit, omitEmpty, pageExtensions, parseJson, parseUrl, pick, resolveFeedProtocol, resolveFeedScheme, resolveUrl, sleep, startsWithAnyOf, stripWww, subtitleExtensions, t, toMap, trimArray, trimObject, trimString, trimUrlValues, tx, upgradeProtocol, upgradeScheme, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions };
|
|
12
|
+
import { AnyOf, DeepOmit, HostPattern, IsPlainObject, LocaleOptions, LocaleSegmentOptions, MaybePromise, NormalizeOptions, Nullish, ParamOptions, ParamSource, PartialNullish, Pattern, QueryParamPredicate, RegistrableDomainOptions, TrimOptions, TrimPredicate, WeightedMimeType } from "./types.js";
|
|
13
|
+
import { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodeFormPart, decodePath, decodePercent, decodeSegment, encodePathSegment, filterQuery, fixEscapedAmpersands, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSequenceLength, getSubdomain, hasFragmentParam, hasQueryParam, isBrandDomainOf, isDeadHref, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isIpAddress, isSubdomainOf, isUtf8Binary, matchesHostPattern, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripUrlIgnorableChars, stripWww, toPercentEscape, trimUrlValues, upgradeProtocol, upgradeScheme } from "./urls.js";
|
|
14
|
+
export { type AnyOf, type DeepOmit, type HostPattern, type IsPlainObject, type LocaleOptions, type LocaleSegmentOptions, type MaybePromise, type NormalizeOptions, type Nullish, type ParamOptions, type ParamSource, type PartialNullish, type Pattern, type QueryParamPredicate, type RegistrableDomainOptions, type TrimOptions, type TrimPredicate, type WeightedMimeType, addMissingProtocol, addMissingScheme, anyWordMatchesAnyOf, archiveExtensions, audioExtensions, codeExtensions, coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString, composeFragment, composeQuery, createAcceptHeader, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodeFormPart, decodePath, decodePercent, decodeSegment, documentExtensions, encodePathSegment, endsWithAnyOf, escapeHtml, escapeRegex, feedExtensions, feedMimeTypes, filterQuery, fixEscapedAmpersands, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, flashExtensions, fontExtensions, genericFeedMimeTypes, genericHtmlMimeTypes, getAnyOf, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSequenceLength, getSubdomain, hasFragmentParam, hasQueryParam, htmlMimeTypes, imageExtensions, includesAnyOf, installerExtensions, isAnyOf, isBoolean, isBrandDomainOf, isDeadHref, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isInteger, isIpAddress, isJsonLike, isLocale, isLocaleOrRegion, isLocaleSegment, isLooseMatch, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isRegion, isRegionSegment, isSameText, isString, isSubdomainOf, isUtf8Binary, isValidDate, matchesHostPattern, normalizeUrl, omit, omitEmpty, pageExtensions, parseJson, parseUrl, pick, resolveFeedProtocol, resolveFeedScheme, resolveUrl, setKey, sleep, startsWithAnyOf, stripUrlIgnorableChars, stripWww, subtitleExtensions, t, toMap, toPercentEscape, trimArray, trimObject, trimString, trimUrlValues, tx, upgradeProtocol, upgradeScheme, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions };
|
package/dist/index.js
CHANGED
|
@@ -6,8 +6,8 @@ import { isJsonLike, isParseableJson, parseJson } from "./json.js";
|
|
|
6
6
|
import { escapeHtml, isLocale, isLocaleOrRegion, isLocaleSegment, isRegion, isRegionSegment, t, tx } from "./locales.js";
|
|
7
7
|
import { anyWordMatchesAnyOf, endsWithAnyOf, escapeRegex, getAnyOf, includesAnyOf, isAnyOf, isLooseMatch, isSameText, startsWithAnyOf } from "./matching.js";
|
|
8
8
|
import { createAcceptHeader, feedMimeTypes, genericFeedMimeTypes, genericHtmlMimeTypes, htmlMimeTypes } from "./mimes.js";
|
|
9
|
-
import { omit, pick, toMap, trimObject } from "./objects.js";
|
|
9
|
+
import { omit, pick, setKey, toMap, trimObject } from "./objects.js";
|
|
10
10
|
import { trimString } from "./strings.js";
|
|
11
11
|
import { sleep } from "./timers.js";
|
|
12
|
-
import { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodePercent, decodeSegment, encodePathSegment, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, isBrandDomainOf, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isIpAddress, isSubdomainOf, isUtf8Binary, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripWww, trimUrlValues, upgradeProtocol, upgradeScheme } from "./urls.js";
|
|
13
|
-
export { addMissingProtocol, addMissingScheme, anyWordMatchesAnyOf, archiveExtensions, audioExtensions, codeExtensions, coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString, composeFragment, composeQuery, createAcceptHeader, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodePercent, decodeSegment, documentExtensions, encodePathSegment, endsWithAnyOf, escapeHtml, escapeRegex, feedExtensions, feedMimeTypes, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, flashExtensions, fontExtensions, genericFeedMimeTypes, genericHtmlMimeTypes, getAnyOf, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, htmlMimeTypes, imageExtensions, includesAnyOf, installerExtensions, isAnyOf, isBoolean, isBrandDomainOf, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isInteger, isIpAddress, isJsonLike, isLocale, isLocaleOrRegion, isLocaleSegment, isLooseMatch, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isRegion, isRegionSegment, isSameText, isString, isSubdomainOf, isUtf8Binary, isValidDate, normalizeUrl, omit, omitEmpty, pageExtensions, parseJson, parseUrl, pick, resolveFeedProtocol, resolveFeedScheme, resolveUrl, sleep, startsWithAnyOf, stripWww, subtitleExtensions, t, toMap, trimArray, trimObject, trimString, trimUrlValues, tx, upgradeProtocol, upgradeScheme, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions };
|
|
12
|
+
import { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodeFormPart, decodePath, decodePercent, decodeSegment, encodePathSegment, filterQuery, fixEscapedAmpersands, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSequenceLength, getSubdomain, hasFragmentParam, hasQueryParam, isBrandDomainOf, isDeadHref, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isIpAddress, isSubdomainOf, isUtf8Binary, matchesHostPattern, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripUrlIgnorableChars, stripWww, toPercentEscape, trimUrlValues, upgradeProtocol, upgradeScheme } from "./urls.js";
|
|
13
|
+
export { addMissingProtocol, addMissingScheme, anyWordMatchesAnyOf, archiveExtensions, audioExtensions, codeExtensions, coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString, composeFragment, composeQuery, createAcceptHeader, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodeFormPart, decodePath, decodePercent, decodeSegment, documentExtensions, encodePathSegment, endsWithAnyOf, escapeHtml, escapeRegex, feedExtensions, feedMimeTypes, filterQuery, fixEscapedAmpersands, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, flashExtensions, fontExtensions, genericFeedMimeTypes, genericHtmlMimeTypes, getAnyOf, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSequenceLength, getSubdomain, hasFragmentParam, hasQueryParam, htmlMimeTypes, imageExtensions, includesAnyOf, installerExtensions, isAnyOf, isBoolean, isBrandDomainOf, isDeadHref, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isInteger, isIpAddress, isJsonLike, isLocale, isLocaleOrRegion, isLocaleSegment, isLooseMatch, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isRegion, isRegionSegment, isSameText, isString, isSubdomainOf, isUtf8Binary, isValidDate, matchesHostPattern, normalizeUrl, omit, omitEmpty, pageExtensions, parseJson, parseUrl, pick, resolveFeedProtocol, resolveFeedScheme, resolveUrl, setKey, sleep, startsWithAnyOf, stripUrlIgnorableChars, stripWww, subtitleExtensions, t, toMap, toPercentEscape, trimArray, trimObject, trimString, trimUrlValues, tx, upgradeProtocol, upgradeScheme, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions };
|
package/dist/matching.d.ts
CHANGED
|
@@ -6,6 +6,6 @@ export declare const isLooseMatch: (value: Nullish<string>, other: Nullish<strin
|
|
|
6
6
|
export declare const isSameText: typeof isLooseMatch;
|
|
7
7
|
export declare const getAnyOf: <T extends string>(value: Nullish<string>, patterns: ReadonlyArray<T>, parser?: (value: string) => string) => T | undefined;
|
|
8
8
|
export declare const includesAnyOf: (value: Nullish<string>, patterns: Pattern | ReadonlyArray<Pattern>, parser?: (value: string) => string) => boolean;
|
|
9
|
-
export declare const startsWithAnyOf: (value: Nullish<string>, patterns: Pattern | ReadonlyArray<Pattern
|
|
10
|
-
export declare const endsWithAnyOf: (value: Nullish<string>, patterns: Pattern | ReadonlyArray<Pattern
|
|
11
|
-
export declare const anyWordMatchesAnyOf: (value: string, patterns: ReadonlyArray<Pattern
|
|
9
|
+
export declare const startsWithAnyOf: (value: Nullish<string>, patterns: Pattern | ReadonlyArray<Pattern>, parser?: (value: string) => string) => boolean;
|
|
10
|
+
export declare const endsWithAnyOf: (value: Nullish<string>, patterns: Pattern | ReadonlyArray<Pattern>, parser?: (value: string) => string) => boolean;
|
|
11
|
+
export declare const anyWordMatchesAnyOf: (value: string, patterns: ReadonlyArray<Pattern>, parser?: (value: string) => string) => boolean;
|
package/dist/matching.js
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import { toPatternList } from "./internal.js";
|
|
2
2
|
import { isNullish, isString } from "./is.js";
|
|
3
|
+
import { trimString } from "./strings.js";
|
|
3
4
|
// src/matching.ts
|
|
4
5
|
const whitespaceRegex = /\s+/;
|
|
5
6
|
const regexSyntaxCharsRegex = /[.*+?^${}()|[\]\\/]/;
|
|
6
7
|
const regexEscapableCharsRegex = /[.*+?^${}()|[\]\\/&!#%,:;<=>@`~-]/g;
|
|
8
|
+
const foldText = (value) => {
|
|
9
|
+
return (trimString(value) ?? "").toLowerCase();
|
|
10
|
+
};
|
|
7
11
|
const testRegex = (regex, value) => {
|
|
8
12
|
regex.lastIndex = 0;
|
|
9
13
|
return regex.test(value);
|
|
10
14
|
};
|
|
11
|
-
const
|
|
12
|
-
|
|
15
|
+
const testRegexPattern = (regex, parsedValue, value, parser) => {
|
|
16
|
+
if (testRegex(regex, parsedValue)) return true;
|
|
17
|
+
if (parser !== foldText) return false;
|
|
18
|
+
const originalValue = trimString(value) ?? "";
|
|
19
|
+
return originalValue !== parsedValue && testRegex(regex, originalValue);
|
|
13
20
|
};
|
|
14
21
|
const escapeRegex = (value) => {
|
|
15
22
|
return value.replace(regexEscapableCharsRegex, (char) => {
|
|
@@ -17,72 +24,70 @@ const escapeRegex = (value) => {
|
|
|
17
24
|
return `\\x${char.charCodeAt(0).toString(16)}`;
|
|
18
25
|
});
|
|
19
26
|
};
|
|
20
|
-
const isAnyOf = (value, patterns, parser) => {
|
|
27
|
+
const isAnyOf = (value, patterns, parser = foldText) => {
|
|
21
28
|
if (isNullish(value)) return false;
|
|
22
|
-
const parsedValue = parser
|
|
23
|
-
if (isString(patterns)) return parsedValue === (
|
|
24
|
-
let originalValue;
|
|
29
|
+
const parsedValue = parser(value);
|
|
30
|
+
if (isString(patterns)) return parsedValue === parser(patterns);
|
|
25
31
|
for (const pattern of toPatternList(patterns)) {
|
|
26
32
|
if (pattern instanceof RegExp) {
|
|
27
|
-
if (
|
|
28
|
-
if (testRegexAnyCase(pattern, parsedValue, originalValue)) return true;
|
|
33
|
+
if (testRegexPattern(pattern, parsedValue, value, parser)) return true;
|
|
29
34
|
continue;
|
|
30
35
|
}
|
|
31
|
-
if (parsedValue === (
|
|
36
|
+
if (parsedValue === parser(pattern)) return true;
|
|
32
37
|
}
|
|
33
38
|
return false;
|
|
34
39
|
};
|
|
35
40
|
const isLooseMatch = (value, other) => {
|
|
36
41
|
if (isNullish(value) || isNullish(other)) return false;
|
|
37
|
-
return value
|
|
42
|
+
return foldText(value) === foldText(other);
|
|
38
43
|
};
|
|
39
44
|
/** @deprecated Use `isLooseMatch` instead. */
|
|
40
45
|
const isSameText = isLooseMatch;
|
|
41
|
-
const getAnyOf = (value, patterns, parser) => {
|
|
46
|
+
const getAnyOf = (value, patterns, parser = foldText) => {
|
|
42
47
|
if (isNullish(value)) return;
|
|
43
|
-
const parsedValue = parser
|
|
44
|
-
return patterns.find((pattern) =>
|
|
45
|
-
return parsedValue === (parser ? pattern : pattern.toLowerCase().trim());
|
|
46
|
-
});
|
|
48
|
+
const parsedValue = parser(value);
|
|
49
|
+
return patterns.find((pattern) => parsedValue === parser(pattern));
|
|
47
50
|
};
|
|
48
|
-
const includesAnyOf = (value, patterns, parser) => {
|
|
51
|
+
const includesAnyOf = (value, patterns, parser = foldText) => {
|
|
49
52
|
if (isNullish(value)) return false;
|
|
50
|
-
const parsedValue = parser
|
|
51
|
-
const originalValue = parser ? parsedValue : value;
|
|
53
|
+
const parsedValue = parser(value);
|
|
52
54
|
for (const pattern of toPatternList(patterns)) {
|
|
53
55
|
if (pattern instanceof RegExp) {
|
|
54
|
-
if (
|
|
56
|
+
if (testRegexPattern(pattern, parsedValue, value, parser)) return true;
|
|
55
57
|
continue;
|
|
56
58
|
}
|
|
57
|
-
|
|
59
|
+
const parsedPattern = parser(pattern);
|
|
60
|
+
if (parsedPattern && parsedValue.includes(parsedPattern)) return true;
|
|
58
61
|
}
|
|
59
62
|
return false;
|
|
60
63
|
};
|
|
61
|
-
const startsWithAnyOf = (value, patterns) => {
|
|
64
|
+
const startsWithAnyOf = (value, patterns, parser = foldText) => {
|
|
62
65
|
if (isNullish(value)) return false;
|
|
63
|
-
const
|
|
66
|
+
const parsedValue = parser(value);
|
|
64
67
|
for (const pattern of toPatternList(patterns)) {
|
|
65
68
|
if (pattern instanceof RegExp) {
|
|
66
|
-
if (
|
|
69
|
+
if (testRegexPattern(pattern, parsedValue, value, parser)) return true;
|
|
67
70
|
continue;
|
|
68
71
|
}
|
|
69
|
-
|
|
72
|
+
const parsedPattern = parser(pattern);
|
|
73
|
+
if (parsedPattern && parsedValue.startsWith(parsedPattern)) return true;
|
|
70
74
|
}
|
|
71
75
|
return false;
|
|
72
76
|
};
|
|
73
|
-
const endsWithAnyOf = (value, patterns) => {
|
|
77
|
+
const endsWithAnyOf = (value, patterns, parser = foldText) => {
|
|
74
78
|
if (isNullish(value)) return false;
|
|
75
|
-
const
|
|
79
|
+
const parsedValue = parser(value);
|
|
76
80
|
for (const pattern of toPatternList(patterns)) {
|
|
77
81
|
if (pattern instanceof RegExp) {
|
|
78
|
-
if (
|
|
82
|
+
if (testRegexPattern(pattern, parsedValue, value, parser)) return true;
|
|
79
83
|
continue;
|
|
80
84
|
}
|
|
81
|
-
|
|
85
|
+
const parsedPattern = parser(pattern);
|
|
86
|
+
if (parsedPattern && parsedValue.endsWith(parsedPattern)) return true;
|
|
82
87
|
}
|
|
83
88
|
return false;
|
|
84
89
|
};
|
|
85
|
-
const anyWordMatchesAnyOf = (value, patterns) => {
|
|
90
|
+
const anyWordMatchesAnyOf = (value, patterns, parser = foldText) => {
|
|
86
91
|
const stringPatterns = [];
|
|
87
92
|
const regexPatterns = [];
|
|
88
93
|
for (const pattern of patterns) {
|
|
@@ -90,14 +95,14 @@ const anyWordMatchesAnyOf = (value, patterns) => {
|
|
|
90
95
|
regexPatterns.push(pattern);
|
|
91
96
|
continue;
|
|
92
97
|
}
|
|
93
|
-
const stringPattern = pattern
|
|
98
|
+
const stringPattern = parser(pattern);
|
|
94
99
|
if (stringPattern) stringPatterns.push(stringPattern);
|
|
95
100
|
}
|
|
96
101
|
const words = value.split(whitespaceRegex);
|
|
97
102
|
for (const word of words) {
|
|
98
|
-
const
|
|
99
|
-
for (const stringPattern of stringPatterns) if (
|
|
100
|
-
for (const regexPattern of regexPatterns) if (
|
|
103
|
+
const parsedWord = parser(word);
|
|
104
|
+
for (const stringPattern of stringPatterns) if (parsedWord === stringPattern) return true;
|
|
105
|
+
for (const regexPattern of regexPatterns) if (testRegexPattern(regexPattern, parsedWord, word, parser)) return true;
|
|
101
106
|
}
|
|
102
107
|
return false;
|
|
103
108
|
};
|
package/dist/objects.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AnyOf, TrimOptions, TrimPredicate } from "./types.js";
|
|
2
|
+
export declare const setKey: <T extends object, K extends keyof T>(object: T, key: K, value: T[K]) => void;
|
|
2
3
|
export declare const pick: <T extends object, K extends keyof T>(object: T, keys: ReadonlyArray<K>) => Pick<T, K>;
|
|
3
4
|
export declare const omit: <T extends object, K extends keyof T>(object: T, keys: ReadonlyArray<K>) => Omit<T, K>;
|
|
4
5
|
type TrimObject = {
|
package/dist/objects.js
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -5,6 +5,10 @@ export type Nullish<T> = T | null | undefined;
|
|
|
5
5
|
export type PartialNullish<T> = { [Key in keyof T]?: Nullish<T[Key]>; };
|
|
6
6
|
export type MaybePromise<T> = T | Promise<T>;
|
|
7
7
|
export type Pattern = string | RegExp;
|
|
8
|
+
export type HostPattern = {
|
|
9
|
+
host: string | ReadonlyArray<string>;
|
|
10
|
+
path?: RegExp;
|
|
11
|
+
};
|
|
8
12
|
export type ParamSource = URL | URLSearchParams | string;
|
|
9
13
|
export type ParamOptions = {
|
|
10
14
|
decodeValues?: boolean;
|
package/dist/urls.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NormalizeOptions, Nullish, ParamOptions, ParamSource, Pattern, RegistrableDomainOptions } from "./types.js";
|
|
1
|
+
import { HostPattern, NormalizeOptions, Nullish, ParamOptions, ParamSource, Pattern, RegistrableDomainOptions } from "./types.js";
|
|
2
2
|
export declare const parseUrl: (value: Nullish<string | URL>, base?: string | URL) => URL | undefined;
|
|
3
3
|
export declare const getPathSegments: (value: string | URL) => Array<string>;
|
|
4
4
|
export declare const getOrigin: (value: Nullish<string | URL>) => string | undefined;
|
|
@@ -6,15 +6,22 @@ export declare const isHostOf: (url: string | URL, hosts: Pattern | ReadonlyArra
|
|
|
6
6
|
export declare const isSubdomainOf: (url: string | URL, domains: Pattern | ReadonlyArray<Pattern>) => boolean;
|
|
7
7
|
export declare const isHttpUrl: (url: string | URL) => boolean;
|
|
8
8
|
export declare const isHostOrSubdomainOf: (url: string | URL, domains: Pattern | ReadonlyArray<Pattern>) => boolean;
|
|
9
|
+
export declare const matchesHostPattern: (value: string, patterns: ReadonlyArray<HostPattern>) => boolean;
|
|
10
|
+
export declare const stripUrlIgnorableChars: (url: string) => string;
|
|
11
|
+
export declare const isDeadHref: (href: string) => boolean;
|
|
9
12
|
export declare const getSubdomain: (url: string | URL, domains: Pattern | ReadonlyArray<Pattern>) => string | undefined;
|
|
10
13
|
export declare const decodeSegment: (segment: string | undefined) => string | undefined;
|
|
14
|
+
export declare const getSequenceLength: (byte: number) => number;
|
|
11
15
|
export declare const decodePercent: (value: string) => string;
|
|
16
|
+
export declare const toPercentEscape: (character: string) => string;
|
|
17
|
+
export declare const decodePath: (path: string) => string;
|
|
12
18
|
export declare const decodeBase64Binary: (value: string) => string | undefined;
|
|
13
19
|
export declare const isUtf8Binary: (binary: string) => boolean;
|
|
14
20
|
export declare const decodeBase64: (value: string) => string | undefined;
|
|
15
21
|
export declare const decodeBase64Url: (value: string) => string | undefined;
|
|
16
22
|
export declare const decodeBase64UrlBinary: (value: string) => string | undefined;
|
|
17
23
|
export declare const encodePathSegment: (value: string) => string;
|
|
24
|
+
export declare const decodeFormPart: (part: string) => string;
|
|
18
25
|
export declare const hasQueryParam: (source: ParamSource | undefined, name: string) => boolean;
|
|
19
26
|
export declare const getQueryParamValues: (source: ParamSource | undefined, name: string, options?: ParamOptions) => Array<string>;
|
|
20
27
|
export declare const getQueryParam: (source: ParamSource | undefined, name: string, options?: ParamOptions) => string | undefined;
|
|
@@ -26,9 +33,11 @@ export declare const getFragmentParams: (source: ParamSource | undefined, names:
|
|
|
26
33
|
type QueryValue = string | number | true;
|
|
27
34
|
export declare const composeQuery: (params?: Record<string, QueryValue>) => string;
|
|
28
35
|
export declare const composeFragment: (params?: Record<string, QueryValue>) => string;
|
|
36
|
+
export declare const filterQuery: (url: URL, isKept: (name: string) => boolean) => string;
|
|
29
37
|
export declare const fixMalformedScheme: (url: string) => string;
|
|
30
38
|
export declare const fixMalformedHost: (url: string) => string;
|
|
31
39
|
export declare const fixMalformedQuery: (url: string) => string;
|
|
40
|
+
export declare const fixEscapedAmpersands: (url: string) => string;
|
|
32
41
|
export declare const trimUrlValues: (url: string) => string;
|
|
33
42
|
export declare const resolveFeedScheme: (url: string, scheme?: 'http' | 'https') => string;
|
|
34
43
|
export declare const addMissingScheme: (url: string, scheme?: 'http' | 'https') => string;
|
package/dist/urls.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { toPatternList } from "./internal.js";
|
|
2
2
|
import { isFunction, isNullish, isString } from "./is.js";
|
|
3
3
|
import { includesAnyOf, isAnyOf } from "./matching.js";
|
|
4
|
+
import { trimString } from "./strings.js";
|
|
4
5
|
import { decodeHTMLStrict } from "entities";
|
|
5
6
|
// src/urls.ts
|
|
6
7
|
const httpSchemes = ["http:", "https:"];
|
|
8
|
+
const placeholderBaseUrl = "https://placeholder.invalid";
|
|
7
9
|
const parseUrl = (value, base) => {
|
|
8
10
|
if (isNullish(value)) return;
|
|
9
11
|
if (value instanceof URL && base === void 0) return value;
|
|
@@ -90,6 +92,24 @@ const isHttpUrl = (url) => {
|
|
|
90
92
|
const isHostOrSubdomainOf = (url, domains) => {
|
|
91
93
|
return isHostOf(url, domains) || isSubdomainOf(url, domains);
|
|
92
94
|
};
|
|
95
|
+
const matchesHostPattern = (value, patterns) => {
|
|
96
|
+
const url = parseUrl(value, placeholderBaseUrl);
|
|
97
|
+
if (!url) return false;
|
|
98
|
+
return patterns.some((pattern) => {
|
|
99
|
+
return isHostOrSubdomainOf(url, pattern.host) && (!pattern.path || url.pathname.search(pattern.path) !== -1);
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
const c0ControlOrSpaceClass = "[\\x00-\\x20]+";
|
|
103
|
+
const urlEdgeCharsRegex = new RegExp(`^${c0ControlOrSpaceClass}|${c0ControlOrSpaceClass}$`, "g");
|
|
104
|
+
const urlTabOrNewlineRegex = /[\t\n\r]/g;
|
|
105
|
+
const stripUrlIgnorableChars = (url) => {
|
|
106
|
+
return url.replace(urlEdgeCharsRegex, "").replace(urlTabOrNewlineRegex, "");
|
|
107
|
+
};
|
|
108
|
+
const javascriptSchemeRegex = /^javascript:/i;
|
|
109
|
+
const deadHrefs = ["", "#"];
|
|
110
|
+
const isDeadHref = (href) => {
|
|
111
|
+
return deadHrefs.includes(href.trim()) || javascriptSchemeRegex.test(stripUrlIgnorableChars(href));
|
|
112
|
+
};
|
|
93
113
|
const getSubdomain = (url, domains) => {
|
|
94
114
|
const hostname = getHostname(url)?.toLowerCase();
|
|
95
115
|
if (!hostname) return;
|
|
@@ -142,9 +162,19 @@ const decodeEscapeRun = (run) => {
|
|
|
142
162
|
const decodePercent = (value) => {
|
|
143
163
|
return value.replace(escapeRunRegex, decodeEscapeRun);
|
|
144
164
|
};
|
|
165
|
+
const toPercentEscape = (character) => {
|
|
166
|
+
return `%${character.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}`;
|
|
167
|
+
};
|
|
168
|
+
const decodePathSegment = (segment) => {
|
|
169
|
+
return decodePercent(segment).replaceAll("%", "%25").replaceAll("/", "%2F");
|
|
170
|
+
};
|
|
171
|
+
const decodePath = (path) => {
|
|
172
|
+
if (!path.includes("%")) return path;
|
|
173
|
+
return path.split("/").map(decodePathSegment).join("/");
|
|
174
|
+
};
|
|
145
175
|
const decodeBase64Binary = (value) => {
|
|
146
176
|
try {
|
|
147
|
-
return atob(value
|
|
177
|
+
return atob(value);
|
|
148
178
|
} catch {}
|
|
149
179
|
};
|
|
150
180
|
const utf8Decoder = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
|
|
@@ -161,12 +191,11 @@ const decodeBase64 = (value) => {
|
|
|
161
191
|
if (binary === void 0) return;
|
|
162
192
|
let encoded = "";
|
|
163
193
|
for (let index = 0; index < binary.length; index++) {
|
|
164
|
-
|
|
165
|
-
if (byte < 128 && !binary.startsWith("%", index)) {
|
|
194
|
+
if (binary.charCodeAt(index) < 128 && !binary.startsWith("%", index)) {
|
|
166
195
|
encoded += binary[index];
|
|
167
196
|
continue;
|
|
168
197
|
}
|
|
169
|
-
encoded +=
|
|
198
|
+
encoded += toPercentEscape(binary[index]);
|
|
170
199
|
}
|
|
171
200
|
return decodePercent(encoded);
|
|
172
201
|
};
|
|
@@ -289,6 +318,13 @@ const composeFragment = (params) => {
|
|
|
289
318
|
const pairs = composePairs(params);
|
|
290
319
|
return pairs ? `#${pairs}` : "";
|
|
291
320
|
};
|
|
321
|
+
const filterQuery = (url, isKept) => {
|
|
322
|
+
const pairs = url.search.slice(1).split("&").filter((pair) => {
|
|
323
|
+
const [name] = [...new URLSearchParams(pair).keys()];
|
|
324
|
+
return !!name && isKept(name);
|
|
325
|
+
});
|
|
326
|
+
return pairs.length > 0 ? `?${pairs.join("&")}` : "";
|
|
327
|
+
};
|
|
292
328
|
const strippedParamsCache = /* @__PURE__ */ new WeakMap();
|
|
293
329
|
const getStrippedParamsSet = (params) => {
|
|
294
330
|
let cached = strippedParamsCache.get(params);
|
|
@@ -311,6 +347,7 @@ const plusRegex = /\+/g;
|
|
|
311
347
|
const httpSchemeRegex = /^http:\/\//i;
|
|
312
348
|
const httpsSchemeRegex = /^https:\/\//i;
|
|
313
349
|
const repeatedSlashesRegex = /(?<!:|%3a)\/{2,}/gi;
|
|
350
|
+
const pathSlashRunRegex = /^https?:\/\/[^/?#]+(?=\/)[^?#]*\/\//i;
|
|
314
351
|
const leadingWhitespaceChars = [
|
|
315
352
|
" ",
|
|
316
353
|
" ",
|
|
@@ -440,6 +477,10 @@ const fixMalformedQuery = (url) => {
|
|
|
440
477
|
if (!hasNestedUrl) query = query.replace(lostSeparatorRegex, "&").replace(slashClosedValueRegex, "$1");
|
|
441
478
|
return `${head.slice(0, queryIndex)}?${query}${head.slice(ownEnd)}${fragment}`;
|
|
442
479
|
};
|
|
480
|
+
const escapedAmpersandRegex = /&(?:amp;)*%23038;|&(?:amp;)+/g;
|
|
481
|
+
const fixEscapedAmpersands = (url) => {
|
|
482
|
+
return url.replace(escapedAmpersandRegex, "&");
|
|
483
|
+
};
|
|
443
484
|
const segmentEdgeSpace = "(?:\\s|%(?:09|0A|0D|20))";
|
|
444
485
|
const valueEdgeSpace = "(?:\\s|\\+|%(?:09|0A|0D|20))";
|
|
445
486
|
const segmentEdgeSpaceRegex = new RegExp(`^${segmentEdgeSpace}+|(?<!${segmentEdgeSpace})${segmentEdgeSpace}+$`, "gi");
|
|
@@ -522,7 +563,7 @@ const upgradeScheme = (url, scheme = "https") => {
|
|
|
522
563
|
return parsed.href;
|
|
523
564
|
};
|
|
524
565
|
const resolveUrl = (url, base) => {
|
|
525
|
-
const trimmedUrl = url
|
|
566
|
+
const trimmedUrl = trimString(url);
|
|
526
567
|
if (!trimmedUrl) return;
|
|
527
568
|
if (trimmedUrl.startsWith("#") && !base) return;
|
|
528
569
|
let resolvedUrl;
|
|
@@ -591,6 +632,7 @@ const lowercaseQueryPair = (pair) => {
|
|
|
591
632
|
});
|
|
592
633
|
};
|
|
593
634
|
const normalizeUrl = (url, options) => {
|
|
635
|
+
if (options.collapseSlashes && Object.entries(options).every(([key, value]) => key === "collapseSlashes" || !value) && !pathSlashRunRegex.test(url)) return url;
|
|
594
636
|
const parsed = parseUrl(options.normalizeUnicode ? url.normalize("NFC") : url);
|
|
595
637
|
if (!parsed) return url;
|
|
596
638
|
if (options.stripHostTrailingDot && parsed.hostname.endsWith(".")) parsed.hostname = parsed.hostname.slice(0, -1);
|
|
@@ -787,4 +829,4 @@ const resolveFeedProtocol = resolveFeedScheme;
|
|
|
787
829
|
const addMissingProtocol = addMissingScheme;
|
|
788
830
|
/** @deprecated Use `upgradeScheme` instead. */
|
|
789
831
|
const upgradeProtocol = upgradeScheme;
|
|
790
|
-
export { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodePercent, decodeSegment, encodePathSegment, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, isBrandDomainOf, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isIpAddress, isSubdomainOf, isUtf8Binary, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripWww, trimUrlValues, upgradeProtocol, upgradeScheme };
|
|
832
|
+
export { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodeFormPart, decodePath, decodePercent, decodeSegment, encodePathSegment, filterQuery, fixEscapedAmpersands, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSequenceLength, getSubdomain, hasFragmentParam, hasQueryParam, isBrandDomainOf, isDeadHref, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isIpAddress, isSubdomainOf, isUtf8Binary, matchesHostPattern, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripUrlIgnorableChars, stripWww, toPercentEscape, trimUrlValues, upgradeProtocol, upgradeScheme };
|
package/package.json
CHANGED