@fluixi/utils 1.0.0-alpha.53
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/LICENSE +21 -0
- package/README.md +73 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/dist/lib/array/advanced.cjs +1 -0
- package/dist/lib/array/advanced.d.ts +1116 -0
- package/dist/lib/array/advanced.d.ts.map +1 -0
- package/dist/lib/array/advanced.js +2429 -0
- package/dist/lib/array/advanced.mjs +1 -0
- package/dist/lib/array/array.cjs +1 -0
- package/dist/lib/array/array.d.ts +6 -0
- package/dist/lib/array/array.d.ts.map +1 -0
- package/dist/lib/array/array.js +31 -0
- package/dist/lib/array/array.mjs +1 -0
- package/dist/lib/array/index.cjs +1 -0
- package/dist/lib/array/index.d.ts +4 -0
- package/dist/lib/array/index.d.ts.map +1 -0
- package/dist/lib/array/index.js +3 -0
- package/dist/lib/array/index.mjs +1 -0
- package/dist/lib/array/utils.cjs +1 -0
- package/dist/lib/array/utils.d.ts +100 -0
- package/dist/lib/array/utils.d.ts.map +1 -0
- package/dist/lib/array/utils.js +165 -0
- package/dist/lib/array/utils.mjs +1 -0
- package/dist/lib/color/color.cjs +1 -0
- package/dist/lib/color/color.d.ts +366 -0
- package/dist/lib/color/color.d.ts.map +1 -0
- package/dist/lib/color/color.js +722 -0
- package/dist/lib/color/color.mjs +1 -0
- package/dist/lib/color/constants.cjs +1 -0
- package/dist/lib/color/constants.d.ts +361 -0
- package/dist/lib/color/constants.d.ts.map +1 -0
- package/dist/lib/color/constants.js +358 -0
- package/dist/lib/color/constants.mjs +1 -0
- package/dist/lib/color/conversions.cjs +1 -0
- package/dist/lib/color/conversions.d.ts +166 -0
- package/dist/lib/color/conversions.d.ts.map +1 -0
- package/dist/lib/color/conversions.js +766 -0
- package/dist/lib/color/conversions.mjs +1 -0
- package/dist/lib/color/helpers.cjs +1 -0
- package/dist/lib/color/helpers.d.ts +75 -0
- package/dist/lib/color/helpers.d.ts.map +1 -0
- package/dist/lib/color/helpers.js +419 -0
- package/dist/lib/color/helpers.mjs +1 -0
- package/dist/lib/color/index.cjs +1 -0
- package/dist/lib/color/index.d.ts +12 -0
- package/dist/lib/color/index.d.ts.map +1 -0
- package/dist/lib/color/index.js +15 -0
- package/dist/lib/color/index.mjs +1 -0
- package/dist/lib/color/types.cjs +1 -0
- package/dist/lib/color/types.d.ts +179 -0
- package/dist/lib/color/types.d.ts.map +1 -0
- package/dist/lib/color/types.js +1 -0
- package/dist/lib/color/types.mjs +0 -0
- package/dist/lib/color/utils.cjs +1 -0
- package/dist/lib/color/utils.d.ts +199 -0
- package/dist/lib/color/utils.d.ts.map +1 -0
- package/dist/lib/color/utils.js +858 -0
- package/dist/lib/color/utils.mjs +1 -0
- package/dist/lib/compare/comparators.cjs +1 -0
- package/dist/lib/compare/comparators.d.ts +65 -0
- package/dist/lib/compare/comparators.d.ts.map +1 -0
- package/dist/lib/compare/comparators.js +210 -0
- package/dist/lib/compare/comparators.mjs +1 -0
- package/dist/lib/compare/index.cjs +1 -0
- package/dist/lib/compare/index.d.ts +2 -0
- package/dist/lib/compare/index.d.ts.map +1 -0
- package/dist/lib/compare/index.js +1 -0
- package/dist/lib/compare/index.mjs +1 -0
- package/dist/lib/crypto/hash.cjs +1 -0
- package/dist/lib/crypto/hash.d.ts +6 -0
- package/dist/lib/crypto/hash.d.ts.map +1 -0
- package/dist/lib/crypto/hash.js +53 -0
- package/dist/lib/crypto/hash.mjs +1 -0
- package/dist/lib/crypto/index.cjs +1 -0
- package/dist/lib/crypto/index.d.ts +2 -0
- package/dist/lib/crypto/index.d.ts.map +1 -0
- package/dist/lib/crypto/index.js +1 -0
- package/dist/lib/crypto/index.mjs +1 -0
- package/dist/lib/dom/events.cjs +1 -0
- package/dist/lib/dom/events.d.ts +159 -0
- package/dist/lib/dom/events.d.ts.map +1 -0
- package/dist/lib/dom/events.js +50 -0
- package/dist/lib/dom/events.mjs +1 -0
- package/dist/lib/dom/index.cjs +1 -0
- package/dist/lib/dom/index.d.ts +3 -0
- package/dist/lib/dom/index.d.ts.map +1 -0
- package/dist/lib/dom/index.js +2 -0
- package/dist/lib/dom/index.mjs +1 -0
- package/dist/lib/dom/utils.cjs +1 -0
- package/dist/lib/dom/utils.d.ts +6 -0
- package/dist/lib/dom/utils.d.ts.map +1 -0
- package/dist/lib/dom/utils.js +37 -0
- package/dist/lib/dom/utils.mjs +1 -0
- package/dist/lib/functions/functions.cjs +1 -0
- package/dist/lib/functions/functions.d.ts +329 -0
- package/dist/lib/functions/functions.d.ts.map +1 -0
- package/dist/lib/functions/functions.js +585 -0
- package/dist/lib/functions/functions.mjs +1 -0
- package/dist/lib/functions/index.cjs +1 -0
- package/dist/lib/functions/index.d.ts +3 -0
- package/dist/lib/functions/index.d.ts.map +1 -0
- package/dist/lib/functions/index.js +1 -0
- package/dist/lib/functions/index.mjs +1 -0
- package/dist/lib/functions/types.cjs +1 -0
- package/dist/lib/functions/types.d.ts +2 -0
- package/dist/lib/functions/types.d.ts.map +1 -0
- package/dist/lib/functions/types.js +1 -0
- package/dist/lib/functions/types.mjs +0 -0
- package/dist/lib/helpers.cjs +1 -0
- package/dist/lib/helpers.d.ts +13 -0
- package/dist/lib/helpers.d.ts.map +1 -0
- package/dist/lib/helpers.js +66 -0
- package/dist/lib/helpers.mjs +1 -0
- package/dist/lib/index.cjs +1 -0
- package/dist/lib/index.d.ts +6 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/index.js +8 -0
- package/dist/lib/index.mjs +1 -0
- package/dist/lib/maths/constants.cjs +1 -0
- package/dist/lib/maths/constants.d.ts +84 -0
- package/dist/lib/maths/constants.d.ts.map +1 -0
- package/dist/lib/maths/constants.js +79 -0
- package/dist/lib/maths/constants.mjs +1 -0
- package/dist/lib/maths/fn/advanced.cjs +1 -0
- package/dist/lib/maths/fn/advanced.d.ts +149 -0
- package/dist/lib/maths/fn/advanced.d.ts.map +1 -0
- package/dist/lib/maths/fn/advanced.js +264 -0
- package/dist/lib/maths/fn/advanced.mjs +1 -0
- package/dist/lib/maths/fn/basic.cjs +1 -0
- package/dist/lib/maths/fn/basic.d.ts +182 -0
- package/dist/lib/maths/fn/basic.d.ts.map +1 -0
- package/dist/lib/maths/fn/basic.js +343 -0
- package/dist/lib/maths/fn/basic.mjs +1 -0
- package/dist/lib/maths/fn/complex.cjs +1 -0
- package/dist/lib/maths/fn/complex.d.ts +153 -0
- package/dist/lib/maths/fn/complex.d.ts.map +1 -0
- package/dist/lib/maths/fn/complex.js +383 -0
- package/dist/lib/maths/fn/complex.mjs +1 -0
- package/dist/lib/maths/fn/index.cjs +1 -0
- package/dist/lib/maths/fn/index.d.ts +10 -0
- package/dist/lib/maths/fn/index.d.ts.map +1 -0
- package/dist/lib/maths/fn/index.js +9 -0
- package/dist/lib/maths/fn/index.mjs +1 -0
- package/dist/lib/maths/fn/trigo.cjs +1 -0
- package/dist/lib/maths/fn/trigo.d.ts +147 -0
- package/dist/lib/maths/fn/trigo.d.ts.map +1 -0
- package/dist/lib/maths/fn/trigo.js +227 -0
- package/dist/lib/maths/fn/trigo.mjs +1 -0
- package/dist/lib/maths/fn/vector.cjs +1 -0
- package/dist/lib/maths/fn/vector.d.ts +512 -0
- package/dist/lib/maths/fn/vector.d.ts.map +1 -0
- package/dist/lib/maths/fn/vector.js +1136 -0
- package/dist/lib/maths/fn/vector.mjs +1 -0
- package/dist/lib/maths/formatter.cjs +1 -0
- package/dist/lib/maths/formatter.d.ts +35 -0
- package/dist/lib/maths/formatter.d.ts.map +1 -0
- package/dist/lib/maths/formatter.js +56 -0
- package/dist/lib/maths/formatter.mjs +1 -0
- package/dist/lib/maths/guards.cjs +1 -0
- package/dist/lib/maths/guards.d.ts +166 -0
- package/dist/lib/maths/guards.d.ts.map +1 -0
- package/dist/lib/maths/guards.js +315 -0
- package/dist/lib/maths/guards.mjs +1 -0
- package/dist/lib/maths/index.cjs +1 -0
- package/dist/lib/maths/index.d.ts +24 -0
- package/dist/lib/maths/index.d.ts.map +1 -0
- package/dist/lib/maths/index.js +29 -0
- package/dist/lib/maths/index.mjs +1 -0
- package/dist/lib/maths/parser.cjs +1 -0
- package/dist/lib/maths/parser.d.ts +20 -0
- package/dist/lib/maths/parser.d.ts.map +1 -0
- package/dist/lib/maths/parser.js +78 -0
- package/dist/lib/maths/parser.mjs +1 -0
- package/dist/lib/maths/random.cjs +1 -0
- package/dist/lib/maths/random.d.ts +27 -0
- package/dist/lib/maths/random.d.ts.map +1 -0
- package/dist/lib/maths/random.js +49 -0
- package/dist/lib/maths/random.mjs +1 -0
- package/dist/lib/maths/types/advanced.cjs +1 -0
- package/dist/lib/maths/types/advanced.d.ts +357 -0
- package/dist/lib/maths/types/advanced.d.ts.map +1 -0
- package/dist/lib/maths/types/advanced.js +5 -0
- package/dist/lib/maths/types/advanced.mjs +0 -0
- package/dist/lib/maths/types/basic.cjs +1 -0
- package/dist/lib/maths/types/basic.d.ts +95 -0
- package/dist/lib/maths/types/basic.d.ts.map +1 -0
- package/dist/lib/maths/types/basic.js +5 -0
- package/dist/lib/maths/types/basic.mjs +0 -0
- package/dist/lib/maths/types/index.cjs +1 -0
- package/dist/lib/maths/types/index.d.ts +4 -0
- package/dist/lib/maths/types/index.d.ts.map +1 -0
- package/dist/lib/maths/types/index.js +3 -0
- package/dist/lib/maths/types/index.mjs +0 -0
- package/dist/lib/maths/types/negative.cjs +1 -0
- package/dist/lib/maths/types/negative.d.ts +51 -0
- package/dist/lib/maths/types/negative.d.ts.map +1 -0
- package/dist/lib/maths/types/negative.js +5 -0
- package/dist/lib/maths/types/negative.mjs +0 -0
- package/dist/lib/maths/types/operations.cjs +1 -0
- package/dist/lib/maths/types/operations.d.ts +188 -0
- package/dist/lib/maths/types/operations.d.ts.map +1 -0
- package/dist/lib/maths/types/operations.js +6 -0
- package/dist/lib/maths/types/operations.mjs +0 -0
- package/dist/lib/object/clone.cjs +1 -0
- package/dist/lib/object/clone.d.ts +46 -0
- package/dist/lib/object/clone.d.ts.map +1 -0
- package/dist/lib/object/clone.js +104 -0
- package/dist/lib/object/clone.mjs +1 -0
- package/dist/lib/object/diff.cjs +1 -0
- package/dist/lib/object/diff.d.ts +203 -0
- package/dist/lib/object/diff.d.ts.map +1 -0
- package/dist/lib/object/diff.js +805 -0
- package/dist/lib/object/diff.mjs +1 -0
- package/dist/lib/object/getter-setter.cjs +1 -0
- package/dist/lib/object/getter-setter.d.ts +361 -0
- package/dist/lib/object/getter-setter.d.ts.map +1 -0
- package/dist/lib/object/getter-setter.js +1771 -0
- package/dist/lib/object/getter-setter.mjs +1 -0
- package/dist/lib/object/index.cjs +6 -0
- package/dist/lib/object/index.d.ts +13 -0
- package/dist/lib/object/index.d.ts.map +1 -0
- package/dist/lib/object/index.js +12 -0
- package/dist/lib/object/index.mjs +6 -0
- package/dist/lib/object/merge.cjs +1 -0
- package/dist/lib/object/merge.d.ts +93 -0
- package/dist/lib/object/merge.d.ts.map +1 -0
- package/dist/lib/object/merge.js +474 -0
- package/dist/lib/object/merge.mjs +1 -0
- package/dist/lib/object/object.cjs +1 -0
- package/dist/lib/object/object.d.ts +128 -0
- package/dist/lib/object/object.d.ts.map +1 -0
- package/dist/lib/object/object.js +432 -0
- package/dist/lib/object/object.mjs +1 -0
- package/dist/lib/object/omit.cjs +1 -0
- package/dist/lib/object/omit.d.ts +351 -0
- package/dist/lib/object/omit.d.ts.map +1 -0
- package/dist/lib/object/omit.js +656 -0
- package/dist/lib/object/omit.mjs +1 -0
- package/dist/lib/object/reconcile.cjs +1 -0
- package/dist/lib/object/reconcile.d.ts +7 -0
- package/dist/lib/object/reconcile.d.ts.map +1 -0
- package/dist/lib/object/reconcile.js +122 -0
- package/dist/lib/object/reconcile.mjs +1 -0
- package/dist/lib/object/selector.cjs +1 -0
- package/dist/lib/object/selector.d.ts +50 -0
- package/dist/lib/object/selector.d.ts.map +1 -0
- package/dist/lib/object/selector.js +371 -0
- package/dist/lib/object/selector.mjs +1 -0
- package/dist/lib/object/sort.cjs +1 -0
- package/dist/lib/object/sort.d.ts +149 -0
- package/dist/lib/object/sort.d.ts.map +1 -0
- package/dist/lib/object/sort.js +298 -0
- package/dist/lib/object/sort.mjs +1 -0
- package/dist/lib/object/transform.cjs +1 -0
- package/dist/lib/object/transform.d.ts +620 -0
- package/dist/lib/object/transform.d.ts.map +1 -0
- package/dist/lib/object/transform.js +1058 -0
- package/dist/lib/object/transform.mjs +1 -0
- package/dist/lib/object/types.cjs +1 -0
- package/dist/lib/object/types.d.ts +250 -0
- package/dist/lib/object/types.d.ts.map +1 -0
- package/dist/lib/object/types.js +1 -0
- package/dist/lib/object/types.mjs +0 -0
- package/dist/lib/object/utils.cjs +6 -0
- package/dist/lib/object/utils.d.ts +165 -0
- package/dist/lib/object/utils.d.ts.map +1 -0
- package/dist/lib/object/utils.js +444 -0
- package/dist/lib/object/utils.mjs +6 -0
- package/dist/lib/primitive/boolean/boolean.cjs +1 -0
- package/dist/lib/primitive/boolean/boolean.d.ts +276 -0
- package/dist/lib/primitive/boolean/boolean.d.ts.map +1 -0
- package/dist/lib/primitive/boolean/boolean.js +615 -0
- package/dist/lib/primitive/boolean/boolean.mjs +1 -0
- package/dist/lib/primitive/boolean/index.cjs +1 -0
- package/dist/lib/primitive/boolean/index.d.ts +3 -0
- package/dist/lib/primitive/boolean/index.d.ts.map +1 -0
- package/dist/lib/primitive/boolean/index.js +2 -0
- package/dist/lib/primitive/boolean/index.mjs +1 -0
- package/dist/lib/primitive/boolean/types.cjs +1 -0
- package/dist/lib/primitive/boolean/types.d.ts +14 -0
- package/dist/lib/primitive/boolean/types.d.ts.map +1 -0
- package/dist/lib/primitive/boolean/types.js +1 -0
- package/dist/lib/primitive/boolean/types.mjs +0 -0
- package/dist/lib/primitive/date/date.cjs +1 -0
- package/dist/lib/primitive/date/date.d.ts +641 -0
- package/dist/lib/primitive/date/date.d.ts.map +1 -0
- package/dist/lib/primitive/date/date.js +1998 -0
- package/dist/lib/primitive/date/date.mjs +1 -0
- package/dist/lib/primitive/date/index.cjs +1 -0
- package/dist/lib/primitive/date/index.d.ts +3 -0
- package/dist/lib/primitive/date/index.d.ts.map +1 -0
- package/dist/lib/primitive/date/index.js +2 -0
- package/dist/lib/primitive/date/index.mjs +1 -0
- package/dist/lib/primitive/date/types.cjs +1 -0
- package/dist/lib/primitive/date/types.d.ts +205 -0
- package/dist/lib/primitive/date/types.d.ts.map +1 -0
- package/dist/lib/primitive/date/types.js +7 -0
- package/dist/lib/primitive/date/types.mjs +0 -0
- package/dist/lib/primitive/index.cjs +5 -0
- package/dist/lib/primitive/index.d.ts +5 -0
- package/dist/lib/primitive/index.d.ts.map +1 -0
- package/dist/lib/primitive/index.js +4 -0
- package/dist/lib/primitive/index.mjs +5 -0
- package/dist/lib/primitive/string/index.cjs +5 -0
- package/dist/lib/primitive/string/index.d.ts +3 -0
- package/dist/lib/primitive/string/index.d.ts.map +1 -0
- package/dist/lib/primitive/string/index.js +2 -0
- package/dist/lib/primitive/string/index.mjs +5 -0
- package/dist/lib/primitive/string/parser-2.cjs +8 -0
- package/dist/lib/primitive/string/parser-2.d.ts +17 -0
- package/dist/lib/primitive/string/parser-2.d.ts.map +1 -0
- package/dist/lib/primitive/string/parser-2.js +656 -0
- package/dist/lib/primitive/string/parser-2.mjs +8 -0
- package/dist/lib/primitive/string/parser.cjs +3 -0
- package/dist/lib/primitive/string/parser.d.ts +9 -0
- package/dist/lib/primitive/string/parser.d.ts.map +1 -0
- package/dist/lib/primitive/string/parser.js +775 -0
- package/dist/lib/primitive/string/parser.mjs +3 -0
- package/dist/lib/primitive/string/string.cjs +5 -0
- package/dist/lib/primitive/string/string.d.ts +535 -0
- package/dist/lib/primitive/string/string.d.ts.map +1 -0
- package/dist/lib/primitive/string/string.js +1301 -0
- package/dist/lib/primitive/string/string.mjs +5 -0
- package/dist/lib/primitive/string/types.cjs +1 -0
- package/dist/lib/primitive/string/types.d.ts +318 -0
- package/dist/lib/primitive/string/types.d.ts.map +1 -0
- package/dist/lib/primitive/string/types.js +5 -0
- package/dist/lib/primitive/string/types.mjs +0 -0
- package/dist/lib/primitive/types.cjs +1 -0
- package/dist/lib/primitive/types.d.ts +27 -0
- package/dist/lib/primitive/types.d.ts.map +1 -0
- package/dist/lib/primitive/types.js +1 -0
- package/dist/lib/primitive/types.mjs +0 -0
- package/dist/lib/url/index.cjs +1 -0
- package/dist/lib/url/index.d.ts +3 -0
- package/dist/lib/url/index.d.ts.map +1 -0
- package/dist/lib/url/index.js +2 -0
- package/dist/lib/url/index.mjs +1 -0
- package/dist/lib/url/parser.cjs +1 -0
- package/dist/lib/url/parser.d.ts +216 -0
- package/dist/lib/url/parser.d.ts.map +1 -0
- package/dist/lib/url/parser.js +488 -0
- package/dist/lib/url/parser.mjs +1 -0
- package/dist/lib/url/types.cjs +1 -0
- package/dist/lib/url/types.d.ts +18 -0
- package/dist/lib/url/types.d.ts.map +1 -0
- package/dist/lib/url/types.js +1 -0
- package/dist/lib/url/types.mjs +0 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/package.json +150 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import type { URL_PARSER } from './types.js';
|
|
2
|
+
export declare const url_parser: (url: string) => URL_PARSER | null;
|
|
3
|
+
/**
|
|
4
|
+
* Parses a URL and returns its components
|
|
5
|
+
* @param url - URL string to parse
|
|
6
|
+
* @returns Parsed URL components or null if invalid
|
|
7
|
+
* @example
|
|
8
|
+
* parseUrl('https://example.com:8080/path?q=search#hash');
|
|
9
|
+
* // { protocol: 'https:', hostname: 'example.com', port: '8080', ... }
|
|
10
|
+
*/
|
|
11
|
+
export declare function parseUrl(url: string): URL_PARSER | null;
|
|
12
|
+
/**
|
|
13
|
+
* Checks if a string is a valid URL
|
|
14
|
+
* @param url - String to validate
|
|
15
|
+
* @returns True if valid URL
|
|
16
|
+
* @example
|
|
17
|
+
* isValidUrl('https://example.com'); // true
|
|
18
|
+
* isValidUrl('not a url'); // false
|
|
19
|
+
*/
|
|
20
|
+
export declare function isValidUrl(url: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Checks if a URL is absolute
|
|
23
|
+
* @param url - URL string to check
|
|
24
|
+
* @returns True if absolute URL
|
|
25
|
+
* @example
|
|
26
|
+
* isAbsoluteUrl('https://example.com/path'); // true
|
|
27
|
+
* isAbsoluteUrl('/path'); // false
|
|
28
|
+
*/
|
|
29
|
+
export declare function isAbsoluteUrl(url: string): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Checks if a URL is relative
|
|
32
|
+
* @param url - URL string to check
|
|
33
|
+
* @returns True if relative URL
|
|
34
|
+
* @example
|
|
35
|
+
* isRelativeUrl('/path'); // true
|
|
36
|
+
* isRelativeUrl('https://example.com'); // false
|
|
37
|
+
*/
|
|
38
|
+
export declare function isRelativeUrl(url: string): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Parses query string into an object
|
|
41
|
+
* @param queryString - Query string (with or without leading ?)
|
|
42
|
+
* @returns Object with parsed parameters
|
|
43
|
+
* @example
|
|
44
|
+
* parseQueryString('?name=John&age=30'); // { name: 'John', age: '30' }
|
|
45
|
+
*/
|
|
46
|
+
export declare function parseQueryString(queryString: string): Record<string, string>;
|
|
47
|
+
/**
|
|
48
|
+
* Parses query string into an object with array support
|
|
49
|
+
* @param queryString - Query string
|
|
50
|
+
* @returns Object with parsed parameters (arrays for repeated keys)
|
|
51
|
+
* @example
|
|
52
|
+
* parseQueryStringAdvanced('?tags=js&tags=ts'); // { tags: ['js', 'ts'] }
|
|
53
|
+
*/
|
|
54
|
+
export declare function parseQueryStringAdvanced(queryString: string): Record<string, string | string[]>;
|
|
55
|
+
/**
|
|
56
|
+
* Converts an object to a query string
|
|
57
|
+
* @param params - Object with parameters
|
|
58
|
+
* @param options - Serialization options
|
|
59
|
+
* @returns Query string
|
|
60
|
+
* @example
|
|
61
|
+
* toQueryString({ name: 'John', age: 30 }); // 'name=John&age=30'
|
|
62
|
+
*/
|
|
63
|
+
export declare function toQueryString(params: Record<string, any>, options?: {
|
|
64
|
+
encode?: boolean;
|
|
65
|
+
arrayFormat?: 'brackets' | 'repeat';
|
|
66
|
+
}): string;
|
|
67
|
+
/**
|
|
68
|
+
* Adds or updates query parameters in a URL
|
|
69
|
+
* @param url - Base URL
|
|
70
|
+
* @param params - Parameters to add/update
|
|
71
|
+
* @returns URL with updated parameters
|
|
72
|
+
* @example
|
|
73
|
+
* addQueryParams('https://example.com', { q: 'search' });
|
|
74
|
+
* // 'https://example.com?q=search'
|
|
75
|
+
*/
|
|
76
|
+
export declare function addQueryParams(url: string, params: Record<string, any>): string;
|
|
77
|
+
/**
|
|
78
|
+
* Removes query parameters from a URL
|
|
79
|
+
* @param url - URL to modify
|
|
80
|
+
* @param keys - Parameter keys to remove
|
|
81
|
+
* @returns URL without specified parameters
|
|
82
|
+
* @example
|
|
83
|
+
* removeQueryParams('https://example.com?a=1&b=2', 'a');
|
|
84
|
+
* // 'https://example.com?b=2'
|
|
85
|
+
*/
|
|
86
|
+
export declare function removeQueryParams(url: string, ...keys: string[]): string;
|
|
87
|
+
/**
|
|
88
|
+
* Gets a specific query parameter from a URL
|
|
89
|
+
* @param url - URL to parse
|
|
90
|
+
* @param key - Parameter key
|
|
91
|
+
* @returns Parameter value or null
|
|
92
|
+
* @example
|
|
93
|
+
* getQueryParam('https://example.com?name=John', 'name'); // 'John'
|
|
94
|
+
*/
|
|
95
|
+
export declare function getQueryParam(url: string, key: string): string | null;
|
|
96
|
+
/**
|
|
97
|
+
* Gets all values for a query parameter (for repeated parameters)
|
|
98
|
+
* @param url - URL to parse
|
|
99
|
+
* @param key - Parameter key
|
|
100
|
+
* @returns Array of parameter values
|
|
101
|
+
* @example
|
|
102
|
+
* getQueryParams('https://example.com?tags=js&tags=ts', 'tags');
|
|
103
|
+
* // ['js', 'ts']
|
|
104
|
+
*/
|
|
105
|
+
export declare function getQueryParams(url: string, key: string): string[];
|
|
106
|
+
/**
|
|
107
|
+
* Builds a URL from components
|
|
108
|
+
* @param components - URL components
|
|
109
|
+
* @returns Built URL
|
|
110
|
+
* @example
|
|
111
|
+
* buildUrl({
|
|
112
|
+
* protocol: 'https',
|
|
113
|
+
* hostname: 'example.com',
|
|
114
|
+
* pathname: '/path',
|
|
115
|
+
* query: { q: 'search' }
|
|
116
|
+
* }); // 'https://example.com/path?q=search'
|
|
117
|
+
*/
|
|
118
|
+
export declare function buildUrl(components: {
|
|
119
|
+
protocol?: string;
|
|
120
|
+
hostname: string;
|
|
121
|
+
port?: string | number;
|
|
122
|
+
pathname?: string;
|
|
123
|
+
query?: Record<string, any>;
|
|
124
|
+
hash?: string;
|
|
125
|
+
}): string;
|
|
126
|
+
/**
|
|
127
|
+
* Joins URL paths, handling slashes correctly
|
|
128
|
+
* @param base - Base URL or path
|
|
129
|
+
* @param paths - Path segments to join
|
|
130
|
+
* @returns Joined URL
|
|
131
|
+
* @example
|
|
132
|
+
* joinUrl('https://example.com', 'api', 'users', '123');
|
|
133
|
+
* // 'https://example.com/api/users/123'
|
|
134
|
+
*/
|
|
135
|
+
export declare function joinUrl(base: string, ...paths: string[]): string;
|
|
136
|
+
/**
|
|
137
|
+
* Normalizes a URL (removes default ports, trailing slashes, etc.)
|
|
138
|
+
* @param url - URL to normalize
|
|
139
|
+
* @param options - Normalization options
|
|
140
|
+
* @returns Normalized URL
|
|
141
|
+
* @example
|
|
142
|
+
* normalizeUrl('https://example.com:443/path/'); // 'https://example.com/path'
|
|
143
|
+
*/
|
|
144
|
+
export declare function normalizeUrl(url: string, options?: {
|
|
145
|
+
removeTrailingSlash?: boolean;
|
|
146
|
+
removeDefaultPort?: boolean;
|
|
147
|
+
sortQuery?: boolean;
|
|
148
|
+
}): string;
|
|
149
|
+
/**
|
|
150
|
+
* Extracts the domain from a URL
|
|
151
|
+
* @param url - URL to parse
|
|
152
|
+
* @param options - Extraction options
|
|
153
|
+
* @returns Domain name
|
|
154
|
+
* @example
|
|
155
|
+
* getDomain('https://subdomain.example.com/path'); // 'example.com'
|
|
156
|
+
*/
|
|
157
|
+
export declare function getDomain(url: string, options?: {
|
|
158
|
+
includeSubdomain?: boolean;
|
|
159
|
+
}): string | null;
|
|
160
|
+
/**
|
|
161
|
+
* Gets the file extension from a URL
|
|
162
|
+
* @param url - URL to parse
|
|
163
|
+
* @returns File extension (without dot) or null
|
|
164
|
+
* @example
|
|
165
|
+
* getUrlExtension('https://example.com/image.jpg'); // 'jpg'
|
|
166
|
+
*/
|
|
167
|
+
export declare function getUrlExtension(url: string): string | null;
|
|
168
|
+
/**
|
|
169
|
+
* Checks if URLs are from the same origin
|
|
170
|
+
* @param url1 - First URL
|
|
171
|
+
* @param url2 - Second URL
|
|
172
|
+
* @returns True if same origin
|
|
173
|
+
* @example
|
|
174
|
+
* isSameOrigin('https://example.com/a', 'https://example.com/b'); // true
|
|
175
|
+
*/
|
|
176
|
+
export declare function isSameOrigin(url1: string, url2: string): boolean;
|
|
177
|
+
/**
|
|
178
|
+
* Converts a relative URL to absolute using a base URL
|
|
179
|
+
* @param relativeUrl - Relative URL
|
|
180
|
+
* @param baseUrl - Base URL
|
|
181
|
+
* @returns Absolute URL
|
|
182
|
+
* @example
|
|
183
|
+
* toAbsoluteUrl('/path', 'https://example.com'); // 'https://example.com/path'
|
|
184
|
+
*/
|
|
185
|
+
export declare function toAbsoluteUrl(relativeUrl: string, baseUrl: string): string;
|
|
186
|
+
/**
|
|
187
|
+
* Sanitizes a URL by removing dangerous protocols and characters
|
|
188
|
+
* @param url - URL to sanitize
|
|
189
|
+
* @returns Sanitized URL or null if dangerous
|
|
190
|
+
* @example
|
|
191
|
+
* sanitizeUrl('javascript:alert(1)'); // null
|
|
192
|
+
* sanitizeUrl('https://example.com'); // 'https://example.com'
|
|
193
|
+
*/
|
|
194
|
+
export declare function sanitizeUrl(url: string): string | null;
|
|
195
|
+
/**
|
|
196
|
+
* Extracts all URLs from a text string
|
|
197
|
+
* @param text - Text to search
|
|
198
|
+
* @param options - Extraction options
|
|
199
|
+
* @returns Array of found URLs
|
|
200
|
+
* @example
|
|
201
|
+
* extractUrls('Check out https://example.com and http://test.com');
|
|
202
|
+
* // ['https://example.com', 'http://test.com']
|
|
203
|
+
*/
|
|
204
|
+
export declare function extractUrls(text: string, options?: {
|
|
205
|
+
unique?: boolean;
|
|
206
|
+
}): string[];
|
|
207
|
+
/**
|
|
208
|
+
* Checks if a URL matches a pattern (with wildcard support)
|
|
209
|
+
* @param url - URL to test
|
|
210
|
+
* @param pattern - Pattern with wildcards (* and **)
|
|
211
|
+
* @returns True if matches
|
|
212
|
+
* @example
|
|
213
|
+
* matchesUrlPattern('https://example.com/api/users/123', 'https://example.com/api/*'); // true
|
|
214
|
+
*/
|
|
215
|
+
export declare function matchesUrlPattern(url: string, pattern: string): boolean;
|
|
216
|
+
//# sourceMappingURL=parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../../src/lib/url/parser.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
export const url_parser = (url) => {
|
|
2
|
+
try {
|
|
3
|
+
const p = new URL(url);
|
|
4
|
+
return {
|
|
5
|
+
hash: p.hash,
|
|
6
|
+
host: p.host,
|
|
7
|
+
hostname: p.hostname,
|
|
8
|
+
href: p.href,
|
|
9
|
+
origin: p.origin,
|
|
10
|
+
pathname: p.pathname,
|
|
11
|
+
port: p.port,
|
|
12
|
+
search: p.search,
|
|
13
|
+
searchParams: p.searchParams.toString(),
|
|
14
|
+
protocol: p.protocol,
|
|
15
|
+
};
|
|
16
|
+
//JSON.parse(p.toJSON());
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Parses a URL and returns its components
|
|
24
|
+
* @param url - URL string to parse
|
|
25
|
+
* @returns Parsed URL components or null if invalid
|
|
26
|
+
* @example
|
|
27
|
+
* parseUrl('https://example.com:8080/path?q=search#hash');
|
|
28
|
+
* // { protocol: 'https:', hostname: 'example.com', port: '8080', ... }
|
|
29
|
+
*/
|
|
30
|
+
export function parseUrl(url) {
|
|
31
|
+
return url_parser(url);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Checks if a string is a valid URL
|
|
35
|
+
* @param url - String to validate
|
|
36
|
+
* @returns True if valid URL
|
|
37
|
+
* @example
|
|
38
|
+
* isValidUrl('https://example.com'); // true
|
|
39
|
+
* isValidUrl('not a url'); // false
|
|
40
|
+
*/
|
|
41
|
+
export function isValidUrl(url) {
|
|
42
|
+
try {
|
|
43
|
+
new URL(url);
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Checks if a URL is absolute
|
|
52
|
+
* @param url - URL string to check
|
|
53
|
+
* @returns True if absolute URL
|
|
54
|
+
* @example
|
|
55
|
+
* isAbsoluteUrl('https://example.com/path'); // true
|
|
56
|
+
* isAbsoluteUrl('/path'); // false
|
|
57
|
+
*/
|
|
58
|
+
export function isAbsoluteUrl(url) {
|
|
59
|
+
try {
|
|
60
|
+
new URL(url);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Checks if a URL is relative
|
|
69
|
+
* @param url - URL string to check
|
|
70
|
+
* @returns True if relative URL
|
|
71
|
+
* @example
|
|
72
|
+
* isRelativeUrl('/path'); // true
|
|
73
|
+
* isRelativeUrl('https://example.com'); // false
|
|
74
|
+
*/
|
|
75
|
+
export function isRelativeUrl(url) {
|
|
76
|
+
return !isAbsoluteUrl(url);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Parses query string into an object
|
|
80
|
+
* @param queryString - Query string (with or without leading ?)
|
|
81
|
+
* @returns Object with parsed parameters
|
|
82
|
+
* @example
|
|
83
|
+
* parseQueryString('?name=John&age=30'); // { name: 'John', age: '30' }
|
|
84
|
+
*/
|
|
85
|
+
export function parseQueryString(queryString) {
|
|
86
|
+
const params = {};
|
|
87
|
+
const searchParams = new URLSearchParams(queryString);
|
|
88
|
+
searchParams.forEach((value, key) => {
|
|
89
|
+
params[key] = value;
|
|
90
|
+
});
|
|
91
|
+
return params;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Parses query string into an object with array support
|
|
95
|
+
* @param queryString - Query string
|
|
96
|
+
* @returns Object with parsed parameters (arrays for repeated keys)
|
|
97
|
+
* @example
|
|
98
|
+
* parseQueryStringAdvanced('?tags=js&tags=ts'); // { tags: ['js', 'ts'] }
|
|
99
|
+
*/
|
|
100
|
+
export function parseQueryStringAdvanced(queryString) {
|
|
101
|
+
const params = {};
|
|
102
|
+
const searchParams = new URLSearchParams(queryString);
|
|
103
|
+
searchParams.forEach((value, key) => {
|
|
104
|
+
if (params[key]) {
|
|
105
|
+
if (Array.isArray(params[key])) {
|
|
106
|
+
params[key].push(value);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
params[key] = [params[key], value];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
params[key] = value;
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
return params;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Converts an object to a query string
|
|
120
|
+
* @param params - Object with parameters
|
|
121
|
+
* @param options - Serialization options
|
|
122
|
+
* @returns Query string
|
|
123
|
+
* @example
|
|
124
|
+
* toQueryString({ name: 'John', age: 30 }); // 'name=John&age=30'
|
|
125
|
+
*/
|
|
126
|
+
export function toQueryString(params, options = {}) {
|
|
127
|
+
const { encode = true, arrayFormat = 'repeat' } = options;
|
|
128
|
+
const searchParams = new URLSearchParams();
|
|
129
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
130
|
+
if (value === null || value === undefined)
|
|
131
|
+
return;
|
|
132
|
+
if (Array.isArray(value)) {
|
|
133
|
+
value.forEach((v) => {
|
|
134
|
+
if (arrayFormat === 'brackets') {
|
|
135
|
+
searchParams.append(`${key}[]`, String(v));
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
searchParams.append(key, String(v));
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
searchParams.append(key, String(value));
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
let result = searchParams.toString();
|
|
147
|
+
if (!encode) {
|
|
148
|
+
result = decodeURIComponent(result);
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Adds or updates query parameters in a URL
|
|
154
|
+
* @param url - Base URL
|
|
155
|
+
* @param params - Parameters to add/update
|
|
156
|
+
* @returns URL with updated parameters
|
|
157
|
+
* @example
|
|
158
|
+
* addQueryParams('https://example.com', { q: 'search' });
|
|
159
|
+
* // 'https://example.com?q=search'
|
|
160
|
+
*/
|
|
161
|
+
export function addQueryParams(url, params) {
|
|
162
|
+
try {
|
|
163
|
+
const urlObj = new URL(url);
|
|
164
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
165
|
+
if (value === null || value === undefined) {
|
|
166
|
+
urlObj.searchParams.delete(key);
|
|
167
|
+
}
|
|
168
|
+
else if (Array.isArray(value)) {
|
|
169
|
+
urlObj.searchParams.delete(key);
|
|
170
|
+
value.forEach((v) => urlObj.searchParams.append(key, String(v)));
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
urlObj.searchParams.set(key, String(value));
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
return urlObj.toString();
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return url;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Removes query parameters from a URL
|
|
184
|
+
* @param url - URL to modify
|
|
185
|
+
* @param keys - Parameter keys to remove
|
|
186
|
+
* @returns URL without specified parameters
|
|
187
|
+
* @example
|
|
188
|
+
* removeQueryParams('https://example.com?a=1&b=2', 'a');
|
|
189
|
+
* // 'https://example.com?b=2'
|
|
190
|
+
*/
|
|
191
|
+
export function removeQueryParams(url, ...keys) {
|
|
192
|
+
try {
|
|
193
|
+
const urlObj = new URL(url);
|
|
194
|
+
keys.forEach((key) => urlObj.searchParams.delete(key));
|
|
195
|
+
return urlObj.toString();
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
return url;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Gets a specific query parameter from a URL
|
|
203
|
+
* @param url - URL to parse
|
|
204
|
+
* @param key - Parameter key
|
|
205
|
+
* @returns Parameter value or null
|
|
206
|
+
* @example
|
|
207
|
+
* getQueryParam('https://example.com?name=John', 'name'); // 'John'
|
|
208
|
+
*/
|
|
209
|
+
export function getQueryParam(url, key) {
|
|
210
|
+
try {
|
|
211
|
+
const urlObj = new URL(url);
|
|
212
|
+
return urlObj.searchParams.get(key);
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Gets all values for a query parameter (for repeated parameters)
|
|
220
|
+
* @param url - URL to parse
|
|
221
|
+
* @param key - Parameter key
|
|
222
|
+
* @returns Array of parameter values
|
|
223
|
+
* @example
|
|
224
|
+
* getQueryParams('https://example.com?tags=js&tags=ts', 'tags');
|
|
225
|
+
* // ['js', 'ts']
|
|
226
|
+
*/
|
|
227
|
+
export function getQueryParams(url, key) {
|
|
228
|
+
try {
|
|
229
|
+
const urlObj = new URL(url);
|
|
230
|
+
return urlObj.searchParams.getAll(key);
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
return [];
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Builds a URL from components
|
|
238
|
+
* @param components - URL components
|
|
239
|
+
* @returns Built URL
|
|
240
|
+
* @example
|
|
241
|
+
* buildUrl({
|
|
242
|
+
* protocol: 'https',
|
|
243
|
+
* hostname: 'example.com',
|
|
244
|
+
* pathname: '/path',
|
|
245
|
+
* query: { q: 'search' }
|
|
246
|
+
* }); // 'https://example.com/path?q=search'
|
|
247
|
+
*/
|
|
248
|
+
export function buildUrl(components) {
|
|
249
|
+
const { protocol = 'https', hostname, port, pathname = '', query, hash, } = components;
|
|
250
|
+
let url = `${protocol}://${hostname}`;
|
|
251
|
+
if (port) {
|
|
252
|
+
url += `:${port}`;
|
|
253
|
+
}
|
|
254
|
+
if (pathname) {
|
|
255
|
+
url += pathname.startsWith('/') ? pathname : `/${pathname}`;
|
|
256
|
+
}
|
|
257
|
+
if (query && Object.keys(query).length > 0) {
|
|
258
|
+
url += `?${toQueryString(query)}`;
|
|
259
|
+
}
|
|
260
|
+
if (hash) {
|
|
261
|
+
url += hash.startsWith('#') ? hash : `#${hash}`;
|
|
262
|
+
}
|
|
263
|
+
return url;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Joins URL paths, handling slashes correctly
|
|
267
|
+
* @param base - Base URL or path
|
|
268
|
+
* @param paths - Path segments to join
|
|
269
|
+
* @returns Joined URL
|
|
270
|
+
* @example
|
|
271
|
+
* joinUrl('https://example.com', 'api', 'users', '123');
|
|
272
|
+
* // 'https://example.com/api/users/123'
|
|
273
|
+
*/
|
|
274
|
+
export function joinUrl(base, ...paths) {
|
|
275
|
+
try {
|
|
276
|
+
const url = new URL(base);
|
|
277
|
+
// Get base pathname and filter out root slash if it's the only path
|
|
278
|
+
const basePath = url.pathname === '/' ? '' : url.pathname;
|
|
279
|
+
const pathParts = [basePath, ...paths]
|
|
280
|
+
.filter(Boolean)
|
|
281
|
+
.map((part) => part.replace(/^\/+|\/+$/g, ''));
|
|
282
|
+
url.pathname = '/' + pathParts.filter(Boolean).join('/');
|
|
283
|
+
return url.toString();
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
// If not a valid URL, treat as path
|
|
287
|
+
const parts = [base, ...paths]
|
|
288
|
+
.filter(Boolean)
|
|
289
|
+
.map((part) => part.replace(/^\/+|\/+$/g, ''));
|
|
290
|
+
return '/' + parts.join('/');
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Normalizes a URL (removes default ports, trailing slashes, etc.)
|
|
295
|
+
* @param url - URL to normalize
|
|
296
|
+
* @param options - Normalization options
|
|
297
|
+
* @returns Normalized URL
|
|
298
|
+
* @example
|
|
299
|
+
* normalizeUrl('https://example.com:443/path/'); // 'https://example.com/path'
|
|
300
|
+
*/
|
|
301
|
+
export function normalizeUrl(url, options = {}) {
|
|
302
|
+
const { removeTrailingSlash = true, removeDefaultPort = true, sortQuery = false, } = options;
|
|
303
|
+
try {
|
|
304
|
+
const urlObj = new URL(url);
|
|
305
|
+
// Remove default ports
|
|
306
|
+
if (removeDefaultPort) {
|
|
307
|
+
if ((urlObj.protocol === 'http:' && urlObj.port === '80') ||
|
|
308
|
+
(urlObj.protocol === 'https:' && urlObj.port === '443')) {
|
|
309
|
+
urlObj.port = '';
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
// Remove trailing slash
|
|
313
|
+
if (removeTrailingSlash &&
|
|
314
|
+
urlObj.pathname.endsWith('/') &&
|
|
315
|
+
urlObj.pathname !== '/') {
|
|
316
|
+
urlObj.pathname = urlObj.pathname.slice(0, -1);
|
|
317
|
+
}
|
|
318
|
+
// Sort query parameters
|
|
319
|
+
if (sortQuery) {
|
|
320
|
+
const params = Array.from(urlObj.searchParams.entries());
|
|
321
|
+
params.sort((a, b) => a[0].localeCompare(b[0]));
|
|
322
|
+
urlObj.search = new URLSearchParams(params).toString();
|
|
323
|
+
}
|
|
324
|
+
return urlObj.toString();
|
|
325
|
+
}
|
|
326
|
+
catch {
|
|
327
|
+
return url;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Extracts the domain from a URL
|
|
332
|
+
* @param url - URL to parse
|
|
333
|
+
* @param options - Extraction options
|
|
334
|
+
* @returns Domain name
|
|
335
|
+
* @example
|
|
336
|
+
* getDomain('https://subdomain.example.com/path'); // 'example.com'
|
|
337
|
+
*/
|
|
338
|
+
export function getDomain(url, options = {}) {
|
|
339
|
+
const { includeSubdomain = false } = options;
|
|
340
|
+
try {
|
|
341
|
+
const urlObj = new URL(url);
|
|
342
|
+
const hostname = urlObj.hostname;
|
|
343
|
+
if (includeSubdomain) {
|
|
344
|
+
return hostname;
|
|
345
|
+
}
|
|
346
|
+
// Extract root domain (simplified - doesn't handle all TLDs perfectly)
|
|
347
|
+
const parts = hostname.split('.');
|
|
348
|
+
if (parts.length >= 2) {
|
|
349
|
+
return parts.slice(-2).join('.');
|
|
350
|
+
}
|
|
351
|
+
return hostname;
|
|
352
|
+
}
|
|
353
|
+
catch {
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Gets the file extension from a URL
|
|
359
|
+
* @param url - URL to parse
|
|
360
|
+
* @returns File extension (without dot) or null
|
|
361
|
+
* @example
|
|
362
|
+
* getUrlExtension('https://example.com/image.jpg'); // 'jpg'
|
|
363
|
+
*/
|
|
364
|
+
export function getUrlExtension(url) {
|
|
365
|
+
try {
|
|
366
|
+
const urlObj = new URL(url);
|
|
367
|
+
const pathname = urlObj.pathname;
|
|
368
|
+
const lastDot = pathname.lastIndexOf('.');
|
|
369
|
+
const lastSlash = pathname.lastIndexOf('/');
|
|
370
|
+
if (lastDot > lastSlash && lastDot < pathname.length - 1) {
|
|
371
|
+
return pathname.slice(lastDot + 1);
|
|
372
|
+
}
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
catch {
|
|
376
|
+
return null;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Checks if URLs are from the same origin
|
|
381
|
+
* @param url1 - First URL
|
|
382
|
+
* @param url2 - Second URL
|
|
383
|
+
* @returns True if same origin
|
|
384
|
+
* @example
|
|
385
|
+
* isSameOrigin('https://example.com/a', 'https://example.com/b'); // true
|
|
386
|
+
*/
|
|
387
|
+
export function isSameOrigin(url1, url2) {
|
|
388
|
+
try {
|
|
389
|
+
const urlObj1 = new URL(url1);
|
|
390
|
+
const urlObj2 = new URL(url2);
|
|
391
|
+
return urlObj1.origin === urlObj2.origin;
|
|
392
|
+
}
|
|
393
|
+
catch {
|
|
394
|
+
return false;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Converts a relative URL to absolute using a base URL
|
|
399
|
+
* @param relativeUrl - Relative URL
|
|
400
|
+
* @param baseUrl - Base URL
|
|
401
|
+
* @returns Absolute URL
|
|
402
|
+
* @example
|
|
403
|
+
* toAbsoluteUrl('/path', 'https://example.com'); // 'https://example.com/path'
|
|
404
|
+
*/
|
|
405
|
+
export function toAbsoluteUrl(relativeUrl, baseUrl) {
|
|
406
|
+
try {
|
|
407
|
+
return new URL(relativeUrl, baseUrl).toString();
|
|
408
|
+
}
|
|
409
|
+
catch {
|
|
410
|
+
return relativeUrl;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Sanitizes a URL by removing dangerous protocols and characters
|
|
415
|
+
* @param url - URL to sanitize
|
|
416
|
+
* @returns Sanitized URL or null if dangerous
|
|
417
|
+
* @example
|
|
418
|
+
* sanitizeUrl('javascript:alert(1)'); // null
|
|
419
|
+
* sanitizeUrl('https://example.com'); // 'https://example.com'
|
|
420
|
+
*/
|
|
421
|
+
export function sanitizeUrl(url) {
|
|
422
|
+
const dangerousProtocols = ['javascript:', 'data:', 'vbscript:'];
|
|
423
|
+
try {
|
|
424
|
+
const urlObj = new URL(url);
|
|
425
|
+
if (dangerousProtocols.some((proto) => urlObj.protocol === proto)) {
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
// Return original URL to preserve format
|
|
429
|
+
return url;
|
|
430
|
+
}
|
|
431
|
+
catch {
|
|
432
|
+
// Check for protocol in string
|
|
433
|
+
const lower = url.toLowerCase().trim();
|
|
434
|
+
if (dangerousProtocols.some((proto) => lower.startsWith(proto))) {
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
return url;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Extracts all URLs from a text string
|
|
442
|
+
* @param text - Text to search
|
|
443
|
+
* @param options - Extraction options
|
|
444
|
+
* @returns Array of found URLs
|
|
445
|
+
* @example
|
|
446
|
+
* extractUrls('Check out https://example.com and http://test.com');
|
|
447
|
+
* // ['https://example.com', 'http://test.com']
|
|
448
|
+
*/
|
|
449
|
+
export function extractUrls(text, options = {}) {
|
|
450
|
+
const { unique = true } = options;
|
|
451
|
+
const urlRegex = /https?:\/\/[^\s<>"{}|\\^`\]]+/gi;
|
|
452
|
+
const matches = text.match(urlRegex) || [];
|
|
453
|
+
return unique ? Array.from(new Set(matches)) : matches;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Checks if a URL matches a pattern (with wildcard support)
|
|
457
|
+
* @param url - URL to test
|
|
458
|
+
* @param pattern - Pattern with wildcards (* and **)
|
|
459
|
+
* @returns True if matches
|
|
460
|
+
* @example
|
|
461
|
+
* matchesUrlPattern('https://example.com/api/users/123', 'https://example.com/api/*'); // true
|
|
462
|
+
*/
|
|
463
|
+
export function matchesUrlPattern(url, pattern) {
|
|
464
|
+
try {
|
|
465
|
+
const urlObj = new URL(url);
|
|
466
|
+
const patternObj = new URL(pattern);
|
|
467
|
+
// Check protocol
|
|
468
|
+
if (patternObj.protocol !== '*:' &&
|
|
469
|
+
urlObj.protocol !== patternObj.protocol) {
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
// Check hostname
|
|
473
|
+
if (patternObj.hostname !== '*' &&
|
|
474
|
+
urlObj.hostname !== patternObj.hostname) {
|
|
475
|
+
return false;
|
|
476
|
+
}
|
|
477
|
+
// Check pathname with wildcards
|
|
478
|
+
const pathPattern = patternObj.pathname
|
|
479
|
+
.replace(/\*\*/g, '.*')
|
|
480
|
+
.replace(/\*/g, '[^/]*')
|
|
481
|
+
.replace(/[.+?^${}()|[\]\\]/g, '\\$&');
|
|
482
|
+
const pathRegex = new RegExp(`^${pathPattern}$`);
|
|
483
|
+
return pathRegex.test(urlObj.pathname);
|
|
484
|
+
}
|
|
485
|
+
catch {
|
|
486
|
+
return false;
|
|
487
|
+
}
|
|
488
|
+
}
|