trousse 2.1.0 → 2.2.1

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 CHANGED
@@ -7,5 +7,5 @@ import { escapeHtml, t, tx } from "./locales.js";
7
7
  import { anyWordMatchesAnyOf, endsWithAnyOf, escapeRegex, includesAnyOf, isAnyOf, startsWithAnyOf } from "./matching.js";
8
8
  import { omit, pick, toMap, trimObject } from "./objects.js";
9
9
  import { sleep } from "./timers.js";
10
- import { getPathSegments, isHostOf, isSubdomainOf, parseUrl } from "./urls.js";
11
- export { type AnyOf, type DeepOmit, type IsPlainObject, type MaybePromise, type Nullish, type PartialNullish, type Pattern, anyWordMatchesAnyOf, coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString, endsWithAnyOf, escapeHtml, escapeRegex, getPathSegments, includesAnyOf, isAnyOf, isBoolean, isFunction, isHostOf, isJsonLike, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isString, isSubdomainOf, isValidDate, omit, omitEmpty, parseUrl, pick, sleep, startsWithAnyOf, t, toMap, trimObject, tx };
10
+ import { decodeSegment, getPathSegments, getSubdomain, isHostOf, isHostOrSubdomainOf, isHttpUrl, isSubdomainOf, parseUrl } from "./urls.js";
11
+ export { type AnyOf, type DeepOmit, type IsPlainObject, type MaybePromise, type Nullish, type PartialNullish, type Pattern, anyWordMatchesAnyOf, coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString, decodeSegment, endsWithAnyOf, escapeHtml, escapeRegex, getPathSegments, getSubdomain, includesAnyOf, isAnyOf, isBoolean, isFunction, isHostOf, isHostOrSubdomainOf, isHttpUrl, isJsonLike, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isString, isSubdomainOf, isValidDate, omit, omitEmpty, parseUrl, pick, sleep, startsWithAnyOf, t, toMap, trimObject, tx };
package/dist/index.js CHANGED
@@ -6,5 +6,5 @@ import { escapeHtml, t, tx } from "./locales.js";
6
6
  import { anyWordMatchesAnyOf, endsWithAnyOf, escapeRegex, includesAnyOf, isAnyOf, startsWithAnyOf } from "./matching.js";
7
7
  import { omit, pick, toMap, trimObject } from "./objects.js";
8
8
  import { sleep } from "./timers.js";
9
- import { getPathSegments, isHostOf, isSubdomainOf, parseUrl } from "./urls.js";
10
- export { anyWordMatchesAnyOf, coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString, endsWithAnyOf, escapeHtml, escapeRegex, getPathSegments, includesAnyOf, isAnyOf, isBoolean, isFunction, isHostOf, isJsonLike, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isString, isSubdomainOf, isValidDate, omit, omitEmpty, parseUrl, pick, sleep, startsWithAnyOf, t, toMap, trimObject, tx };
9
+ import { decodeSegment, getPathSegments, getSubdomain, isHostOf, isHostOrSubdomainOf, isHttpUrl, isSubdomainOf, parseUrl } from "./urls.js";
10
+ export { anyWordMatchesAnyOf, coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString, decodeSegment, endsWithAnyOf, escapeHtml, escapeRegex, getPathSegments, getSubdomain, includesAnyOf, isAnyOf, isBoolean, isFunction, isHostOf, isHostOrSubdomainOf, isHttpUrl, isJsonLike, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isString, isSubdomainOf, isValidDate, omit, omitEmpty, parseUrl, pick, sleep, startsWithAnyOf, t, toMap, trimObject, tx };
package/dist/urls.d.ts CHANGED
@@ -3,4 +3,8 @@ export declare const parseUrl: (value: string | URL, base?: string | URL) => URL
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;
5
5
  export declare const isSubdomainOf: (url: string | URL, domains: string | ReadonlyArray<string>) => boolean;
6
+ export declare const isHttpUrl: (url: string | URL) => boolean;
7
+ export declare const isHostOrSubdomainOf: (url: string | URL, domains: string | ReadonlyArray<string>) => boolean;
8
+ export declare const getSubdomain: (url: string | URL, domains: string | ReadonlyArray<string>) => string | undefined;
9
+ export declare const decodeSegment: (segment: string | undefined) => string | undefined;
6
10
  //#endregion
package/dist/urls.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { endsWithAnyOf, isAnyOf } from "./matching.js";
2
2
  //#region src/urls.ts
3
+ const httpProtocols = ["http:", "https:"];
3
4
  const parseUrl = (value, base) => {
4
5
  if (value instanceof URL && base === void 0) return value;
5
6
  try {
@@ -19,5 +20,28 @@ const isSubdomainOf = (url, domains) => {
19
20
  if (!hostname) return false;
20
21
  return endsWithAnyOf(hostname, (typeof domains === "string" ? [domains] : domains).map((domain) => `.${domain}`));
21
22
  };
23
+ const isHttpUrl = (url) => {
24
+ const protocol = parseUrl(url)?.protocol;
25
+ if (!protocol) return false;
26
+ return httpProtocols.includes(protocol);
27
+ };
28
+ const isHostOrSubdomainOf = (url, domains) => {
29
+ return isHostOf(url, domains) || isSubdomainOf(url, domains);
30
+ };
31
+ const getSubdomain = (url, domains) => {
32
+ const hostname = parseUrl(url)?.hostname.toLowerCase();
33
+ if (!hostname) return;
34
+ const list = typeof domains === "string" ? [domains] : domains;
35
+ for (const domain of list) {
36
+ const suffix = `.${domain.toLowerCase()}`;
37
+ if (hostname.endsWith(suffix) && hostname.length > suffix.length) return hostname.slice(0, -suffix.length);
38
+ }
39
+ };
40
+ const decodeSegment = (segment) => {
41
+ if (segment === void 0) return;
42
+ try {
43
+ return decodeURIComponent(segment);
44
+ } catch {}
45
+ };
22
46
  //#endregion
23
- export { getPathSegments, isHostOf, isSubdomainOf, parseUrl };
47
+ export { decodeSegment, getPathSegments, getSubdomain, isHostOf, isHostOrSubdomainOf, isHttpUrl, isSubdomainOf, parseUrl };
package/package.json CHANGED
@@ -40,5 +40,5 @@
40
40
  "kvalita": "^1.21.0",
41
41
  "tsdown": "^0.23.0"
42
42
  },
43
- "version": "2.1.0"
43
+ "version": "2.2.1"
44
44
  }