trousse 3.14.0 → 3.15.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/README.md +59 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/locales.d.ts +8 -3
- package/dist/locales.js +14 -7
- package/dist/types.d.ts +3 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,3 +13,62 @@ npm add trousse
|
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
In published packages, prefer adding it as a dev dependency and bundling it at build time so it adds no runtime dependency.
|
|
16
|
+
|
|
17
|
+
## API
|
|
18
|
+
|
|
19
|
+
### `normalizeUrl`
|
|
20
|
+
|
|
21
|
+
Builds a key for comparing URLs, so two spellings of one address come out the same. The result is not meant for fetching: `stripScheme` and `stripRootSlash` produce strings no URL parser reads back, and `sortQueryParams` or `lowercaseQuery` can produce a URL the server answers differently.
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { normalizeUrl } from 'trousse'
|
|
25
|
+
|
|
26
|
+
normalizeUrl('https://www.example.com/feed/?utm_source=x&b=2&a=1#top', {
|
|
27
|
+
stripWww: true,
|
|
28
|
+
stripTrailingSlash: true,
|
|
29
|
+
stripHash: true,
|
|
30
|
+
sortQueryParams: true,
|
|
31
|
+
stripQueryParams: ['utm_source'],
|
|
32
|
+
})
|
|
33
|
+
// 'https://example.com/feed?a=1&b=2'
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
There are no defaults. Every option is off unless set, so `normalizeUrl(url, {})` applies none of them. The URL still goes through the URL parser, which lowercases the host, drops a default port and resolves `..` segments. A string that does not parse comes back unchanged.
|
|
37
|
+
|
|
38
|
+
| Option | Type | What it does |
|
|
39
|
+
|--------|------|--------------|
|
|
40
|
+
| `stripScheme` | `boolean` | Removes `http://` or `https://`, so both schemes compare the same |
|
|
41
|
+
| `stripProtocol` | `boolean` | Deprecated alias of `stripScheme`, which wins when both are set |
|
|
42
|
+
| `stripHostTrailingDot` | `boolean` | `example.com.` → `example.com`, the DNS absolute form |
|
|
43
|
+
| `stripAuthentication` | `boolean` | Removes `user:pass@` |
|
|
44
|
+
| `stripWww` | `boolean` | Removes every leading `www.` label, except the last one before a bare top-level domain, so `www.com` stays |
|
|
45
|
+
| `stripTrailingSlash` | `boolean` | `/feed/` → `/feed`, leaving the root `/` alone |
|
|
46
|
+
| `stripRootSlash` | `boolean` | `https://example.com/` → `https://example.com` |
|
|
47
|
+
| `collapseSlashes` | `boolean` | `///` → `/` in the path, except right after a `:`, so a URL inside the path keeps its `//` |
|
|
48
|
+
| `stripHash` | `boolean` | Removes the `#fragment` |
|
|
49
|
+
| `sortQueryParams` | `boolean` | Sorts query pairs by decoded name and drops empty pairs |
|
|
50
|
+
| `stripQueryParams` | `ReadonlyArray<string> \| QueryParamPredicate` | Removes query pairs by name or by a function, see below |
|
|
51
|
+
| `stripQuery` | `boolean` | Removes the whole query |
|
|
52
|
+
| `stripEmptyQuery` | `boolean` | `/feed?` → `/feed` |
|
|
53
|
+
| `lowercaseQuery` | `boolean` | Lowercases query names and values, leaving percent escapes as they are |
|
|
54
|
+
| `normalizeEncoding` | `boolean` | Uppercases `%xx` hex and decodes needless escapes in the path, query and fragment. In the query, `q=a%3Ab` and `q=a:b` come out the same, while `%26` keeps a value whole |
|
|
55
|
+
| `normalizeUnicode` | `boolean` | Applies NFC to the raw input before parsing |
|
|
56
|
+
|
|
57
|
+
#### `stripQueryParams`
|
|
58
|
+
|
|
59
|
+
A list of names removes every pair with one of those names, matched case-insensitively against the decoded name. The list is cached by identity, so build it once and never mutate it.
|
|
60
|
+
|
|
61
|
+
A function decides pair by pair and returns `true` to remove one. It gets three arguments:
|
|
62
|
+
|
|
63
|
+
- `name`: the decoded name, in its original case.
|
|
64
|
+
- `value`: the decoded value, or `undefined` for a pair with no `=`.
|
|
65
|
+
- `url`: the URL as normalized so far, so with `stripWww` the host arrives without its `www.`.
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
normalizeUrl('https://example.com/feed?utm_source=x&page=1&ref=', {
|
|
69
|
+
stripQueryParams: (name, value) => name.startsWith('utm_') || value === '',
|
|
70
|
+
})
|
|
71
|
+
// 'https://example.com/feed?page=1'
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
In both forms, empty pairs go too, and the pairs that stay are written back as written, in their original encoding, or the one `normalizeEncoding` gave them. A query left with no pairs loses its `?`.
|
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, isLocaleSegment, isRegionSegment, t, tx } from "./locales.js";
|
|
6
|
+
import { escapeHtml, isLocale, isLocaleOrRegion, isLocaleSegment, isRegion, isRegionSegment, 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 { AnyOf, DeepOmit, IsPlainObject, LocaleSegmentOptions, MaybePromise, NormalizeOptions, Nullish, ParamOptions, ParamSource, PartialNullish, Pattern, QueryParamPredicate, RegistrableDomainOptions, WeightedMimeType } from "./types.js";
|
|
11
|
+
import { AnyOf, DeepOmit, IsPlainObject, LocaleOptions, LocaleSegmentOptions, MaybePromise, NormalizeOptions, Nullish, ParamOptions, ParamSource, PartialNullish, Pattern, QueryParamPredicate, RegistrableDomainOptions, WeightedMimeType } from "./types.js";
|
|
12
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, isHostOf, isHostOrSubdomainOf, isHostname, 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 LocaleSegmentOptions, type MaybePromise, type NormalizeOptions, type Nullish, type ParamOptions, type ParamSource, type PartialNullish, type Pattern, type QueryParamPredicate, 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, 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, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isInteger, isIpAddress, isJsonLike, isLocaleSegment, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isRegionSegment, 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 };
|
|
13
|
+
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 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, 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, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isInteger, isIpAddress, isJsonLike, isLocale, isLocaleOrRegion, isLocaleSegment, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isRegion, isRegionSegment, 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, isLocaleSegment, isRegionSegment, t, tx } from "./locales.js";
|
|
6
|
+
import { escapeHtml, isLocale, isLocaleOrRegion, isLocaleSegment, isRegion, isRegionSegment, 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 { 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, isHostOf, isHostOrSubdomainOf, isHostname, 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, 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, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isInteger, isIpAddress, isJsonLike, isLocaleSegment, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isRegionSegment, 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 };
|
|
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, 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, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isInteger, isIpAddress, isJsonLike, isLocale, isLocaleOrRegion, isLocaleSegment, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isRegion, isRegionSegment, 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/locales.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LocaleOptions } from "./types.js";
|
|
2
2
|
export declare const escapeHtml: (value: string) => string;
|
|
3
3
|
export declare const t: (template: string, params: Record<string, string | number>) => string;
|
|
4
4
|
export declare const tx: (template: string, params: Record<string, string | number>) => string;
|
|
5
|
-
export declare const
|
|
6
|
-
export declare const
|
|
5
|
+
export declare const isLocale: (value: string, options?: LocaleOptions) => boolean;
|
|
6
|
+
export declare const isRegion: (value: string) => boolean;
|
|
7
|
+
export declare const isLocaleOrRegion: (value: string, options?: LocaleOptions) => boolean;
|
|
8
|
+
/** @deprecated Use `isLocale` instead. */
|
|
9
|
+
export declare const isLocaleSegment: typeof isLocale;
|
|
10
|
+
/** @deprecated Use `isRegion` instead. */
|
|
11
|
+
export declare const isRegionSegment: typeof isRegion;
|
package/dist/locales.js
CHANGED
|
@@ -32,9 +32,9 @@ const specialPurposeCodes = [
|
|
|
32
32
|
"mul",
|
|
33
33
|
"zxx"
|
|
34
34
|
];
|
|
35
|
-
const
|
|
36
|
-
if (isAnyOf(
|
|
37
|
-
const tag = options?.underscore ?
|
|
35
|
+
const isLocale = (value, options) => {
|
|
36
|
+
if (isAnyOf(value, specialPurposeCodes)) return false;
|
|
37
|
+
const tag = options?.underscore ? value.replaceAll("_", "-") : value;
|
|
38
38
|
try {
|
|
39
39
|
return languageNames.of(tag) !== void 0;
|
|
40
40
|
} catch {
|
|
@@ -51,12 +51,19 @@ const specialPurposeRegions = [
|
|
|
51
51
|
"xa",
|
|
52
52
|
"xb"
|
|
53
53
|
];
|
|
54
|
-
const
|
|
55
|
-
if (isAnyOf(
|
|
54
|
+
const isRegion = (value) => {
|
|
55
|
+
if (isAnyOf(value, specialPurposeRegions)) return false;
|
|
56
56
|
try {
|
|
57
|
-
return regionNames.of(
|
|
57
|
+
return regionNames.of(value.toUpperCase()) !== void 0;
|
|
58
58
|
} catch {
|
|
59
59
|
return false;
|
|
60
60
|
}
|
|
61
61
|
};
|
|
62
|
-
|
|
62
|
+
const isLocaleOrRegion = (value, options) => {
|
|
63
|
+
return isLocale(value, options) || isRegion(value);
|
|
64
|
+
};
|
|
65
|
+
/** @deprecated Use `isLocale` instead. */
|
|
66
|
+
const isLocaleSegment = isLocale;
|
|
67
|
+
/** @deprecated Use `isRegion` instead. */
|
|
68
|
+
const isRegionSegment = isRegion;
|
|
69
|
+
export { escapeHtml, isLocale, isLocaleOrRegion, isLocaleSegment, isRegion, isRegionSegment, t, tx };
|
package/dist/types.d.ts
CHANGED
|
@@ -11,9 +11,11 @@ export type ParamOptions = {
|
|
|
11
11
|
trimValues?: boolean;
|
|
12
12
|
preserveEmptyValues?: boolean;
|
|
13
13
|
};
|
|
14
|
-
export type
|
|
14
|
+
export type LocaleOptions = {
|
|
15
15
|
underscore?: boolean;
|
|
16
16
|
};
|
|
17
|
+
/** @deprecated Use `LocaleOptions` instead. */
|
|
18
|
+
export type LocaleSegmentOptions = LocaleOptions;
|
|
17
19
|
export type RegistrableDomainOptions = {
|
|
18
20
|
suffixes?: ReadonlyArray<string>;
|
|
19
21
|
};
|
package/package.json
CHANGED