@rzl-zone/utils-js 3.0.1-beta.0 → 3.1.0-beta.2
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/assertions/index.cjs +1 -1
- package/dist/assertions/index.d.ts +96 -116
- package/dist/assertions/index.js +1 -1
- package/dist/chunk-25G6B35W.js +1 -0
- package/dist/chunk-2AUUPGMN.js +1 -0
- package/dist/chunk-2JQQQ625.js +1 -0
- package/dist/chunk-445TAVT4.cjs +1 -0
- package/dist/chunk-467VEMJH.js +1 -0
- package/dist/chunk-6Q4I2RPC.cjs +1 -0
- package/dist/chunk-6SMJMZ7G.cjs +1 -0
- package/dist/chunk-BLCANGFS.js +1 -0
- package/dist/chunk-CK3BZGEU.js +1 -0
- package/dist/{chunk-JWHM3WZQ.cjs → chunk-D43GV6EF.cjs} +1 -1
- package/dist/chunk-DFSTVQFI.cjs +1 -0
- package/dist/chunk-DYRDBYPF.cjs +1 -0
- package/dist/chunk-EXXLUO7L.cjs +1 -0
- package/dist/chunk-G72VSVGO.js +1 -0
- package/dist/chunk-GAJTFCUA.js +1 -0
- package/dist/chunk-GOJNH55O.cjs +1 -0
- package/dist/chunk-HXST7RJB.js +1 -0
- package/dist/{chunk-BYZAD3XN.cjs → chunk-HXTACM3O.cjs} +1 -1
- package/dist/chunk-IJTZWWRJ.cjs +1 -0
- package/dist/chunk-ISJ33O2J.cjs +1 -0
- package/dist/chunk-JISH2VGG.js +1 -0
- package/dist/{chunk-FF76ISQ2.js → chunk-JQZUWBIX.js} +1 -1
- package/dist/{chunk-XH6MCRLP.js → chunk-K4PLGD7C.js} +1 -1
- package/dist/chunk-KZQB7H4E.cjs +1 -0
- package/dist/chunk-LUVV5KSD.cjs +2 -0
- package/dist/chunk-LVUSFXQS.cjs +1 -0
- package/dist/chunk-ME5OV5HN.js +1 -0
- package/dist/chunk-MF5Y5K4N.cjs +1 -0
- package/dist/chunk-MJAW5RAK.js +1 -0
- package/dist/chunk-MWLEM7ED.js +1 -0
- package/dist/chunk-NSVLG7NY.js +4 -0
- package/dist/chunk-NUHOOAUN.cjs +1 -0
- package/dist/chunk-NUVGETPD.js +2 -0
- package/dist/chunk-O5VXBNUQ.cjs +4 -0
- package/dist/chunk-O7SJR4CY.cjs +1 -0
- package/dist/chunk-OX6RLS2F.cjs +1 -0
- package/dist/chunk-PFLNHD4B.cjs +1 -0
- package/dist/chunk-PSWITZK5.js +1 -0
- package/dist/chunk-PYUVKHUF.cjs +1 -0
- package/dist/chunk-REFMEB4A.js +1 -0
- package/dist/chunk-RHE3M2NJ.cjs +1 -0
- package/dist/chunk-RHIBYOFQ.js +1 -0
- package/dist/chunk-S7GMG4TB.js +1 -0
- package/dist/chunk-TQMZ2LPI.js +1 -0
- package/dist/{chunk-RN3TP3S3.js → chunk-U4TZO7WV.js} +1 -1
- package/dist/chunk-UUMKL74S.js +1 -0
- package/dist/{chunk-EXZ47NOW.js → chunk-VQLAPEL3.js} +1 -1
- package/dist/chunk-VTR75UID.cjs +1 -0
- package/dist/chunk-XFCJDSB2.cjs +1 -0
- package/dist/chunk-XOYX4PLZ.cjs +1 -0
- package/dist/chunk-Z6FLCEN6.js +1 -0
- package/dist/chunk-ZO5364A6.cjs +1 -0
- package/dist/conversions/index.cjs +1 -1
- package/dist/conversions/index.d.ts +396 -593
- package/dist/conversions/index.js +1 -1
- package/dist/env/index.d.ts +5 -8
- package/dist/events/index.cjs +1 -1
- package/dist/events/index.d.ts +80 -77
- package/dist/events/index.js +1 -1
- package/dist/{array-CIZRbqTF.d.ts → extends-Bk_SBGdT.d.ts} +145 -1
- package/dist/formatting/index.cjs +1 -1
- package/dist/formatting/index.d.ts +478 -440
- package/dist/formatting/index.js +1 -1
- package/dist/generator/index.cjs +1 -1
- package/dist/generator/index.d.ts +135 -112
- package/dist/generator/index.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/{is-array-Ckm_47hw.d.ts → is-array--YjXV-Wx.d.ts} +2 -352
- package/dist/{isPlainObject-BKYaI6a8.d.ts → isPlainObject-BVhBAPHX.d.ts} +49 -62
- package/dist/next/index.cjs +6 -6
- package/dist/next/index.d.ts +125 -105
- package/dist/next/index.js +5 -5
- package/dist/next/server/index.cjs +1 -1
- package/dist/next/server/index.d.ts +8 -22
- package/dist/next/server/index.js +1 -1
- package/dist/operations/index.cjs +1 -1
- package/dist/operations/index.d.ts +72 -127
- package/dist/operations/index.js +1 -1
- package/dist/parsers/index.cjs +1 -1
- package/dist/parsers/index.d.ts +26 -27
- package/dist/parsers/index.js +1 -1
- package/dist/predicates/index.cjs +1 -1
- package/dist/predicates/index.d.ts +686 -660
- package/dist/predicates/index.js +1 -1
- package/dist/promise/index.cjs +1 -1
- package/dist/promise/index.d.ts +14 -23
- package/dist/promise/index.js +1 -1
- package/dist/rzl-utils.global.js +3 -3
- package/dist/string-XA-til3C.d.ts +351 -0
- package/dist/strings/index.cjs +1 -1
- package/dist/strings/index.d.ts +301 -376
- package/dist/strings/index.js +1 -1
- package/dist/stylings/index.cjs +1 -1
- package/dist/stylings/index.d.ts +15 -23
- package/dist/stylings/index.js +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/urls/index.cjs +1 -1
- package/dist/urls/index.d.ts +279 -302
- package/dist/urls/index.js +1 -1
- package/package.json +53 -11
- package/dist/chunk-2TRAPBZ7.cjs +0 -1
- package/dist/chunk-4DK3RCC4.js +0 -2
- package/dist/chunk-4JOQ45HL.js +0 -1
- package/dist/chunk-55CZALRS.js +0 -1
- package/dist/chunk-62HX5Z45.cjs +0 -1
- package/dist/chunk-7NA6DUHR.cjs +0 -2
- package/dist/chunk-BVPMMWDL.cjs +0 -1
- package/dist/chunk-CEFYHEM4.cjs +0 -1
- package/dist/chunk-CN53M4QZ.cjs +0 -1
- package/dist/chunk-D47MHBSD.cjs +0 -1
- package/dist/chunk-DGH75GJD.js +0 -1
- package/dist/chunk-E5NUI7PN.js +0 -1
- package/dist/chunk-FDITZ5C6.cjs +0 -1
- package/dist/chunk-FSSV24W7.cjs +0 -1
- package/dist/chunk-GCGU2WB7.js +0 -1
- package/dist/chunk-GECI2YBP.js +0 -1
- package/dist/chunk-GQE4OVHC.cjs +0 -1
- package/dist/chunk-GRVZXQXL.cjs +0 -1
- package/dist/chunk-I4AVNHPA.cjs +0 -1
- package/dist/chunk-KBKYG3IY.cjs +0 -1
- package/dist/chunk-KCQDDZJE.cjs +0 -1
- package/dist/chunk-LVKAYEZ4.js +0 -1
- package/dist/chunk-MBDWTK54.cjs +0 -1
- package/dist/chunk-MNGGDB2G.js +0 -1
- package/dist/chunk-MY7BA4GI.cjs +0 -1
- package/dist/chunk-N2IJPIND.cjs +0 -1
- package/dist/chunk-NIMNTEGV.js +0 -1
- package/dist/chunk-POCPQYZS.js +0 -1
- package/dist/chunk-PVJF2JHM.js +0 -1
- package/dist/chunk-QCFXEUKL.js +0 -1
- package/dist/chunk-QFCGBBSY.js +0 -1
- package/dist/chunk-QQYAUPSK.cjs +0 -1
- package/dist/chunk-TC4VBE4Y.cjs +0 -1
- package/dist/chunk-TUXDINHF.cjs +0 -1
- package/dist/chunk-U5Y2FXMN.cjs +0 -1
- package/dist/chunk-UUPQI6ND.cjs +0 -1
- package/dist/chunk-VYCGZ2S6.js +0 -1
- package/dist/chunk-W5EDKJK3.js +0 -1
- package/dist/chunk-WHAVUFEU.js +0 -1
- package/dist/chunk-X3GTRVVK.cjs +0 -4
- package/dist/chunk-XCFIOTCV.js +0 -1
- package/dist/chunk-XE27XPJR.js +0 -1
- package/dist/chunk-XVB3ZZEB.js +0 -1
- package/dist/chunk-YEERJDOF.js +0 -4
- package/dist/chunk-ZBSIGJFO.cjs +0 -1
- package/dist/chunk-ZYAML74V.js +0 -1
- package/dist/extends-Mp81Hq9-.d.ts +0 -145
package/dist/urls/index.d.ts
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
1
|
/** ----------------------------------------------------------
|
|
2
|
-
* * ***
|
|
2
|
+
* * ***Type-Utility: `QueryParamPairs`.***
|
|
3
3
|
* ----------------------------------------------------------
|
|
4
|
-
*
|
|
4
|
+
* **Represents a non-empty array of key–value pairs.**
|
|
5
|
+
* @description
|
|
5
6
|
* Type for `queryParams` parameter, the second parameter of **{@link constructURL}**.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
7
|
+
* - **Behavior:**
|
|
8
|
+
* - Each inner tuple strictly follows the `[string, string | number]` shape.
|
|
9
|
+
* - Ensures the outer array contains **at least one pair** (non-empty).
|
|
10
|
+
* - Commonly used for URL construction parameters,
|
|
11
|
+
* query string segments, or other key–value structured data.
|
|
12
12
|
* @example
|
|
13
13
|
* // ✅ valid usage
|
|
14
14
|
* const params: QueryParamPairs = [
|
|
15
15
|
* ["foo", 1],
|
|
16
16
|
* ["bar", "baz"]
|
|
17
17
|
* ];
|
|
18
|
-
*
|
|
19
18
|
* constructURL("https://example.com", params);
|
|
20
19
|
*
|
|
21
|
-
* @example
|
|
22
20
|
* // ❌ invalid: must contain at least one item
|
|
23
21
|
* const empty: QueryParamPairs = [];
|
|
24
22
|
*
|
|
@@ -27,354 +25,335 @@
|
|
|
27
25
|
*/
|
|
28
26
|
type QueryParamPairs=[[string,string|number],...[string,string|number][]];
|
|
29
27
|
/** ---------------------------------
|
|
30
|
-
* * ***
|
|
28
|
+
* * ***Utility: `constructURL`.***
|
|
31
29
|
* ---------------------------------
|
|
32
|
-
*
|
|
33
|
-
* @param {string | URL} baseUrl
|
|
34
|
-
*
|
|
35
|
-
* domain, and may include port and existing query parameters.
|
|
36
|
-
*
|
|
37
|
-
* @param {Iterable<[string, string]>|URLSearchParamsIterator<[string, string]>} [queryParams]
|
|
30
|
+
* **Constructs a valid URL with optional query parameters and allows selective removal of duplicate parameters.**
|
|
31
|
+
* @param {string | URL} baseUrl The base URL to build upon. Must include protocol (e.g., `"https://"`), `domain`, and may include port and existing query parameters.
|
|
32
|
+
* @param {Iterable<[string, string]> | URLSearchParamsIterator<[string, string]> | QueryParamPairs} [queryParams]
|
|
38
33
|
* Additional query parameters to append or overwrite on the URL.
|
|
39
|
-
* Accepts any iterable of key-value pairs (like `new URLSearchParams().entries()` and `[[string, string | number]...]`).
|
|
40
|
-
*
|
|
34
|
+
* - Accepts any iterable of key-value pairs (like `new URLSearchParams().entries()` and `[[string, string | number]...]`).
|
|
41
35
|
* @param {string[]} [removeParams]
|
|
42
|
-
* A list of query parameter keys to remove from the final URL,
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* @returns {URL}
|
|
46
|
-
* A new URL object representing the constructed URL with merged
|
|
47
|
-
* and cleaned query parameters.
|
|
48
|
-
*
|
|
49
|
-
* @throws {TypeError}
|
|
50
|
-
* Throws if `baseUrl` is not a valid non-empty string or URL object,
|
|
51
|
-
* or if `queryParams` is not iterable, or if `removeParams` is not an array of strings.
|
|
52
|
-
*
|
|
36
|
+
* A list of query parameter keys to remove from the final URL, whether they were in the base URL or provided queryParams.
|
|
37
|
+
* @returns {URL} A new URL object representing the constructed URL with merged and cleaned query parameters.
|
|
38
|
+
* @throws {TypeError} Throws if `baseUrl` is not a valid non-empty string or URL object, or if `queryParams` is not iterable, or if `removeParams` is not an array of strings.
|
|
53
39
|
* @example
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
40
|
+
* 1. #### Basic Usage:
|
|
41
|
+
* ```ts
|
|
42
|
+
* constructURL(
|
|
43
|
+
* "https://example.com/path",
|
|
44
|
+
* new URLSearchParams({ a: "1", b: "2" }).entries()
|
|
45
|
+
* );
|
|
46
|
+
* // ➔ URL { href: "https://example.com/path?a=1&b=2", ... }
|
|
47
|
+
* ```
|
|
48
|
+
* 2. #### Remove parameters from Base and Added:
|
|
49
|
+
* ```ts
|
|
50
|
+
* // with new URLSearchParams({ ... }).entries();
|
|
51
|
+
* constructURL(
|
|
52
|
+
* "https://example.com/path?foo=1&bar=2",
|
|
53
|
+
* new URLSearchParams({ bar: "ignored", baz: "3" }).entries(),
|
|
54
|
+
* ["bar"]
|
|
55
|
+
* );
|
|
56
|
+
* // ➔ URL { href: "https://example.com/path?foo=1&baz=3", ... }
|
|
57
|
+
*
|
|
58
|
+
* // with [[string, string | number]...]
|
|
59
|
+
* constructURL(
|
|
60
|
+
* "https://example.com/path?foo=1&bar=2",
|
|
61
|
+
* [["bar", "ignored"],["baz", 3]],
|
|
62
|
+
* ["bar"]
|
|
63
|
+
* );
|
|
64
|
+
* // ➔ URL { href: "https://example.com/path?foo=1&baz=3", ... }
|
|
65
|
+
*
|
|
66
|
+
* const params: QueryParamPairs = [
|
|
67
|
+
* ["foo", 1],
|
|
68
|
+
* ["bar", 2],
|
|
69
|
+
* ["baz", 3]
|
|
70
|
+
* ];
|
|
71
|
+
*
|
|
72
|
+
* constructURL("https://example.com", params, ["bar"]);
|
|
73
|
+
* // ➔ URL { href: "https://example.com/?foo=1&baz=3", ... }
|
|
74
|
+
* ```
|
|
77
75
|
*/
|
|
78
76
|
declare const constructURL:(baseUrl:string|URL,queryParams?:URLSearchParamsIterator<[string,string|number]>|QueryParamPairs,removeParams?:string[])=>URL;
|
|
79
77
|
/** ---------------------------------
|
|
80
|
-
* * ***
|
|
78
|
+
* * ***Utility: `extractURLs`.***
|
|
81
79
|
* ---------------------------------
|
|
82
|
-
*
|
|
80
|
+
* **Extracts all valid URLs from a given string.**
|
|
81
|
+
* @description
|
|
83
82
|
* This function scans the input url and returns an array of URLs
|
|
84
83
|
* that match a valid `http` or `https` format.
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* -
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* - Non-HTTP(S) protocols (ftp, mailto, etc)
|
|
95
|
-
*
|
|
96
|
-
* @param {string|null|undefined} [url] - The input string containing potential URLs.
|
|
84
|
+
* - **Supports:**
|
|
85
|
+
* - Internationalized domain names (IDN), e.g. `https://münich.de`
|
|
86
|
+
* - Unicode & emoji paths, e.g. `https://example.com/🎉/page`
|
|
87
|
+
* - Long URLs with multiple queries & fragments, e.g. `https://example.com/path?foo=1#hash`
|
|
88
|
+
* - **Ignores:**
|
|
89
|
+
* - Non-string inputs
|
|
90
|
+
* - Empty or whitespace-only strings
|
|
91
|
+
* - Non-HTTP(S) protocols (ftp, mailto, etc)
|
|
92
|
+
* @param {string | null | undefined} url - The input string containing potential URLs.
|
|
97
93
|
* @returns {string[] | null} An array of extracted URLs or `null` if no URLs are found.
|
|
98
94
|
* @example
|
|
99
95
|
* extractURLs("Visit https://example.com and https://例子.公司");
|
|
100
96
|
* // ➔ ["https://example.com", "https://例子.公司"]
|
|
101
|
-
*
|
|
102
|
-
* @example
|
|
103
97
|
* extractURLs("Here: https://example.com/🎉/page");
|
|
104
98
|
* // ➔ ["https://example.com/🎉/page"]
|
|
105
|
-
*
|
|
106
|
-
* @example
|
|
107
99
|
* extractURLs("ftp://example.com http://example.com");
|
|
108
100
|
* // ➔ ["http://example.com"]
|
|
109
101
|
*/
|
|
110
|
-
declare const extractURLs:(url
|
|
111
|
-
/**
|
|
112
|
-
* * ***Get Prefix from URL with Optional Base or Auto-detection (Supports String or Array of URLs).***
|
|
113
|
-
* --------------------------------------------------------
|
|
114
|
-
*
|
|
115
|
-
* @description
|
|
116
|
-
* This function extracts the prefix from one or more URLs. It can either:
|
|
117
|
-
* - Use a provided `base` string or an array of strings to check and return the matching prefix.
|
|
118
|
-
* - Automatically detect the prefix if no `base` is provided by analyzing the first part of the URL.
|
|
119
|
-
*
|
|
120
|
-
* The function is flexible and can handle both scenarios:
|
|
121
|
-
* 1. **When the base is provided as a single string or an array of strings**: The function will check if the URL starts with one of the provided base(s) and return the matching base.
|
|
122
|
-
* 2. **When the base is not provided**: The function will automatically detect the prefix by splitting the URL or using a regex.
|
|
123
|
-
*
|
|
124
|
-
* **Important Notes**:
|
|
125
|
-
* - If a base (or an array of bases) is provided, the URL must start with one of the given base(s).
|
|
126
|
-
* - If no base is provided, the function will attempt to detect the prefix automatically.
|
|
127
|
-
* - The `url` parameter can be either a string or an array of strings.
|
|
128
|
-
* - Supports deduplication of results (enabled by default).
|
|
129
|
-
* - Automatically returns a single string if only one unique result exists after processing.
|
|
130
|
-
*
|
|
131
|
-
* ---
|
|
102
|
+
declare const extractURLs:(url:string|null|undefined)=>string[]|null;type GetPrefixPathnameOptions={
|
|
103
|
+
/** The number of levels to include in the prefix (default is `1`). For example, with `levels = 2`, the function will return the first two parts of the URL.
|
|
132
104
|
*
|
|
133
|
-
* @
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
* - `removeDuplicates` (default `true`): Whether to remove duplicate prefixes from the final array result.
|
|
138
|
-
*
|
|
139
|
-
* @returns {string|string[]|null}
|
|
140
|
-
* Returns one of:
|
|
141
|
-
* - A single string if only one unique prefix/base is found.
|
|
142
|
-
* - An array of strings if multiple different prefixes/bases are found.
|
|
143
|
-
* - `null` if no matching base is found when using `base`.
|
|
144
|
-
*
|
|
145
|
-
* ---
|
|
146
|
-
*
|
|
147
|
-
* @throws {TypeError}
|
|
148
|
-
* Throws if:
|
|
149
|
-
* - `url` is not a string or an array of strings.
|
|
150
|
-
* - `base` is not a string, array of strings, or `null`.
|
|
151
|
-
* - `options` is not an object.
|
|
152
|
-
* - `levels` is not a number.
|
|
153
|
-
* - `removeDuplicates` is not a boolean.
|
|
154
|
-
*
|
|
155
|
-
* ---
|
|
156
|
-
*
|
|
157
|
-
* ### **🔹 Usage Examples**
|
|
105
|
+
* @default 1
|
|
106
|
+
*/
|
|
107
|
+
levels?:number;
|
|
108
|
+
/** Whether to remove duplicates from the result if multiple URLs are passed (default is `true`).
|
|
158
109
|
*
|
|
159
|
-
*
|
|
110
|
+
* @default true
|
|
111
|
+
*/
|
|
112
|
+
removeDuplicates?:boolean;};
|
|
113
|
+
/** --------------------------------------------------------
|
|
114
|
+
* * ***Utility: `getPrefixPathname`.***
|
|
115
|
+
* --------------------------------------------------------
|
|
116
|
+
* **Get Prefix from URL with Optional Base or Auto-detection (Supports String or Array of URLs).**
|
|
117
|
+
* - **This function extracts the prefix from one or more URLs. It can either:**
|
|
118
|
+
* - Use a provided `base` string or an array of strings to check and return the matching prefix.
|
|
119
|
+
* - Automatically detect the prefix if no `base` is provided by analyzing the first part of the URL.
|
|
120
|
+
* - **The function is flexible and can handle both scenarios:**
|
|
121
|
+
* 1. **When the base is provided as a single string or an array of strings**:
|
|
122
|
+
* - The function will check if the URL starts with one of the provided base(s) and return the matching base.
|
|
123
|
+
* 2. **When the base is not provided**:
|
|
124
|
+
* - The function will automatically detect the prefix by splitting the URL or using a regex.
|
|
125
|
+
* - **Important Notes**:
|
|
126
|
+
* - If a base (or an array of bases) is provided, the URL must start with one of the given base(s).
|
|
127
|
+
* - If no base is provided, the function will attempt to detect the prefix automatically.
|
|
128
|
+
* - The `url` parameter can be either a string or an array of strings.
|
|
129
|
+
* - Supports deduplication of results (enabled by default).
|
|
130
|
+
* - Automatically returns a single string if only one unique result exists after processing.
|
|
131
|
+
* @param {string|string[]} url The full URL(s) from which the prefix should be extracted.
|
|
132
|
+
* - Can be a `string` or an `array of strings`.
|
|
133
|
+
* @param {string|string[]|null} [base=null] The base URL(s) (e.g., `"/settings"`).
|
|
134
|
+
* - It can be a `string`, an `array of strings`, or `null`.
|
|
135
|
+
* - If `provided`, it will be used to check the URL(s).
|
|
136
|
+
* - If `not provided`, the prefix will be auto-detected.
|
|
137
|
+
* @param {GetPrefixPathnameOptions} [options] Additional options object:
|
|
138
|
+
* - `levels` (default `1`): The number of segments to include when auto-detecting the prefix (e.g. `/foo/bar` for `levels: 2`).
|
|
139
|
+
* - `removeDuplicates` (default `true`): Whether to remove duplicate prefixes from the final array result.
|
|
140
|
+
* @returns {string|string[]|null} Returns one of:
|
|
141
|
+
* - A single string if only one unique prefix/base is found.
|
|
142
|
+
* - An array of strings if multiple different prefixes/bases are found.
|
|
143
|
+
* - `null` if no matching base is found when using `base`.
|
|
144
|
+
* @throws {TypeError} Throws if:
|
|
145
|
+
* - `url` is `not a string` or `not an array of strings`.
|
|
146
|
+
* - `base` is `not a string`, `array of strings`, or `null`.
|
|
147
|
+
* - `options` is `not an object`.
|
|
148
|
+
* - `levels` is `not a number`.
|
|
149
|
+
* - `removeDuplicates` is `not a boolean`.
|
|
150
|
+
* @example
|
|
151
|
+
* - #### ✅ **Correct Usage (With an Array of URLs and Base):**
|
|
160
152
|
* ```ts
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
153
|
+
* const routes = [
|
|
154
|
+
* "/settings/profile",
|
|
155
|
+
* "/settings/password",
|
|
156
|
+
* "/settings/other-path",
|
|
157
|
+
* "/other-path/xyz",
|
|
158
|
+
* ];
|
|
159
|
+
*
|
|
160
|
+
* // With base provided as a string
|
|
161
|
+
* routes.forEach(route => {
|
|
162
|
+
* console.log(getPrefixPathname(route, '/settings'));
|
|
163
|
+
* // ➔ "/settings"
|
|
164
|
+
* });
|
|
165
|
+
*
|
|
166
|
+
* // With base provided as an array
|
|
167
|
+
* routes.forEach(route => {
|
|
168
|
+
* console.log(getPrefixPathname(route, ['/settings', '/admin']));
|
|
169
|
+
* // ➔ "/settings" or "/admin" depending on the current URL.
|
|
170
|
+
* });
|
|
179
171
|
* ```
|
|
180
|
-
*
|
|
181
|
-
* #### ✅ **Correct Usage (With Single URL and Single Base)**
|
|
172
|
+
* - #### ✅ **Correct Usage (With Single URL and Single Base):**
|
|
182
173
|
* ```ts
|
|
183
|
-
*
|
|
184
|
-
*
|
|
174
|
+
* const result = getPrefixPathname("/settings/profile", "/settings");
|
|
175
|
+
* console.log(result); // ➔ "/settings"
|
|
185
176
|
* ```
|
|
186
|
-
*
|
|
187
|
-
* #### ✅ **Correct Usage (With Multiple URLs and Single Base)**
|
|
177
|
+
* - #### ✅ **Correct Usage (With Multiple URLs and Single Base):**
|
|
188
178
|
* ```ts
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
179
|
+
* const result = getPrefixPathname(
|
|
180
|
+
* ["/settings/profile", "/settings/password"],
|
|
181
|
+
* "/settings"
|
|
182
|
+
* );
|
|
183
|
+
* console.log(result); // ➔ "/settings"
|
|
184
|
+
*
|
|
185
|
+
* const result2 = getPrefixPathname(
|
|
186
|
+
* ["/settings/profile", "/other/password"],
|
|
187
|
+
* "/other"
|
|
188
|
+
* );
|
|
189
|
+
* console.log(result2); // ➔ "/other"
|
|
194
190
|
* ```
|
|
195
|
-
*
|
|
196
|
-
* #### ✅ **Correct Usage (With Multiple URLs and Multiple Bases)**
|
|
191
|
+
* - #### ✅ **Correct Usage (With Multiple URLs and Multiple Bases)**
|
|
197
192
|
* ```ts
|
|
198
|
-
*
|
|
199
|
-
*
|
|
193
|
+
* const result = getPrefixPathname(
|
|
194
|
+
* ["/settings/profile", "/admin/password"],
|
|
195
|
+
* ["/settings", "/admin"]
|
|
196
|
+
* );
|
|
197
|
+
* console.log(result); // ➔ ["/settings", "/admin"]
|
|
200
198
|
* ```
|
|
201
|
-
*
|
|
202
|
-
* #### ✅ **Auto-detection of Prefix**
|
|
199
|
+
* - #### ✅ **Auto-detection of Prefix**
|
|
203
200
|
* ```ts
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
201
|
+
* const result = getPrefixPathname("/settings/profile");
|
|
202
|
+
* console.log(result); // ➔ "/settings"
|
|
203
|
+
*
|
|
204
|
+
* const result2 = getPrefixPathname(
|
|
205
|
+
* "/settings/profile/info",
|
|
206
|
+
* null,
|
|
207
|
+
* { levels: 2 }
|
|
208
|
+
* );
|
|
209
|
+
* console.log(result2); // ➔ "/settings/profile"
|
|
209
210
|
* ```
|
|
210
|
-
*
|
|
211
|
-
* #### ✅ **Multiple URLs with Auto-detection**
|
|
211
|
+
* - #### ✅ **Multiple URLs with Auto-detection**
|
|
212
212
|
* ```ts
|
|
213
|
-
*
|
|
214
|
-
*
|
|
213
|
+
* const result = getPrefixPathname(["/admin/profile", "/settings/password"]);
|
|
214
|
+
* console.log(result); // ➔ ["/admin", "/settings"]
|
|
215
215
|
* ```
|
|
216
|
-
*
|
|
217
|
-
* #### ✅ **Handling Duplicates**
|
|
216
|
+
* - #### ✅ **Handling Duplicates**
|
|
218
217
|
* ```ts
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
218
|
+
* const result = getPrefixPathname(
|
|
219
|
+
* ["/settings/profile", "/settings/password"],
|
|
220
|
+
* "/settings"
|
|
221
|
+
* );
|
|
222
|
+
* console.log(result); // ➔ "/settings" (deduped to single string)
|
|
223
|
+
*
|
|
224
|
+
* const result2 = getPrefixPathname(
|
|
225
|
+
* ["/settings/profile", "/settings/profile"],
|
|
226
|
+
* "/settings",
|
|
227
|
+
* { removeDuplicates: false }
|
|
228
|
+
* );
|
|
229
|
+
* console.log(result2); // ➔ ["/settings", "/settings"]
|
|
224
230
|
* ```
|
|
225
|
-
*
|
|
226
|
-
* #### ❌ **Incorrect Usage (URL Does Not Match Base)**
|
|
231
|
+
* - #### ❌ **Incorrect Usage (URL Does Not Match Base)**
|
|
227
232
|
* ```ts
|
|
228
|
-
*
|
|
229
|
-
*
|
|
233
|
+
* const result = getPrefixPathname("/other-path/profile", "/settings");
|
|
234
|
+
* console.log(result); // ➔ null
|
|
230
235
|
* ```
|
|
231
236
|
*/
|
|
232
|
-
declare const getPrefixPathname:(url:string|string[],base?:string|string[]|null,options?:
|
|
233
|
-
/** The number of levels to include in the prefix (default is 1). For example, with `levels = 2`, the function will return the first two parts of the URL. */
|
|
234
|
-
levels?:number;
|
|
235
|
-
/** Whether to remove duplicates from the result if multiple URLs are passed (default is `true`). */
|
|
236
|
-
removeDuplicates?:boolean;})=>string|string[]|null;
|
|
237
|
+
declare const getPrefixPathname:(url:string|string[],base?:string|string[]|null,options?:GetPrefixPathnameOptions)=>string|string[]|null;
|
|
237
238
|
/** --------------------------------------------------------
|
|
238
|
-
* * ***
|
|
239
|
+
* * ***Utility: `getFirstPrefixPathname`.***
|
|
239
240
|
* --------------------------------------------------------
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
* This function helps extract the first valid URL prefix from various possible inputs.
|
|
243
|
-
* It is especially useful in routing systems, middleware, or frontend apps that
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
* -
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
* ### 🔍 Validation & Errors:
|
|
267
|
-
*
|
|
268
|
-
* - Throws a `TypeError` if:
|
|
269
|
-
* - `defaultValue` is not a non-empty string.
|
|
270
|
-
* - `result` is an array that contains non-string elements.
|
|
271
|
-
* - `result` is a value that is neither `string`, `string[]`, nor `null`.
|
|
272
|
-
*
|
|
273
|
-
* ---
|
|
274
|
-
*
|
|
275
|
-
* ### 🛠 Usage Examples:
|
|
276
|
-
*
|
|
277
|
-
* 1. #### For React: *Determining layout*
|
|
241
|
+
* **Extract First Valid Prefix from Path Array or String.**
|
|
242
|
+
* - **Main Purpose:**
|
|
243
|
+
* - This function helps extract the first valid URL prefix from various possible inputs.
|
|
244
|
+
* - It is especially useful in routing systems, middleware, or frontend apps that need to
|
|
245
|
+
* decide layout, active navigation, or permissions based on the first segment (or prefix) of a pathname.
|
|
246
|
+
* - **Typical uses include:**
|
|
247
|
+
* - Determining which layout to render (e.g., `/admin` vs `/dashboard` vs `/`).
|
|
248
|
+
* - Highlighting the active menu item in a sidebar based on the current URL.
|
|
249
|
+
* - Enforcing route guards or access controls depending on the URL prefix.
|
|
250
|
+
* - Parsing multi-level route prefixes and selecting the most relevant one.
|
|
251
|
+
* - **Behavior:**
|
|
252
|
+
* - It works as follows:
|
|
253
|
+
* - If `result` is an array of strings, it normalizes each element and returns
|
|
254
|
+
* the first non-root path (i.e., not just `"/"`).
|
|
255
|
+
* - If all items normalize to `"/"`,
|
|
256
|
+
* it returns the `defaultValue` (normalized).
|
|
257
|
+
* - If `result` is a single string, it normalizes it and returns it if valid,
|
|
258
|
+
* otherwise falls back to the normalized `defaultValue`.
|
|
259
|
+
* - If `result` is `null` or `undefined`, it returns the normalized `defaultValue`.
|
|
260
|
+
* - **Validation & Errors:**
|
|
261
|
+
* - Throws a `TypeError` if:
|
|
262
|
+
* - `defaultValue` is not a string or empty-string.
|
|
263
|
+
* - `result` is an array that contains non-string elements.
|
|
264
|
+
* - `result` is a value that is neither `string`, `string[]`, nor `null`.
|
|
265
|
+
* @example
|
|
266
|
+
* 1. #### For React (*Determining layout*):
|
|
278
267
|
* ```ts
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
268
|
+
* const prefix = getFirstPrefixPathname(
|
|
269
|
+
* getPrefixPathname(
|
|
270
|
+
* "/admin/settings",
|
|
271
|
+
* ["/admin", "/dashboard"]
|
|
272
|
+
* )
|
|
273
|
+
* );
|
|
274
|
+
*
|
|
275
|
+
* if (prefix === "/admin") {
|
|
276
|
+
* renderAdminLayout();
|
|
277
|
+
* }
|
|
283
278
|
* ```
|
|
284
279
|
*
|
|
285
|
-
*
|
|
280
|
+
* 2. #### Setting active menu state:
|
|
286
281
|
* ```ts
|
|
287
|
-
*
|
|
288
|
-
*
|
|
282
|
+
* const activeSection = getFirstPrefixPathname(["", "/dashboard", "/profile"]);
|
|
283
|
+
* // ➔ "/dashboard"
|
|
289
284
|
* ```
|
|
290
285
|
*
|
|
291
|
-
*
|
|
286
|
+
* 3. #### Providing graceful fallback:
|
|
292
287
|
* ```ts
|
|
293
|
-
*
|
|
294
|
-
*
|
|
288
|
+
* const section = getFirstPrefixPathname([], "/home");
|
|
289
|
+
* // ➔ "/home"
|
|
295
290
|
* ```
|
|
296
|
-
*
|
|
291
|
+
* 4. #### ✅ Using with an Array of Pathnames:
|
|
297
292
|
* ```ts
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
*
|
|
293
|
+
* const result = getPrefixPathname([" ", "/dashboard", "/settings"]);
|
|
294
|
+
* console.log(getFirstPrefixPathname(result));
|
|
295
|
+
* // ➔ "/dashboard"
|
|
301
296
|
* ```
|
|
302
297
|
*
|
|
303
|
-
*
|
|
298
|
+
* 5. #### ✅ Using with Single String:
|
|
304
299
|
* ```ts
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
300
|
+
* console.log(getFirstPrefixPathname("/profile/settings"));
|
|
301
|
+
* // ➔ "/profile/settings"
|
|
302
|
+
* console.log(getFirstPrefixPathname(" "));
|
|
303
|
+
* // ➔ "/"
|
|
309
304
|
* ```
|
|
310
305
|
*
|
|
311
|
-
*
|
|
306
|
+
* 6. #### ✅ Fallback to Custom Default:
|
|
312
307
|
* ```ts
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
308
|
+
* console.log(getFirstPrefixPathname([" ", ""], "/home"));
|
|
309
|
+
* // ➔ "/home"
|
|
310
|
+
* console.log(getFirstPrefixPathname(null, "/dashboard"));
|
|
311
|
+
* // ➔ "/dashboard"
|
|
317
312
|
* ```
|
|
318
313
|
*
|
|
319
|
-
*
|
|
314
|
+
* 7. #### ✅ Throws on Invalid Input:
|
|
320
315
|
* ```ts
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
*
|
|
316
|
+
* getFirstPrefixPathname([1, 2] as any); // ➔ ❌ throws TypeError
|
|
317
|
+
* getFirstPrefixPathname({} as any); // ➔ ❌ throws TypeError
|
|
318
|
+
* getFirstPrefixPathname(null, " "); // ➔ ❌ throws TypeError
|
|
324
319
|
* ```
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
* @param {string | string[] | null} result
|
|
329
|
-
* The pathname(s) to process. Can be:
|
|
320
|
+
* @param {string | string[] | null | undefined} result
|
|
321
|
+
* The pathname(s) to process, can be:
|
|
330
322
|
* - A string path (e.g. `"/profile"`),
|
|
331
323
|
* - An array of string paths (e.g. `[" ", "/dashboard"]`),
|
|
332
324
|
* - Or `null`.
|
|
333
|
-
*
|
|
334
325
|
* @param {string} [defaultValue="/"]
|
|
335
326
|
* A custom default path to use if `result` is null or no valid prefix is found.
|
|
336
|
-
*
|
|
337
|
-
*
|
|
327
|
+
* - Must be a string and non-empty string.
|
|
328
|
+
* - Defaults to `"/"`.
|
|
338
329
|
* @returns {string}
|
|
339
330
|
* The first valid normalized pathname, or the normalized default.
|
|
340
|
-
*
|
|
341
331
|
* @throws {TypeError}
|
|
342
|
-
* If `result` is not a valid type, or `defaultValue` is not a
|
|
332
|
+
* If `result` is not a valid type, or `defaultValue` is not a string or empty-string.
|
|
343
333
|
*/
|
|
344
|
-
declare const getFirstPrefixPathname:(result
|
|
334
|
+
declare const getFirstPrefixPathname:(result:string|string[]|null|undefined,defaultValue?:string)=>string;
|
|
345
335
|
/** --------------------------------------------------------
|
|
346
|
-
* * ***
|
|
336
|
+
* * ***Utility: `normalizePathname`.***
|
|
347
337
|
* --------------------------------------------------------
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
* Example: `"https://site.com/foo/bar?x=1#sec"` becomes `"/foo/bar?x=1#sec"`.
|
|
358
|
-
*
|
|
359
|
-
* - All spaces inside the pathname are removed.
|
|
360
|
-
*
|
|
361
|
-
* - Multiple consecutive slashes (like `"//"` or `"///"`) are collapsed into a single slash `"/"`.
|
|
362
|
-
*
|
|
363
|
-
* - Ensures the returned string always starts with exactly one `/`.
|
|
364
|
-
*
|
|
338
|
+
* **This function processes and normalizes a given pathname.**
|
|
339
|
+
* - **Behavior:**
|
|
340
|
+
* - If `pathname` is `null`, `undefined`, empty, or only whitespace, the `defaultPath` will be returned instead.
|
|
341
|
+
* - If `pathname` is a full URL (starting with `http://` or `https://`), it extracts and returns the pathname along
|
|
342
|
+
* with any search parameters and hash.
|
|
343
|
+
* - Example: `"https://site.com/foo/bar?x=1#sec"` becomes `"/foo/bar?x=1#sec"`.
|
|
344
|
+
* - All spaces inside the pathname are removed.
|
|
345
|
+
* - Multiple consecutive slashes (like `"//"` or `"///"`) are collapsed into a single slash `"/"`.
|
|
346
|
+
* - Ensures the returned string always starts with exactly one `/`.
|
|
365
347
|
* @param {string | null | undefined} pathname - The pathname to normalize.
|
|
366
|
-
* @param {string} [defaultPath="/"] - A fallback value returned if `pathname` is empty or invalid.
|
|
367
|
-
*
|
|
348
|
+
* @param {string} [defaultPath="/"] - A fallback value returned if `pathname` is empty or invalid.
|
|
349
|
+
* - Must be a string and non-empty string, default `"/"`.
|
|
368
350
|
* @returns {string} A properly normalized pathname starting with a single `/`,
|
|
369
351
|
* or the `defaultPath` if the input is invalid or empty.
|
|
370
|
-
*
|
|
371
|
-
* @throws {TypeError} If `defaultPath` is not a non-empty string.
|
|
352
|
+
* @throws {TypeError} If `defaultPath` is not a string or empty-string.
|
|
372
353
|
* @throws {NormalizePathnameError} If an unexpected error occurs during normalization (e.g., URL parsing failure).
|
|
373
|
-
*
|
|
374
354
|
* @example
|
|
375
355
|
* normalizePathname(" /foo//bar ");
|
|
376
356
|
* // ➔ "/foo/bar"
|
|
377
|
-
*
|
|
378
357
|
* normalizePathname("https://example.com//path///to/resource?x=1#hash");
|
|
379
358
|
* // ➔ "/path/to/resource?x=1#hash"
|
|
380
359
|
* normalizePathname(" ");
|
|
@@ -383,34 +362,32 @@ declare const getFirstPrefixPathname:(result?:string|string[]|null,defaultValue?
|
|
|
383
362
|
* // ➔ "/home"
|
|
384
363
|
* normalizePathname("/double//slashes");
|
|
385
364
|
* // ➔ "/double/slashes"
|
|
386
|
-
|
|
365
|
+
* normalizePathname(" nested / path / 🚀 ");
|
|
387
366
|
* // ➔ "/nested/path/🚀"
|
|
388
367
|
*/
|
|
389
|
-
declare const normalizePathname:(pathname
|
|
368
|
+
declare const normalizePathname:(pathname:string|null|undefined,defaultPath?:string)=>string;type FormatEnvPortOptions={
|
|
369
|
+
/** Add prefix with a colon, defaultValue: `false`.
|
|
370
|
+
*
|
|
371
|
+
* @default false
|
|
372
|
+
*/
|
|
373
|
+
prefixColon?:boolean;};
|
|
390
374
|
/** -----------------------------------------------
|
|
391
|
-
* * ***
|
|
375
|
+
* * ***Utility: `formatEnvPort`.***
|
|
392
376
|
* -----------------------------------------------
|
|
393
|
-
*
|
|
394
|
-
* -
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
*
|
|
399
|
-
* @param envVar The environment variable string.
|
|
400
|
-
* @param options Optional object: `{ prefixColon?: boolean }`.
|
|
401
|
-
* @returns A string like ":8080" or "8080"
|
|
402
|
-
*
|
|
403
|
-
* @throws TypeError if `options` is not an object or `prefixColon` is not boolean.
|
|
404
|
-
*
|
|
377
|
+
* **Retrieves and formats an environment port variable.**
|
|
378
|
+
* - **Behavior:**
|
|
379
|
+
* - Extracts only digits from the input.
|
|
380
|
+
* - If no digits found, returns an empty string.
|
|
381
|
+
* - By default does NOT prefix with a colon.
|
|
382
|
+
* - Use `{ prefixColon: true }` to prefix with a colon.
|
|
383
|
+
* @param {string | null | undefined} envVar The environment variable string.
|
|
384
|
+
* @param {FormatEnvPortOptions} [options] Optional object: `{ prefixColon?: boolean }`.
|
|
385
|
+
* @returns {string} A string like `":8080"` or `"8080"`, or `""` if no digits.
|
|
386
|
+
* @throws {TypeError} If `options` is not an object or `prefixColon` is not boolean.
|
|
405
387
|
* @example
|
|
406
388
|
* formatEnvPort("port:8080");
|
|
407
389
|
* // ➔ "8080"
|
|
408
390
|
* formatEnvPort("port:8080", { prefixColon: true });
|
|
409
391
|
* // ➔ ":8080"
|
|
410
392
|
*/
|
|
411
|
-
declare const formatEnvPort:(envVar
|
|
412
|
-
/** Add prefix with a colon.
|
|
413
|
-
*
|
|
414
|
-
* @default false
|
|
415
|
-
*/
|
|
416
|
-
prefixColon?:boolean;})=>string;export{type QueryParamPairs,constructURL,extractURLs,formatEnvPort,getFirstPrefixPathname,getPrefixPathname,normalizePathname};
|
|
393
|
+
declare const formatEnvPort:(envVar:string|null|undefined,options?:FormatEnvPortOptions)=>string;export{type QueryParamPairs,constructURL,extractURLs,formatEnvPort,getFirstPrefixPathname,getPrefixPathname,normalizePathname};
|