trousse 3.1.0 → 3.2.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 +2 -2
- package/dist/types.d.ts +5 -0
- package/dist/urls.d.ts +7 -7
- package/dist/urls.js +38 -20
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { omitEmpty } from "./arrays.js";
|
|
2
|
-
import { AnyOf, DeepOmit, IsPlainObject, MaybePromise, NormalizeOptions, Nullish, ParamSource, PartialNullish, Pattern, RegistrableDomainOptions, WeightedMimeType } from "./types.js";
|
|
2
|
+
import { AnyOf, DeepOmit, IsPlainObject, MaybePromise, NormalizeOptions, Nullish, ParamOptions, ParamSource, PartialNullish, Pattern, RegistrableDomainOptions, WeightedMimeType } from "./types.js";
|
|
3
3
|
import { coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString } from "./coercions.js";
|
|
4
4
|
import { archiveExtensions, audioExtensions, codeExtensions, documentExtensions, flashExtensions, fontExtensions, imageExtensions, installerExtensions, subtitleExtensions, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions } from "./files.js";
|
|
5
5
|
import { isBoolean, isFunction, isNonEmptyString, isNullish, isNumber, isObject, isPlainObject, isPresent, isString, isValidDate } from "./is.js";
|
|
@@ -10,4 +10,4 @@ import { createAcceptHeader, feedMimeTypes, genericFeedMimeTypes, genericHtmlMim
|
|
|
10
10
|
import { omit, pick, toMap, trimObject } from "./objects.js";
|
|
11
11
|
import { sleep } from "./timers.js";
|
|
12
12
|
import { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodePercent, decodeSegment, encodePathSegment, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, 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 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, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, htmlMimeTypes, imageExtensions, includesAnyOf, installerExtensions, isAnyOf, isBoolean, isFunction, isHostOf, isHostOrSubdomainOf, isHttpUrl, isIpAddress, isJsonLike, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isString, isSubdomainOf, isValidDate, normalizeUrl, omit, omitEmpty, parseUrl, pick, resolveFeedProtocol, resolveFeedScheme, resolveUrl, sleep, startsWithAnyOf, stripWww, subtitleExtensions, t, toMap, trimObject, trimUrlValues, tx, upgradeProtocol, upgradeScheme, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions };
|
|
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, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, htmlMimeTypes, imageExtensions, includesAnyOf, installerExtensions, isAnyOf, isBoolean, isFunction, isHostOf, isHostOrSubdomainOf, isHttpUrl, isIpAddress, isJsonLike, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isString, isSubdomainOf, isValidDate, normalizeUrl, omit, omitEmpty, parseUrl, pick, resolveFeedProtocol, resolveFeedScheme, resolveUrl, sleep, startsWithAnyOf, stripWww, subtitleExtensions, t, toMap, trimObject, trimUrlValues, tx, upgradeProtocol, upgradeScheme, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions };
|
package/dist/types.d.ts
CHANGED
|
@@ -6,6 +6,11 @@ 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
8
|
export type ParamSource = URL | URLSearchParams | string;
|
|
9
|
+
export type ParamOptions = {
|
|
10
|
+
decodeValues?: boolean;
|
|
11
|
+
trimValues?: boolean;
|
|
12
|
+
preserveEmptyValues?: boolean;
|
|
13
|
+
};
|
|
9
14
|
export type RegistrableDomainOptions = {
|
|
10
15
|
suffixes?: ReadonlyArray<string>;
|
|
11
16
|
};
|
package/dist/urls.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NormalizeOptions, Nullish, ParamSource, RegistrableDomainOptions } from "./types.js";
|
|
1
|
+
import { NormalizeOptions, Nullish, ParamOptions, ParamSource, 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 isHostOf: (url: string | URL, hosts: string | ReadonlyArray<string>) => boolean;
|
|
@@ -10,13 +10,13 @@ export declare const decodeSegment: (segment: string | undefined) => string | un
|
|
|
10
10
|
export declare const decodePercent: (value: string) => string;
|
|
11
11
|
export declare const encodePathSegment: (value: string) => string;
|
|
12
12
|
export declare const hasQueryParam: (source: ParamSource | undefined, name: string) => boolean;
|
|
13
|
-
export declare const getQueryParamValues: (source: ParamSource | undefined, name: string) => Array<string>;
|
|
14
|
-
export declare const getQueryParam: (source: ParamSource | undefined, name: string) => string | undefined;
|
|
15
|
-
export declare const getQueryParams: (source: ParamSource | undefined, names: ReadonlyArray<string
|
|
13
|
+
export declare const getQueryParamValues: (source: ParamSource | undefined, name: string, options?: ParamOptions) => Array<string>;
|
|
14
|
+
export declare const getQueryParam: (source: ParamSource | undefined, name: string, options?: ParamOptions) => string | undefined;
|
|
15
|
+
export declare const getQueryParams: (source: ParamSource | undefined, names: ReadonlyArray<string>, options?: ParamOptions) => Record<string, string>;
|
|
16
16
|
export declare const hasFragmentParam: (source: ParamSource | undefined, name: string) => boolean;
|
|
17
|
-
export declare const getFragmentParamValues: (source: ParamSource | undefined, name: string) => Array<string>;
|
|
18
|
-
export declare const getFragmentParam: (source: ParamSource | undefined, name: string) => string | undefined;
|
|
19
|
-
export declare const getFragmentParams: (source: ParamSource | undefined, names: ReadonlyArray<string
|
|
17
|
+
export declare const getFragmentParamValues: (source: ParamSource | undefined, name: string, options?: ParamOptions) => Array<string>;
|
|
18
|
+
export declare const getFragmentParam: (source: ParamSource | undefined, name: string, options?: ParamOptions) => string | undefined;
|
|
19
|
+
export declare const getFragmentParams: (source: ParamSource | undefined, names: ReadonlyArray<string>, options?: ParamOptions) => Record<string, string>;
|
|
20
20
|
export declare const composeQuery: (params?: Record<string, string | number>) => string;
|
|
21
21
|
export declare const composeFragment: (params?: Record<string, string | number>) => string;
|
|
22
22
|
export declare const fixMalformedScheme: (url: string) => string;
|
package/dist/urls.js
CHANGED
|
@@ -94,37 +94,55 @@ const pathSegmentEscapeRegex = /[/?#\\\s]/g;
|
|
|
94
94
|
const encodePathSegment = (value) => {
|
|
95
95
|
return value.replace(pathSegmentEscapeRegex, (character) => encodeURIComponent(character));
|
|
96
96
|
};
|
|
97
|
-
const
|
|
97
|
+
const readRawPairs = (text, prefix) => {
|
|
98
|
+
if (text === void 0) return;
|
|
99
|
+
const pairs = [];
|
|
100
|
+
const body = text.startsWith(prefix) ? text.slice(1) : text;
|
|
101
|
+
for (const pair of body.split("&")) {
|
|
102
|
+
if (!pair) continue;
|
|
103
|
+
const separator = pair.indexOf("=");
|
|
104
|
+
if (separator === -1) {
|
|
105
|
+
pairs.push([decodePercent(pair.replaceAll("+", " ")), ""]);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const name = decodePercent(pair.slice(0, separator).replaceAll("+", " "));
|
|
109
|
+
pairs.push([name, pair.slice(separator + 1)]);
|
|
110
|
+
}
|
|
111
|
+
return pairs;
|
|
112
|
+
};
|
|
113
|
+
const readQuerySource = (source, options) => {
|
|
114
|
+
if (options?.decodeValues === false) return readRawPairs(source instanceof URL ? source.search : source?.toString(), "?");
|
|
98
115
|
if (source instanceof URL) return source.searchParams;
|
|
99
116
|
if (typeof source === "string") return new URLSearchParams(source);
|
|
100
117
|
return source;
|
|
101
118
|
};
|
|
102
|
-
const readFragmentSource = (source) => {
|
|
119
|
+
const readFragmentSource = (source, options) => {
|
|
120
|
+
if (options?.decodeValues === false) return readRawPairs(source instanceof URL ? source.hash : source?.toString(), "#");
|
|
103
121
|
if (source instanceof URL) return new URLSearchParams(source.hash.slice(1));
|
|
104
122
|
if (typeof source === "string") return new URLSearchParams(source.startsWith("#") ? source.slice(1) : source);
|
|
105
123
|
return source;
|
|
106
124
|
};
|
|
107
|
-
const readParamValues = (params, name) => {
|
|
125
|
+
const readParamValues = (params, name, options) => {
|
|
108
126
|
const values = [];
|
|
109
127
|
if (!params) return values;
|
|
110
128
|
const lowercaseName = name.toLowerCase();
|
|
111
129
|
for (const [key, value] of params) {
|
|
112
130
|
if (key.toLowerCase() !== lowercaseName) continue;
|
|
113
|
-
const
|
|
114
|
-
if (
|
|
131
|
+
const read = options?.trimValues === false ? value : value.trim();
|
|
132
|
+
if (read || options?.preserveEmptyValues) values.push(read);
|
|
115
133
|
}
|
|
116
134
|
return values;
|
|
117
135
|
};
|
|
118
136
|
const hasParam = (params, name) => {
|
|
119
137
|
if (!params) return false;
|
|
120
138
|
const lowercaseName = name.toLowerCase();
|
|
121
|
-
for (const key of params
|
|
139
|
+
for (const [key] of params) if (key.toLowerCase() === lowercaseName) return true;
|
|
122
140
|
return false;
|
|
123
141
|
};
|
|
124
|
-
const readParams = (params, names) => {
|
|
142
|
+
const readParams = (params, names, options) => {
|
|
125
143
|
const picked = {};
|
|
126
144
|
for (const name of names) {
|
|
127
|
-
const value = readParamValues(params, name)[0];
|
|
145
|
+
const value = readParamValues(params, name, options)[0];
|
|
128
146
|
if (value !== void 0) picked[name] = value;
|
|
129
147
|
}
|
|
130
148
|
return picked;
|
|
@@ -132,26 +150,26 @@ const readParams = (params, names) => {
|
|
|
132
150
|
const hasQueryParam = (source, name) => {
|
|
133
151
|
return hasParam(readQuerySource(source), name);
|
|
134
152
|
};
|
|
135
|
-
const getQueryParamValues = (source, name) => {
|
|
136
|
-
return readParamValues(readQuerySource(source), name);
|
|
153
|
+
const getQueryParamValues = (source, name, options) => {
|
|
154
|
+
return readParamValues(readQuerySource(source, options), name, options);
|
|
137
155
|
};
|
|
138
|
-
const getQueryParam = (source, name) => {
|
|
139
|
-
return getQueryParamValues(source, name)[0];
|
|
156
|
+
const getQueryParam = (source, name, options) => {
|
|
157
|
+
return getQueryParamValues(source, name, options)[0];
|
|
140
158
|
};
|
|
141
|
-
const getQueryParams = (source, names) => {
|
|
142
|
-
return readParams(readQuerySource(source), names);
|
|
159
|
+
const getQueryParams = (source, names, options) => {
|
|
160
|
+
return readParams(readQuerySource(source, options), names, options);
|
|
143
161
|
};
|
|
144
162
|
const hasFragmentParam = (source, name) => {
|
|
145
163
|
return hasParam(readFragmentSource(source), name);
|
|
146
164
|
};
|
|
147
|
-
const getFragmentParamValues = (source, name) => {
|
|
148
|
-
return readParamValues(readFragmentSource(source), name);
|
|
165
|
+
const getFragmentParamValues = (source, name, options) => {
|
|
166
|
+
return readParamValues(readFragmentSource(source, options), name, options);
|
|
149
167
|
};
|
|
150
|
-
const getFragmentParam = (source, name) => {
|
|
151
|
-
return getFragmentParamValues(source, name)[0];
|
|
168
|
+
const getFragmentParam = (source, name, options) => {
|
|
169
|
+
return getFragmentParamValues(source, name, options)[0];
|
|
152
170
|
};
|
|
153
|
-
const getFragmentParams = (source, names) => {
|
|
154
|
-
return readParams(readFragmentSource(source), names);
|
|
171
|
+
const getFragmentParams = (source, names, options) => {
|
|
172
|
+
return readParams(readFragmentSource(source, options), names, options);
|
|
155
173
|
};
|
|
156
174
|
const queryPartEscapeRegex = /[&#%+\s]/g;
|
|
157
175
|
const composePairs = (params) => {
|
package/package.json
CHANGED