@tempots/std 0.19.0 → 0.21.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/url.d.ts ADDED
@@ -0,0 +1,232 @@
1
+ /**
2
+ * Utilities for working with URLs and paths.
3
+ *
4
+ * This module provides comprehensive URL manipulation, query parameter handling,
5
+ * and path utilities. Functions are designed to work safely with both absolute
6
+ * and relative URLs, and handle edge cases gracefully.
7
+ *
8
+ * @public
9
+ */
10
+ /**
11
+ * Safely parses a URL string into a URL object.
12
+ *
13
+ * This function provides better error handling than the URL constructor
14
+ * by returning null for invalid URLs instead of throwing.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const url = parseUrl('https://example.com/path?param=value')
19
+ * // Result: URL object
20
+ *
21
+ * const invalid = parseUrl('not-a-url')
22
+ * // Result: null
23
+ * ```
24
+ *
25
+ * @param url - The URL string to parse
26
+ * @returns A URL object if valid, null otherwise
27
+ * @public
28
+ */
29
+ export declare const parseUrl: (url: string) => URL | null;
30
+ /**
31
+ * Builds a URL from a base URL and query parameters.
32
+ *
33
+ * This function safely constructs URLs with query parameters,
34
+ * handling encoding and existing parameters properly.
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * const url = buildUrl('https://api.example.com/users', {
39
+ * page: '1',
40
+ * limit: '10',
41
+ * search: 'john doe'
42
+ * })
43
+ * // Result: 'https://api.example.com/users?page=1&limit=10&search=john%20doe'
44
+ * ```
45
+ *
46
+ * @param base - The base URL
47
+ * @param params - Query parameters to add
48
+ * @returns The constructed URL string
49
+ * @public
50
+ */
51
+ export declare const buildUrl: (base: string, params?: Record<string, string>) => string;
52
+ /**
53
+ * Extracts query parameters from a URL as an object.
54
+ *
55
+ * This function parses the query string and returns all parameters
56
+ * as a plain object with string values.
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * const params = getQueryParams('https://example.com/path?name=john&age=30&active=true')
61
+ * // Result: { name: 'john', age: '30', active: 'true' }
62
+ *
63
+ * const empty = getQueryParams('https://example.com/path')
64
+ * // Result: {}
65
+ * ```
66
+ *
67
+ * @param url - The URL to extract parameters from
68
+ * @returns An object containing all query parameters
69
+ * @public
70
+ */
71
+ export declare const getQueryParams: (url: string) => Record<string, string>;
72
+ /**
73
+ * Sets a query parameter in a URL.
74
+ *
75
+ * This function adds or updates a single query parameter in a URL,
76
+ * preserving all other parameters and URL components.
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * const url = setQueryParam('https://example.com/path?existing=value', 'new', 'param')
81
+ * // Result: 'https://example.com/path?existing=value&new=param'
82
+ *
83
+ * const updated = setQueryParam('https://example.com/path?existing=old', 'existing', 'new')
84
+ * // Result: 'https://example.com/path?existing=new'
85
+ * ```
86
+ *
87
+ * @param url - The base URL
88
+ * @param key - The parameter key
89
+ * @param value - The parameter value
90
+ * @returns The URL with the parameter set
91
+ * @public
92
+ */
93
+ export declare const setQueryParam: (url: string, key: string, value: string) => string;
94
+ /**
95
+ * Removes a query parameter from a URL.
96
+ *
97
+ * This function removes a specific query parameter from a URL,
98
+ * preserving all other parameters and URL components.
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * const url = removeQueryParam('https://example.com/path?keep=this&remove=that', 'remove')
103
+ * // Result: 'https://example.com/path?keep=this'
104
+ * ```
105
+ *
106
+ * @param url - The base URL
107
+ * @param key - The parameter key to remove
108
+ * @returns The URL with the parameter removed
109
+ * @public
110
+ */
111
+ export declare const removeQueryParam: (url: string, key: string) => string;
112
+ /**
113
+ * Checks if a string is a valid URL.
114
+ *
115
+ * This function validates URLs more safely than using the URL constructor
116
+ * directly, as it doesn't throw exceptions.
117
+ *
118
+ * @example
119
+ * ```typescript
120
+ * isValidUrl('https://example.com') // true
121
+ * isValidUrl('http://localhost:3000/path') // true
122
+ * isValidUrl('ftp://files.example.com') // true
123
+ * isValidUrl('not-a-url') // false
124
+ * isValidUrl('') // false
125
+ * ```
126
+ *
127
+ * @param url - The string to validate
128
+ * @returns true if the string is a valid URL, false otherwise
129
+ * @public
130
+ */
131
+ export declare const isValidUrl: (url: string) => boolean;
132
+ /**
133
+ * Joins multiple path segments into a single path.
134
+ *
135
+ * This function properly handles leading and trailing slashes,
136
+ * ensuring a clean path without double slashes or missing separators.
137
+ *
138
+ * @example
139
+ * ```typescript
140
+ * const path = joinPaths('api', 'v1', 'users', '123')
141
+ * // Result: 'api/v1/users/123'
142
+ *
143
+ * const withSlashes = joinPaths('/api/', '/v1/', 'users/')
144
+ * // Result: '/api/v1/users/'
145
+ *
146
+ * const empty = joinPaths('', 'path', '', 'file')
147
+ * // Result: 'path/file'
148
+ * ```
149
+ *
150
+ * @param paths - Path segments to join
151
+ * @returns The joined path
152
+ * @public
153
+ */
154
+ export declare const joinPaths: (...paths: string[]) => string;
155
+ /**
156
+ * Normalizes a path by removing redundant segments.
157
+ *
158
+ * This function resolves '..' and '.' segments in paths,
159
+ * similar to path.normalize in Node.js but for web contexts.
160
+ *
161
+ * @example
162
+ * ```typescript
163
+ * const path = normalizePath('/api/../users/./123/../456')
164
+ * // Result: '/users/456'
165
+ *
166
+ * const relative = normalizePath('../../parent/child')
167
+ * // Result: '../../parent/child'
168
+ * ```
169
+ *
170
+ * @param path - The path to normalize
171
+ * @returns The normalized path
172
+ * @public
173
+ */
174
+ export declare const normalizePath: (path: string) => string;
175
+ /**
176
+ * Extracts the file extension from a path.
177
+ *
178
+ * This function returns the file extension including the dot,
179
+ * or an empty string if no extension is found.
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * getFileExtension('/path/to/file.txt') // '.txt'
184
+ * getFileExtension('image.jpeg') // '.jpeg'
185
+ * getFileExtension('README') // ''
186
+ * getFileExtension('.hidden') // ''
187
+ * getFileExtension('file.tar.gz') // '.gz'
188
+ * ```
189
+ *
190
+ * @param path - The path to extract extension from
191
+ * @returns The file extension including the dot, or empty string
192
+ * @public
193
+ */
194
+ export declare const getFileExtension: (path: string) => string;
195
+ /**
196
+ * Extracts the file name from a path.
197
+ *
198
+ * This function returns the last segment of a path,
199
+ * which is typically the file name.
200
+ *
201
+ * @example
202
+ * ```typescript
203
+ * getFileName('/path/to/file.txt') // 'file.txt'
204
+ * getFileName('file.txt') // 'file.txt'
205
+ * getFileName('/path/to/directory/') // ''
206
+ * getFileName('/path/to/directory') // 'directory'
207
+ * ```
208
+ *
209
+ * @param path - The path to extract file name from
210
+ * @returns The file name
211
+ * @public
212
+ */
213
+ export declare const getFileName: (path: string) => string;
214
+ /**
215
+ * Extracts the base name (file name without extension) from a path.
216
+ *
217
+ * This function returns the file name without its extension.
218
+ *
219
+ * @example
220
+ * ```typescript
221
+ * getBaseName('/path/to/file.txt') // 'file'
222
+ * getBaseName('image.jpeg') // 'image'
223
+ * getBaseName('README') // 'README'
224
+ * getBaseName('.hidden') // '.hidden'
225
+ * getBaseName('file.tar.gz') // 'file.tar'
226
+ * ```
227
+ *
228
+ * @param path - The path to extract base name from
229
+ * @returns The base name without extension
230
+ * @public
231
+ */
232
+ export declare const getBaseName: (path: string) => string;
package/url.js ADDED
@@ -0,0 +1,79 @@
1
+ const a = (t) => {
2
+ try {
3
+ return new URL(t);
4
+ } catch {
5
+ return null;
6
+ }
7
+ }, u = (t, e) => {
8
+ if (!e || Object.keys(e).length === 0)
9
+ return t;
10
+ const n = new URL(t);
11
+ for (const [r, s] of Object.entries(e))
12
+ n.searchParams.set(r, s);
13
+ return n.toString();
14
+ }, h = (t) => {
15
+ try {
16
+ const e = new URL(t), n = {};
17
+ for (const [r, s] of e.searchParams)
18
+ n[r] = s;
19
+ return n;
20
+ } catch {
21
+ return {};
22
+ }
23
+ }, f = (t, e, n) => {
24
+ try {
25
+ const r = new URL(t);
26
+ return r.searchParams.set(e, n), r.toString();
27
+ } catch {
28
+ return t;
29
+ }
30
+ }, g = (t, e) => {
31
+ try {
32
+ const n = new URL(t);
33
+ return n.searchParams.delete(e), n.toString();
34
+ } catch {
35
+ return t;
36
+ }
37
+ }, m = (t) => {
38
+ try {
39
+ return new URL(t), !0;
40
+ } catch {
41
+ return !1;
42
+ }
43
+ }, y = (...t) => {
44
+ if (t.length === 0) return "";
45
+ const e = t.filter((c) => c.length > 0);
46
+ if (e.length === 0) return "";
47
+ const n = e[0].startsWith("/"), r = e[e.length - 1].endsWith("/"), o = e.map((c) => c.replace(/^\/+|\/+$/g, "")).filter((c) => c.length > 0).join("/");
48
+ return (n ? "/" : "") + o + (r ? "/" : "");
49
+ }, P = (t) => {
50
+ if (!t) return "";
51
+ const e = t.startsWith("/"), n = t.split("/").filter((o) => o.length > 0), r = [];
52
+ for (const o of n)
53
+ o !== "." && (o === ".." ? r.length > 0 && r[r.length - 1] !== ".." ? r.pop() : e || r.push("..") : r.push(o));
54
+ const s = r.join("/");
55
+ return e ? "/" + s : s;
56
+ }, i = (t) => {
57
+ const e = l(t), n = e.lastIndexOf(".");
58
+ return n === -1 || n === 0 ? "" : e.substring(n);
59
+ }, l = (t) => {
60
+ if (!t || t.match(/\/+$/)) return "";
61
+ const e = t.lastIndexOf("/");
62
+ return e === -1 ? t : t.substring(e + 1);
63
+ }, d = (t) => {
64
+ const e = l(t), n = i(t);
65
+ return n ? e.substring(0, e.length - n.length) : e;
66
+ };
67
+ export {
68
+ u as buildUrl,
69
+ d as getBaseName,
70
+ i as getFileExtension,
71
+ l as getFileName,
72
+ h as getQueryParams,
73
+ m as isValidUrl,
74
+ y as joinPaths,
75
+ P as normalizePath,
76
+ a as parseUrl,
77
+ g as removeQueryParam,
78
+ f as setQueryParam
79
+ };