trousse 3.8.0 → 3.10.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/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/is.d.ts +5 -2
- package/dist/locales.d.ts +2 -1
- package/dist/locales.js +19 -1
- package/dist/matching.js +37 -19
- package/dist/urls.d.ts +5 -0
- package/dist/urls.js +116 -54
- package/package.json +2 -2
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/index.d.ts
CHANGED
|
@@ -3,11 +3,11 @@ import { coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, c
|
|
|
3
3
|
import { archiveExtensions, audioExtensions, codeExtensions, documentExtensions, flashExtensions, fontExtensions, imageExtensions, installerExtensions, subtitleExtensions, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions } from "./files.js";
|
|
4
4
|
import { isBoolean, isFiniteNumber, isFunction, isInteger, isNonEmptyString, isNullish, isNumber, isObject, isPlainObject, isPresent, isString, isValidDate } from "./is.js";
|
|
5
5
|
import { isJsonLike, isParseableJson, parseJson } from "./json.js";
|
|
6
|
-
import { escapeHtml, t, tx } from "./locales.js";
|
|
6
|
+
import { escapeHtml, isLocaleSegment, t, tx } from "./locales.js";
|
|
7
7
|
import { anyWordMatchesAnyOf, endsWithAnyOf, escapeRegex, getAnyOf, includesAnyOf, isAnyOf, isSameText, startsWithAnyOf } from "./matching.js";
|
|
8
8
|
import { createAcceptHeader, feedMimeTypes, genericFeedMimeTypes, genericHtmlMimeTypes, htmlMimeTypes } from "./mimes.js";
|
|
9
9
|
import { omit, pick, toMap, trimObject } from "./objects.js";
|
|
10
10
|
import { sleep } from "./timers.js";
|
|
11
11
|
import { AnyOf, DeepOmit, IsPlainObject, MaybePromise, NormalizeOptions, Nullish, ParamOptions, ParamSource, PartialNullish, Pattern, RegistrableDomainOptions, WeightedMimeType } from "./types.js";
|
|
12
|
-
import { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodePercent, decodeSegment, encodePathSegment, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, isHostOf, isHostOrSubdomainOf, isHttpUrl, isIpAddress, isSubdomainOf, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripWww, trimUrlValues, upgradeProtocol, upgradeScheme } from "./urls.js";
|
|
13
|
-
export { type AnyOf, type DeepOmit, type IsPlainObject, type MaybePromise, type NormalizeOptions, type Nullish, type ParamOptions, type ParamSource, type PartialNullish, type Pattern, type RegistrableDomainOptions, type WeightedMimeType, addMissingProtocol, addMissingScheme, anyWordMatchesAnyOf, archiveExtensions, audioExtensions, codeExtensions, coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString, composeFragment, composeQuery, createAcceptHeader, createUrlSpellings, createUrlVariants, decodePercent, decodeSegment, documentExtensions, encodePathSegment, endsWithAnyOf, escapeHtml, escapeRegex, feedMimeTypes, 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, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHttpUrl, isInteger, isIpAddress, isJsonLike, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isSameText, isString, isSubdomainOf, isValidDate, normalizeUrl, omit, omitEmpty, parseJson, parseUrl, pick, resolveFeedProtocol, resolveFeedScheme, resolveUrl, sleep, startsWithAnyOf, stripWww, subtitleExtensions, t, toMap, trimObject, trimUrlValues, tx, upgradeProtocol, upgradeScheme, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions };
|
|
12
|
+
import { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodePercent, decodeSegment, encodePathSegment, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, isHostOf, isHostOrSubdomainOf, isHttpUrl, isIpAddress, isSubdomainOf, isUtf8Binary, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripWww, trimUrlValues, upgradeProtocol, upgradeScheme } from "./urls.js";
|
|
13
|
+
export { type AnyOf, type DeepOmit, type IsPlainObject, type MaybePromise, type NormalizeOptions, type Nullish, type ParamOptions, type ParamSource, type PartialNullish, type Pattern, type RegistrableDomainOptions, 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, feedMimeTypes, 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, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHttpUrl, isInteger, isIpAddress, isJsonLike, isLocaleSegment, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isSameText, isString, isSubdomainOf, isUtf8Binary, isValidDate, normalizeUrl, omit, omitEmpty, parseJson, parseUrl, pick, resolveFeedProtocol, resolveFeedScheme, resolveUrl, sleep, startsWithAnyOf, stripWww, subtitleExtensions, t, toMap, trimObject, trimUrlValues, tx, upgradeProtocol, upgradeScheme, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions };
|
package/dist/index.js
CHANGED
|
@@ -3,10 +3,10 @@ import { coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, c
|
|
|
3
3
|
import { archiveExtensions, audioExtensions, codeExtensions, documentExtensions, flashExtensions, fontExtensions, imageExtensions, installerExtensions, subtitleExtensions, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions } from "./files.js";
|
|
4
4
|
import { isBoolean, isFiniteNumber, isFunction, isInteger, isNonEmptyString, isNullish, isNumber, isObject, isPlainObject, isPresent, isString, isValidDate } from "./is.js";
|
|
5
5
|
import { isJsonLike, isParseableJson, parseJson } from "./json.js";
|
|
6
|
-
import { escapeHtml, t, tx } from "./locales.js";
|
|
6
|
+
import { escapeHtml, isLocaleSegment, t, tx } from "./locales.js";
|
|
7
7
|
import { anyWordMatchesAnyOf, endsWithAnyOf, escapeRegex, getAnyOf, includesAnyOf, isAnyOf, isSameText, startsWithAnyOf } from "./matching.js";
|
|
8
8
|
import { createAcceptHeader, feedMimeTypes, genericFeedMimeTypes, genericHtmlMimeTypes, htmlMimeTypes } from "./mimes.js";
|
|
9
9
|
import { omit, pick, toMap, trimObject } from "./objects.js";
|
|
10
10
|
import { sleep } from "./timers.js";
|
|
11
|
-
import { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodePercent, decodeSegment, encodePathSegment, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, isHostOf, isHostOrSubdomainOf, isHttpUrl, isIpAddress, isSubdomainOf, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripWww, trimUrlValues, upgradeProtocol, upgradeScheme } from "./urls.js";
|
|
12
|
-
export { addMissingProtocol, addMissingScheme, anyWordMatchesAnyOf, archiveExtensions, audioExtensions, codeExtensions, coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString, composeFragment, composeQuery, createAcceptHeader, createUrlSpellings, createUrlVariants, decodePercent, decodeSegment, documentExtensions, encodePathSegment, endsWithAnyOf, escapeHtml, escapeRegex, feedMimeTypes, 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, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHttpUrl, isInteger, isIpAddress, isJsonLike, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isSameText, isString, isSubdomainOf, isValidDate, normalizeUrl, omit, omitEmpty, parseJson, parseUrl, pick, resolveFeedProtocol, resolveFeedScheme, resolveUrl, sleep, startsWithAnyOf, stripWww, subtitleExtensions, t, toMap, trimObject, trimUrlValues, tx, upgradeProtocol, upgradeScheme, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions };
|
|
11
|
+
import { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodePercent, decodeSegment, encodePathSegment, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, isHostOf, isHostOrSubdomainOf, isHttpUrl, isIpAddress, isSubdomainOf, isUtf8Binary, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripWww, trimUrlValues, upgradeProtocol, upgradeScheme } from "./urls.js";
|
|
12
|
+
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, feedMimeTypes, 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, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHttpUrl, isInteger, isIpAddress, isJsonLike, isLocaleSegment, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isSameText, isString, isSubdomainOf, isUtf8Binary, isValidDate, normalizeUrl, omit, omitEmpty, parseJson, parseUrl, pick, resolveFeedProtocol, resolveFeedScheme, resolveUrl, sleep, startsWithAnyOf, stripWww, subtitleExtensions, t, toMap, trimObject, trimUrlValues, tx, upgradeProtocol, upgradeScheme, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions };
|
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/locales.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export declare const escapeHtml: (value: string) => string;
|
|
2
2
|
export declare const t: (template: string, params: Record<string, string | number>) => string;
|
|
3
|
-
export declare const tx: (template: string, params: Record<string, string | number>) => string;
|
|
3
|
+
export declare const tx: (template: string, params: Record<string, string | number>) => string;
|
|
4
|
+
export declare const isLocaleSegment: (segment: string) => boolean;
|
package/dist/locales.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isAnyOf } from "./matching.js";
|
|
1
2
|
// src/locales.ts
|
|
2
3
|
const placeholderRegex = /\{\{(\w+)\}\}/g;
|
|
3
4
|
const escapableRegex = /[&<>"']/g;
|
|
@@ -22,4 +23,21 @@ const tx = (template, params) => {
|
|
|
22
23
|
return Object.hasOwn(params, key) ? escapeHtml(String(params[key])) : placeholder;
|
|
23
24
|
});
|
|
24
25
|
};
|
|
25
|
-
|
|
26
|
+
const languageNames = /* @__PURE__ */ new Intl.DisplayNames(["en"], {
|
|
27
|
+
type: "language",
|
|
28
|
+
fallback: "none"
|
|
29
|
+
});
|
|
30
|
+
const specialPurposeCodes = [
|
|
31
|
+
"und",
|
|
32
|
+
"mul",
|
|
33
|
+
"zxx"
|
|
34
|
+
];
|
|
35
|
+
const isLocaleSegment = (segment) => {
|
|
36
|
+
if (isAnyOf(segment, specialPurposeCodes)) return false;
|
|
37
|
+
try {
|
|
38
|
+
return languageNames.of(segment) !== void 0;
|
|
39
|
+
} catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
export { escapeHtml, isLocaleSegment, t, tx };
|
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.d.ts
CHANGED
|
@@ -9,6 +9,11 @@ export declare const isHostOrSubdomainOf: (url: string | URL, domains: Pattern |
|
|
|
9
9
|
export declare const getSubdomain: (url: string | URL, domains: Pattern | ReadonlyArray<Pattern>) => string | undefined;
|
|
10
10
|
export declare const decodeSegment: (segment: string | undefined) => string | undefined;
|
|
11
11
|
export declare const decodePercent: (value: string) => string;
|
|
12
|
+
export declare const decodeBase64Binary: (value: string) => string | undefined;
|
|
13
|
+
export declare const isUtf8Binary: (binary: string) => boolean;
|
|
14
|
+
export declare const decodeBase64: (value: string) => string | undefined;
|
|
15
|
+
export declare const decodeBase64Url: (value: string) => string | undefined;
|
|
16
|
+
export declare const decodeBase64UrlBinary: (value: string) => string | undefined;
|
|
12
17
|
export declare const encodePathSegment: (value: string) => string;
|
|
13
18
|
export declare const hasQueryParam: (source: ParamSource | undefined, name: string) => boolean;
|
|
14
19
|
export declare const getQueryParamValues: (source: ParamSource | undefined, name: string, options?: ParamOptions) => Array<string>;
|
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
|
};
|
|
@@ -105,6 +130,43 @@ const decodeEscapeRun = (run) => {
|
|
|
105
130
|
const decodePercent = (value) => {
|
|
106
131
|
return value.replace(escapeRunRegex, decodeEscapeRun);
|
|
107
132
|
};
|
|
133
|
+
const decodeBase64Binary = (value) => {
|
|
134
|
+
try {
|
|
135
|
+
return atob(value);
|
|
136
|
+
} catch {}
|
|
137
|
+
};
|
|
138
|
+
const utf8Decoder = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
|
|
139
|
+
const isUtf8Binary = (binary) => {
|
|
140
|
+
try {
|
|
141
|
+
utf8Decoder.decode(Uint8Array.from(binary, (char) => char.charCodeAt(0)));
|
|
142
|
+
return true;
|
|
143
|
+
} catch {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const decodeBase64 = (value) => {
|
|
148
|
+
const binary = decodeBase64Binary(value);
|
|
149
|
+
if (binary === void 0) return;
|
|
150
|
+
let encoded = "";
|
|
151
|
+
for (let index = 0; index < binary.length; index++) {
|
|
152
|
+
const byte = binary.charCodeAt(index);
|
|
153
|
+
if (byte < 128 && binary[index] !== "%") {
|
|
154
|
+
encoded += binary[index];
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
encoded += `%${byte.toString(16).toUpperCase().padStart(2, "0")}`;
|
|
158
|
+
}
|
|
159
|
+
return decodePercent(encoded);
|
|
160
|
+
};
|
|
161
|
+
const toBase64 = (base64Url) => {
|
|
162
|
+
return base64Url.replaceAll("-", "+").replaceAll("_", "/");
|
|
163
|
+
};
|
|
164
|
+
const decodeBase64Url = (value) => {
|
|
165
|
+
return decodeBase64(toBase64(value));
|
|
166
|
+
};
|
|
167
|
+
const decodeBase64UrlBinary = (value) => {
|
|
168
|
+
return decodeBase64Binary(toBase64(value));
|
|
169
|
+
};
|
|
108
170
|
const pathSegmentEscapeRegex = /[/?#\\\s]/g;
|
|
109
171
|
const encodePathSegment = (value) => {
|
|
110
172
|
return value.replace(pathSegmentEscapeRegex, (character) => encodeURIComponent(character));
|
|
@@ -128,13 +190,13 @@ const readRawPairs = (text, prefix) => {
|
|
|
128
190
|
const readQuerySource = (source, options) => {
|
|
129
191
|
if (options?.decodeValues === false) return readRawPairs(source instanceof URL ? source.search : source?.toString(), "?");
|
|
130
192
|
if (source instanceof URL) return source.searchParams;
|
|
131
|
-
if (
|
|
193
|
+
if (isString(source)) return new URLSearchParams(source);
|
|
132
194
|
return source;
|
|
133
195
|
};
|
|
134
196
|
const readFragmentSource = (source, options) => {
|
|
135
197
|
if (options?.decodeValues === false) return readRawPairs(source instanceof URL ? source.hash : source?.toString(), "#");
|
|
136
198
|
if (source instanceof URL) return new URLSearchParams(source.hash.slice(1));
|
|
137
|
-
if (
|
|
199
|
+
if (isString(source)) return new URLSearchParams(source.startsWith("#") ? source.slice(1) : source);
|
|
138
200
|
return source;
|
|
139
201
|
};
|
|
140
202
|
const readParamValues = (params, name, options) => {
|
|
@@ -251,25 +313,28 @@ const validUrlRegex = /^https?:\/\/(?:www\.|[a-vx-z0-9])/i;
|
|
|
251
313
|
const schemeSeparator = String.raw`\.*[:\s=\\/][:\s=.\\/]*`;
|
|
252
314
|
const separatorChar = String.raw`(?:[:\s=.\\/]|%20|%c2%a0)`;
|
|
253
315
|
const leadingSchemeSeparator = String.raw`\.*(?::${separatorChar}*|[\s=\\/]${separatorChar}+)`;
|
|
254
|
-
const doubledSchemeRegex = /* @__PURE__ */ new RegExp(String.raw`^\/?[htps]{2,7}${leadingSchemeSeparator}([htps]{2,7})(?!:\d|[^/?#\\]*@)(${schemeSeparator})[.,:/]*(www[./]+)?`, "i");
|
|
255
|
-
const singleMalformedRegex = /* @__PURE__ */ new RegExp(String.raw`^\/?(h[htps():]{1,10}|t{1,2}ps?)(${leadingSchemeSeparator})[.,:/]*(www[./]+)?`, "i");
|
|
316
|
+
const doubledSchemeRegex = /* @__PURE__ */ new RegExp(String.raw`^\/?[htps]{2,7}${leadingSchemeSeparator}([htps]{2,7})(?!:\d|[^/?#\\]*@)(${schemeSeparator})([.,:/]*)(www[./]+)?`, "i");
|
|
317
|
+
const singleMalformedRegex = /* @__PURE__ */ new RegExp(String.raw`^\/?(h[htps():]{1,10}|t{1,2}ps?)(${leadingSchemeSeparator})([.,:/]*)(www[./]+)?`, "i");
|
|
256
318
|
const isSchemeToken = (token, separator) => {
|
|
257
319
|
const lowered = token.toLowerCase();
|
|
258
320
|
return `${token}${separator}`.includes(":") || lowered.includes("t") && lowered.includes("p");
|
|
259
321
|
};
|
|
322
|
+
const brokenSlashEscapeRegex = /^[^/]*\/[,.][^/]*$/;
|
|
260
323
|
const fixMalformedScheme = (url) => {
|
|
261
324
|
if (validUrlRegex.test(url) && !doubledSchemeRegex.test(url)) return url;
|
|
262
325
|
const doubledMatch = doubledSchemeRegex.exec(url);
|
|
263
326
|
if (doubledMatch && isSchemeToken(doubledMatch[1], doubledMatch[2])) {
|
|
327
|
+
if (brokenSlashEscapeRegex.test(`${doubledMatch[2]}${doubledMatch[3]}`)) return url;
|
|
264
328
|
const inner = doubledMatch[1];
|
|
265
|
-
const www = doubledMatch[
|
|
329
|
+
const www = doubledMatch[4];
|
|
266
330
|
const rest = url.slice(doubledMatch[0].length);
|
|
267
331
|
return `${httpsLetterRegex.test(inner) ? "https://" : "http://"}${www ? "www." : ""}${rest}`;
|
|
268
332
|
}
|
|
269
333
|
const singleMatch = singleMalformedRegex.exec(url);
|
|
270
334
|
if (singleMatch && isSchemeToken(singleMatch[1], singleMatch[2])) {
|
|
335
|
+
if (brokenSlashEscapeRegex.test(`${singleMatch[2]}${singleMatch[3]}`)) return url;
|
|
271
336
|
const fullMatch = singleMatch[0];
|
|
272
|
-
const www = singleMatch[
|
|
337
|
+
const www = singleMatch[4];
|
|
273
338
|
const rest = url.slice(fullMatch.length);
|
|
274
339
|
return `${httpsLetterRegex.test(fullMatch) ? "https://" : "http://"}${www ? "www." : ""}${rest}`;
|
|
275
340
|
}
|
|
@@ -479,44 +544,41 @@ const lowercaseQueryPair = (pair) => {
|
|
|
479
544
|
});
|
|
480
545
|
};
|
|
481
546
|
const normalizeUrl = (url, options) => {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
508
|
-
if (options.lowercaseQuery) pairs = pairs.map(lowercaseQueryPair);
|
|
509
|
-
parsed.search = pairs.join("&");
|
|
547
|
+
const parsed = parseUrl(options.normalizeUnicode ? url.normalize("NFC") : url);
|
|
548
|
+
if (!parsed) return url;
|
|
549
|
+
if (options.stripHostTrailingDot && parsed.hostname.endsWith(".")) parsed.hostname = parsed.hostname.slice(0, -1);
|
|
550
|
+
if (options.stripAuthentication) {
|
|
551
|
+
parsed.username = "";
|
|
552
|
+
parsed.password = "";
|
|
553
|
+
}
|
|
554
|
+
if (options.stripWww) parsed.hostname = stripWww(parsed.hostname);
|
|
555
|
+
if (options.stripHash) parsed.hash = "";
|
|
556
|
+
let pathname = parsed.pathname;
|
|
557
|
+
if (options.normalizeEncoding) pathname = decodeAndNormalizeEncoding(pathname);
|
|
558
|
+
if (options.collapseSlashes) pathname = pathname.replace(repeatedSlashesRegex, "/");
|
|
559
|
+
if (options.stripTrailingSlash && pathname.length > 1 && pathname.endsWith("/")) pathname = pathname.slice(0, -1);
|
|
560
|
+
parsed.pathname = pathname;
|
|
561
|
+
if (options.normalizeEncoding && parsed.search.length > 1) {
|
|
562
|
+
parsed.search = canonicalizeQuery(parsed.search);
|
|
563
|
+
parsed.search = parsed.search.replace(percentEscapeRegex, (hex) => hex.toUpperCase());
|
|
564
|
+
}
|
|
565
|
+
if (options.normalizeEncoding && parsed.hash.includes("%")) parsed.hash = decodeAndNormalizeEncoding(parsed.hash);
|
|
566
|
+
if (options.stripQuery) parsed.search = "";
|
|
567
|
+
if (parsed.search && (options.stripQueryParams || options.lowercaseQuery)) {
|
|
568
|
+
let pairs = parsed.search.slice(1).split("&");
|
|
569
|
+
if (options.stripQueryParams) {
|
|
570
|
+
const strippedSet = getStrippedParamsSet(options.stripQueryParams);
|
|
571
|
+
pairs = pairs.filter((pair) => pair && !strippedSet.has(decodeQueryKey(pair).toLowerCase()));
|
|
510
572
|
}
|
|
511
|
-
if (options.
|
|
512
|
-
|
|
513
|
-
let result = parsed.href;
|
|
514
|
-
if (options.stripRootSlash && result === `${parsed.origin}/`) result = parsed.origin;
|
|
515
|
-
if (options.stripScheme ?? options.stripProtocol) result = result.replace(schemePrefixRegex, "");
|
|
516
|
-
return result;
|
|
517
|
-
} catch {
|
|
518
|
-
return url;
|
|
573
|
+
if (options.lowercaseQuery) pairs = pairs.map(lowercaseQueryPair);
|
|
574
|
+
parsed.search = pairs.join("&");
|
|
519
575
|
}
|
|
576
|
+
if (options.sortQueryParams && parsed.search.includes("&")) parsed.search = parsed.search.slice(1).split("&").filter(Boolean).sort(compareQueryPairs).join("&");
|
|
577
|
+
if (options.stripEmptyQuery && !parsed.search) parsed.search = "";
|
|
578
|
+
let result = parsed.href;
|
|
579
|
+
if (options.stripRootSlash && result === `${parsed.origin}/`) result = parsed.origin;
|
|
580
|
+
if (options.stripScheme ?? options.stripProtocol) result = result.replace(schemePrefixRegex, "");
|
|
581
|
+
return result;
|
|
520
582
|
};
|
|
521
583
|
const stripWww = (hostname) => {
|
|
522
584
|
return hostname.replace(wwwPrefixRegex, "");
|
|
@@ -662,4 +724,4 @@ const resolveFeedProtocol = resolveFeedScheme;
|
|
|
662
724
|
const addMissingProtocol = addMissingScheme;
|
|
663
725
|
/** @deprecated Use `upgradeScheme` instead. */
|
|
664
726
|
const upgradeProtocol = upgradeScheme;
|
|
665
|
-
export { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodePercent, decodeSegment, encodePathSegment, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, isHostOf, isHostOrSubdomainOf, isHttpUrl, isIpAddress, isSubdomainOf, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripWww, trimUrlValues, upgradeProtocol, upgradeScheme };
|
|
727
|
+
export { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodePercent, decodeSegment, encodePathSegment, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, isHostOf, isHostOrSubdomainOf, isHttpUrl, isIpAddress, isSubdomainOf, isUtf8Binary, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripWww, trimUrlValues, upgradeProtocol, upgradeScheme };
|
package/package.json
CHANGED