toolbox-x 1.0.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/LICENSE +201 -0
- package/dist/Color-B3mgF9Dh.d.cts +486 -0
- package/dist/Color-D38Xrw65.d.mts +486 -0
- package/dist/Stylog-Df7eq3-j.d.cts +519 -0
- package/dist/Stylog-jvlLcMQq.d.mts +519 -0
- package/dist/array-DvW0zIu6.d.mts +130 -0
- package/dist/array-rUnEVisO.d.cts +130 -0
- package/dist/basics-D_eSv0cu.cjs +132 -0
- package/dist/basics-Dp_aEK81.mjs +115 -0
- package/dist/basics-WEYWlnRO.d.cts +95 -0
- package/dist/basics-uBSfkBEI.d.mts +95 -0
- package/dist/case-BWIt8Ash.mjs +449 -0
- package/dist/case-C-S-b5YP.d.cts +327 -0
- package/dist/case-CS8Ii3A7.cjs +526 -0
- package/dist/case-CybASFPD.d.mts +327 -0
- package/dist/change-case.cjs +32 -0
- package/dist/change-case.d.cts +18 -0
- package/dist/change-case.d.mts +18 -0
- package/dist/change-case.mjs +19 -0
- package/dist/colors.cjs +574 -0
- package/dist/colors.d.cts +355 -0
- package/dist/colors.d.mts +355 -0
- package/dist/colors.mjs +547 -0
- package/dist/constants-2gAw23_7.mjs +144 -0
- package/dist/constants-B34K0QPi.d.cts +21 -0
- package/dist/constants-BIBDKY1u.cjs +924 -0
- package/dist/constants-BWT-810U.cjs +158 -0
- package/dist/constants-BwbHnXlM.mjs +662 -0
- package/dist/constants-BxN9l5el.cjs +74 -0
- package/dist/constants-CLS_bgKD.d.mts +847 -0
- package/dist/constants-D73iFu8g.mjs +171 -0
- package/dist/constants-DAfRxaa8.mjs +62 -0
- package/dist/constants-DQYeCjlx.cjs +207 -0
- package/dist/constants-Deeie-iH.d.mts +21 -0
- package/dist/constants-DpTG9RP6.d.mts +29 -0
- package/dist/constants-DqwnkJ_d.cjs +740 -0
- package/dist/constants-DvRUY_FY.cjs +150 -0
- package/dist/constants-VcRtQu0K.d.cts +29 -0
- package/dist/constants-X5hm1UtB.mjs +912 -0
- package/dist/constants-eNd-iYsV.mjs +134 -0
- package/dist/constants-qm8FafmD.d.cts +847 -0
- package/dist/constants.cjs +415 -0
- package/dist/constants.d.cts +184 -0
- package/dist/constants.d.mts +184 -0
- package/dist/constants.mjs +378 -0
- package/dist/convert-BOCgUv2D.cjs +252 -0
- package/dist/convert-Bn4jFomQ.mjs +169 -0
- package/dist/convert-BrzlG-m_.cjs +475 -0
- package/dist/convert-DhaUoPVU.mjs +368 -0
- package/dist/converter-1P90_RcP.d.mts +402 -0
- package/dist/converter-CmkcAppi.d.cts +402 -0
- package/dist/converter.cjs +780 -0
- package/dist/converter.d.cts +29 -0
- package/dist/converter.d.mts +29 -0
- package/dist/converter.mjs +771 -0
- package/dist/countries-CIpmtEzV.cjs +1469 -0
- package/dist/countries-Cy0xiqS3.mjs +1463 -0
- package/dist/css-colors-Bx947Ng3.d.cts +179 -0
- package/dist/css-colors-CXCDqQbG.cjs +186 -0
- package/dist/css-colors-CXTp1vvy.d.mts +179 -0
- package/dist/css-colors-DfUW3nTR.mjs +180 -0
- package/dist/date.cjs +332 -0
- package/dist/date.d.cts +213 -0
- package/dist/date.d.mts +213 -0
- package/dist/date.mjs +298 -0
- package/dist/dom.cjs +461 -0
- package/dist/dom.d.cts +228 -0
- package/dist/dom.d.mts +228 -0
- package/dist/dom.mjs +429 -0
- package/dist/form-BMFVGUrN.d.mts +118 -0
- package/dist/form-DRFbryvK.d.cts +118 -0
- package/dist/guards-3kaUX66g.mjs +157 -0
- package/dist/guards-C8gkvIHb.cjs +240 -0
- package/dist/guards-DdyU4h4o.mjs +110 -0
- package/dist/guards-Efhp1mNy.cjs +151 -0
- package/dist/guards.cjs +172 -0
- package/dist/guards.d.cts +399 -0
- package/dist/guards.d.mts +399 -0
- package/dist/guards.mjs +75 -0
- package/dist/hash-B6JPEyAz.d.mts +131 -0
- package/dist/hash-NTpeKYB_.d.cts +131 -0
- package/dist/hash.cjs +2126 -0
- package/dist/hash.d.cts +1239 -0
- package/dist/hash.d.mts +1239 -0
- package/dist/hash.mjs +2095 -0
- package/dist/http-status-BAZdtr7-.d.mts +65 -0
- package/dist/http-status-U_3MtoGb.d.cts +65 -0
- package/dist/http-status.cjs +173 -0
- package/dist/http-status.d.cts +142 -0
- package/dist/http-status.d.mts +142 -0
- package/dist/http-status.mjs +171 -0
- package/dist/index.cjs +2551 -0
- package/dist/index.d.cts +1493 -0
- package/dist/index.d.mts +1493 -0
- package/dist/index.mjs +2357 -0
- package/dist/object-B0TV3eHx.d.mts +8052 -0
- package/dist/object-Blq0Amdv.d.cts +8052 -0
- package/dist/objectify-CDs0Fbr1.mjs +417 -0
- package/dist/objectify-DIJ-OBmo.cjs +524 -0
- package/dist/paginator.cjs +245 -0
- package/dist/paginator.d.cts +144 -0
- package/dist/paginator.d.mts +144 -0
- package/dist/paginator.mjs +243 -0
- package/dist/parse-2ubxXZRp.cjs +211 -0
- package/dist/parse-N7g942uy.mjs +164 -0
- package/dist/pluralizer-BjMIc6uT.d.mts +42 -0
- package/dist/pluralizer-Cb6ZmrDl.d.cts +42 -0
- package/dist/pluralizer.cjs +678 -0
- package/dist/pluralizer.d.cts +152 -0
- package/dist/pluralizer.d.mts +152 -0
- package/dist/pluralizer.mjs +676 -0
- package/dist/primitives-B26uZolQ.cjs +228 -0
- package/dist/primitives-KsFUp3kQ.mjs +144 -0
- package/dist/specials-D48_IZbd.d.mts +108 -0
- package/dist/specials-DzLr1ZgU.cjs +477 -0
- package/dist/specials-LVONlKbQ.d.cts +108 -0
- package/dist/specials-uhDuRg8H.mjs +292 -0
- package/dist/string-CBAbxaG1.d.mts +258 -0
- package/dist/string-CsNsm_65.d.cts +258 -0
- package/dist/stylog.cjs +621 -0
- package/dist/stylog.d.cts +49 -0
- package/dist/stylog.d.mts +49 -0
- package/dist/stylog.mjs +614 -0
- package/dist/timezone-B2OYK6Fh.mjs +5589 -0
- package/dist/timezone-Beh9IGpw.cjs +5625 -0
- package/dist/types/array.cjs +16 -0
- package/dist/types/array.d.cts +18 -0
- package/dist/types/array.d.mts +18 -0
- package/dist/types/array.mjs +17 -0
- package/dist/types/colors.cjs +16 -0
- package/dist/types/colors.d.cts +18 -0
- package/dist/types/colors.d.mts +18 -0
- package/dist/types/colors.mjs +17 -0
- package/dist/types/converter.cjs +16 -0
- package/dist/types/converter.d.cts +18 -0
- package/dist/types/converter.d.mts +18 -0
- package/dist/types/converter.mjs +17 -0
- package/dist/types/form.cjs +16 -0
- package/dist/types/form.d.cts +18 -0
- package/dist/types/form.d.mts +18 -0
- package/dist/types/form.mjs +17 -0
- package/dist/types/hash.cjs +16 -0
- package/dist/types/hash.d.cts +18 -0
- package/dist/types/hash.d.mts +18 -0
- package/dist/types/hash.mjs +17 -0
- package/dist/types/http-status.cjs +16 -0
- package/dist/types/http-status.d.cts +18 -0
- package/dist/types/http-status.d.mts +18 -0
- package/dist/types/http-status.mjs +17 -0
- package/dist/types/index.cjs +16 -0
- package/dist/types/index.d.cts +18 -0
- package/dist/types/index.d.mts +18 -0
- package/dist/types/index.mjs +17 -0
- package/dist/types/number.cjs +16 -0
- package/dist/types/number.d.cts +18 -0
- package/dist/types/number.d.mts +18 -0
- package/dist/types/number.mjs +17 -0
- package/dist/types/object.cjs +16 -0
- package/dist/types/object.d.cts +18 -0
- package/dist/types/object.d.mts +18 -0
- package/dist/types/object.mjs +17 -0
- package/dist/types/pluralizer.cjs +16 -0
- package/dist/types/pluralizer.d.cts +18 -0
- package/dist/types/pluralizer.d.mts +18 -0
- package/dist/types/pluralizer.mjs +17 -0
- package/dist/types/string.cjs +16 -0
- package/dist/types/string.d.cts +18 -0
- package/dist/types/string.d.mts +18 -0
- package/dist/types/string.mjs +17 -0
- package/dist/types/stylog.cjs +16 -0
- package/dist/types/stylog.d.cts +18 -0
- package/dist/types/stylog.d.mts +18 -0
- package/dist/types/stylog.mjs +17 -0
- package/dist/types/utils.cjs +16 -0
- package/dist/types/utils.d.cts +18 -0
- package/dist/types/utils.d.mts +18 -0
- package/dist/types/utils.mjs +17 -0
- package/dist/types/verbalizer.cjs +16 -0
- package/dist/types/verbalizer.d.cts +30 -0
- package/dist/types/verbalizer.d.mts +30 -0
- package/dist/types/verbalizer.mjs +17 -0
- package/dist/utilities-CLUmdQeV.cjs +140 -0
- package/dist/utilities-m5yFKqLd.mjs +105 -0
- package/dist/utils-ClW9LA6f.mjs +449 -0
- package/dist/utils-DLFRgXUC.cjs +568 -0
- package/dist/verbalizer.cjs +998 -0
- package/dist/verbalizer.d.cts +148 -0
- package/dist/verbalizer.d.mts +148 -0
- package/dist/verbalizer.mjs +996 -0
- package/package.json +249 -0
package/dist/dom.cjs
ADDED
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 - present Nazmul Hassan
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
18
|
+
const require_primitives = require('./primitives-B26uZolQ.cjs');
|
|
19
|
+
const require_specials = require('./specials-DzLr1ZgU.cjs');
|
|
20
|
+
const require_guards = require('./guards-Efhp1mNy.cjs');
|
|
21
|
+
const require_objectify = require('./objectify-DIJ-OBmo.cjs');
|
|
22
|
+
const require_utils = require('./utils-DLFRgXUC.cjs');
|
|
23
|
+
|
|
24
|
+
//#region src/dom/query.ts
|
|
25
|
+
/**
|
|
26
|
+
* * Utility to generate query parameters from an object.
|
|
27
|
+
*
|
|
28
|
+
* @param params - Object containing query parameters.
|
|
29
|
+
* @returns A query string as a URL-encoded string, e.g., `?key1=value1&key2=value2`.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* generateQueryParams({ key1: 'value1', key2: 42 }); // "?key1=value1&key2=42"
|
|
33
|
+
* generateQueryParams({ key1: ['value1', 'value2'], key2: 42 }); // "?key1=value1&key1=value2&key2=42"
|
|
34
|
+
* generateQueryParams({ key1: '', key2: null }); // ""
|
|
35
|
+
* generateQueryParams({ key1: true, key2: false }); // "?key1=true&key2=false"
|
|
36
|
+
* generateQueryParams({ filters: { category: 'laptop', price: 1000 } }); // "?category=laptop&price=1000"
|
|
37
|
+
*/
|
|
38
|
+
function generateQueryParams(params = {}) {
|
|
39
|
+
const flattenedParams = require_objectify.flattenObjectKeyValue(params);
|
|
40
|
+
const queryParams = Object.entries(flattenedParams)?.filter(([_, value]) => value != null && !(require_primitives.isString(value) && value?.trim() === ""))?.flatMap(([key, value]) => Array.isArray(value) ? value?.filter((v) => v != null && !(require_primitives.isString(v) && v.trim() === ""))?.map((v) => `${encodeURIComponent(key)}=${encodeURIComponent(String(v))}`) : `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`).join("&");
|
|
41
|
+
return queryParams ? `?${queryParams}` : "";
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* * Get query params as standard `JavaScript` Object `Record<string, string>`. You can define the type by passing a type argument.
|
|
45
|
+
*
|
|
46
|
+
* - **Note:** *Extracts query parameters from the current URL (window.location.search).*
|
|
47
|
+
*
|
|
48
|
+
* @returns Query string as key-value paired object. `Record<string, string>`.
|
|
49
|
+
*/
|
|
50
|
+
function getQueryParams() {
|
|
51
|
+
return Object.fromEntries(new URLSearchParams(window?.location?.search));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* * Update query params in the browser URL with given key and value.
|
|
55
|
+
* @param key Key for the query to update.
|
|
56
|
+
* @param value Value to updated against the given key.
|
|
57
|
+
*/
|
|
58
|
+
function updateQueryParam(key, value) {
|
|
59
|
+
const url = new URL(window.location.href);
|
|
60
|
+
url.searchParams.set(key, value);
|
|
61
|
+
window.history.replaceState({}, "", url?.toString());
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Parses a query string (with optional `?` prefix) into an object.
|
|
65
|
+
* Supports multiple values for the same key by returning arrays.
|
|
66
|
+
* Optionally parses primitive string values into actual types (e.g., "1" → 1, "true" → true).
|
|
67
|
+
*
|
|
68
|
+
* @remarks This utility is designed to parse generic string, for literal use, try {@link parseQueryStringLiteral}.
|
|
69
|
+
*
|
|
70
|
+
* - **Note:** *This function does **not** access or depend on `current URL` a.k.a `window.location.search`.*
|
|
71
|
+
*
|
|
72
|
+
* @param query - The query string to parse.
|
|
73
|
+
* @param parsePrimitives - Whether to convert stringified primitives into real values (default: true).
|
|
74
|
+
* @returns An object where keys are strings and values can be string, array, number, boolean, or null/undefined.
|
|
75
|
+
*/
|
|
76
|
+
function parseQueryString(query, parsePrimitives = true) {
|
|
77
|
+
const params = new URLSearchParams(query.startsWith("?") ? query.slice(1) : query);
|
|
78
|
+
const entries = {};
|
|
79
|
+
for (const [key, value] of params.entries()) if (key in entries) {
|
|
80
|
+
const current = entries[key];
|
|
81
|
+
const array = Array.isArray(current) ? [...current, value] : [current, value];
|
|
82
|
+
entries[key] = parsePrimitives ? require_utils.deepParsePrimitives(array) : array;
|
|
83
|
+
} else entries[key] = value;
|
|
84
|
+
return parsePrimitives ? require_objectify.parseObjectValues(entries) : entries;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Parses a query string (with optional `?` prefix) into an object.
|
|
88
|
+
* Supports multiple values for the same key by returning arrays.
|
|
89
|
+
* It returns properly typed object.
|
|
90
|
+
*
|
|
91
|
+
* @remarks This utility is designed to parse literal string, for generic use, try {@link parseQueryString}.
|
|
92
|
+
*
|
|
93
|
+
* - **Note:** *This function does **not** access or depend on `current URL` a.k.a `window.location.search`.*
|
|
94
|
+
*
|
|
95
|
+
* @param query - The literal query string to parse.
|
|
96
|
+
* @returns An object where keys are strings and values can be string, array, or null/undefined.
|
|
97
|
+
*/
|
|
98
|
+
function parseQueryStringLiteral(query) {
|
|
99
|
+
const params = new URLSearchParams(query.startsWith("?") ? query.slice(1) : query);
|
|
100
|
+
const entries = {};
|
|
101
|
+
for (const [key, value] of params.entries()) if (key in entries) {
|
|
102
|
+
const current = entries[key];
|
|
103
|
+
entries[key] = Array.isArray(current) ? [...current, value] : [current, value];
|
|
104
|
+
} else entries[key] = value;
|
|
105
|
+
return entries;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region src/dom/storage.ts
|
|
110
|
+
/**
|
|
111
|
+
* * Get item from local storage.
|
|
112
|
+
*
|
|
113
|
+
* @param key - Key to get item from local storage.
|
|
114
|
+
* @param deserialize - Optional deserializer function to convert the stored value back to type `T`. Defaults to `JSON.parse`.
|
|
115
|
+
* @returns Returns saved item from local storage if it exists with that key.
|
|
116
|
+
*/
|
|
117
|
+
function getFromLocalStorage(key, deserialize) {
|
|
118
|
+
const deserializer = deserialize ?? JSON.parse;
|
|
119
|
+
try {
|
|
120
|
+
const item = localStorage.getItem(key);
|
|
121
|
+
return item ? deserializer(item) : null;
|
|
122
|
+
} catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* * Save item in local storage.
|
|
128
|
+
*
|
|
129
|
+
* @param key - Key to save an item.
|
|
130
|
+
* @param value - The item/value to save.
|
|
131
|
+
* @param serialize - Optional serializer function to convert the value of type `T` to a string. Defaults to `JSON.stringify`.
|
|
132
|
+
*/
|
|
133
|
+
function saveToLocalStorage(key, value, serialize) {
|
|
134
|
+
const serializer = serialize ?? JSON.stringify;
|
|
135
|
+
try {
|
|
136
|
+
localStorage.setItem(key, serializer(value));
|
|
137
|
+
} catch (error) {
|
|
138
|
+
console.error(`Error saving item with key "${key}" in local storage:`, error);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* * Remove item from local storage.
|
|
143
|
+
*
|
|
144
|
+
* @param key - Key to delete item from local storage.
|
|
145
|
+
*/
|
|
146
|
+
function removeFromLocalStorage(key) {
|
|
147
|
+
try {
|
|
148
|
+
localStorage.removeItem(key);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
console.error(`Error removing item with key "${key}" from local storage:`, error);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* * Get item from session storage.
|
|
155
|
+
*
|
|
156
|
+
* @param key - Key to get item from session storage.
|
|
157
|
+
* @param deserialize - Optional deserializer function to convert the stored value back to type `T`. Defaults to `JSON.parse`.
|
|
158
|
+
* @returns Returns saved item from session storage if it exists with that key.
|
|
159
|
+
*/
|
|
160
|
+
function getFromSessionStorage(key, deserialize) {
|
|
161
|
+
const deserializer = deserialize ?? JSON.parse;
|
|
162
|
+
try {
|
|
163
|
+
const item = sessionStorage.getItem(key);
|
|
164
|
+
return item ? deserializer(item) : null;
|
|
165
|
+
} catch {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* * Save item in session storage.
|
|
171
|
+
*
|
|
172
|
+
* @param key - Key to save an item.
|
|
173
|
+
* @param value - The item/value to save.
|
|
174
|
+
* @param serialize - Optional serializer function to convert the value of type `T` to a string. Defaults to `JSON.stringify`.
|
|
175
|
+
*/
|
|
176
|
+
function saveToSessionStorage(key, value, serialize) {
|
|
177
|
+
const serializer = serialize ?? JSON.stringify;
|
|
178
|
+
try {
|
|
179
|
+
sessionStorage.setItem(key, serializer(value));
|
|
180
|
+
} catch (error) {
|
|
181
|
+
console.error(`Error saving item with key "${key}" in session storage:`, error);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* * Remove item from session storage.
|
|
186
|
+
*
|
|
187
|
+
* @param key - Key to delete item from session storage.
|
|
188
|
+
*/
|
|
189
|
+
function removeFromSessionStorage(key) {
|
|
190
|
+
try {
|
|
191
|
+
sessionStorage.removeItem(key);
|
|
192
|
+
} catch (error) {
|
|
193
|
+
console.error(`Error removing item with key "${key}" from session storage:`, error);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
//#endregion
|
|
198
|
+
//#region src/dom/utils.ts
|
|
199
|
+
/**
|
|
200
|
+
* * Scrolls smoothly to the given element with an optional vertical offset.
|
|
201
|
+
* @param element The target element to scroll to.
|
|
202
|
+
* @param offset Additional vertical offset in pixels (positive moves down, negative moves up).
|
|
203
|
+
*/
|
|
204
|
+
function smoothScrollTo(element, offset = 0) {
|
|
205
|
+
element.scrollIntoView({
|
|
206
|
+
behavior: "smooth",
|
|
207
|
+
block: "start"
|
|
208
|
+
});
|
|
209
|
+
if (offset !== 0) setTimeout(() => {
|
|
210
|
+
window.scrollBy({
|
|
211
|
+
top: offset,
|
|
212
|
+
behavior: "smooth"
|
|
213
|
+
});
|
|
214
|
+
}, 300);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* * Toggles full-screen mode for a given element (or the `document` by default).
|
|
218
|
+
* @param element The element to toggle fullscreen mode for (default: document root).
|
|
219
|
+
*/
|
|
220
|
+
function toggleFullScreen(element = document.documentElement) {
|
|
221
|
+
const doc = document;
|
|
222
|
+
const elem = element;
|
|
223
|
+
if (!doc.fullscreenElement && !doc.webkitFullscreenElement) {
|
|
224
|
+
if (elem.requestFullscreen) elem.requestFullscreen();
|
|
225
|
+
else if (elem.webkitRequestFullscreen) elem.webkitRequestFullscreen();
|
|
226
|
+
} else if (doc.exitFullscreen) doc.exitFullscreen();
|
|
227
|
+
else if (doc.webkitExitFullscreen) doc.webkitExitFullscreen();
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* * Copies text to the clipboard, falling back to legacy methods if needed.
|
|
231
|
+
* @param text - The text to copy.
|
|
232
|
+
* @returns A promise that resolves when the text is copied.
|
|
233
|
+
*/
|
|
234
|
+
async function copyToClipboard(text) {
|
|
235
|
+
try {
|
|
236
|
+
if (navigator?.clipboard?.writeText) await navigator.clipboard.writeText(text);
|
|
237
|
+
else {
|
|
238
|
+
const textArea = document.createElement("textarea");
|
|
239
|
+
textArea.value = text;
|
|
240
|
+
textArea.style.position = "fixed";
|
|
241
|
+
textArea.style.opacity = "0";
|
|
242
|
+
document.body.appendChild(textArea);
|
|
243
|
+
textArea.select();
|
|
244
|
+
textArea.setSelectionRange(0, textArea.value?.length);
|
|
245
|
+
const success = document.execCommand("copy");
|
|
246
|
+
document.body.removeChild(textArea);
|
|
247
|
+
if (!success) throw new Error("Cannot execute command in this environment!");
|
|
248
|
+
}
|
|
249
|
+
} catch (error) {
|
|
250
|
+
console.error("Failed to copy text:", error);
|
|
251
|
+
throw error;
|
|
252
|
+
} finally {
|
|
253
|
+
const textArea = document.querySelector("textarea[style*=\"fixed\"]");
|
|
254
|
+
if (textArea) document.body.removeChild(textArea);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
//#endregion
|
|
259
|
+
//#region src/form/convert.ts
|
|
260
|
+
/**
|
|
261
|
+
* * Utility to convert object into FormData in a controlled way.
|
|
262
|
+
* - **N.B.** Be cautious when using this in SSR (Server-Side Rendering) environments (such as `Next.js` Server Components), as it may not work as expected.
|
|
263
|
+
*
|
|
264
|
+
* @param data - The source object to control and convert to FormData.
|
|
265
|
+
* @param configs - Configuration options to control the formData.
|
|
266
|
+
* @returns `FormData` instance containing the sanitized and transformed data.
|
|
267
|
+
*/
|
|
268
|
+
const createControlledFormData = (data, configs) => {
|
|
269
|
+
const formData = new FormData();
|
|
270
|
+
const { stringifyNested = "*" } = configs || {};
|
|
271
|
+
/** - Helper to check if a key should be lowercase */
|
|
272
|
+
const _shouldLowercaseKeys = (key) => {
|
|
273
|
+
return Array.isArray(configs?.lowerCaseKeys) ? configs?.lowerCaseKeys?.some((path) => key === path || key.startsWith(`${path}.`)) : configs?.lowerCaseKeys === "*";
|
|
274
|
+
};
|
|
275
|
+
/** - Helper to check if a key should be lowercase */
|
|
276
|
+
const _shouldLowercaseValue = (key) => {
|
|
277
|
+
return Array.isArray(configs?.lowerCaseValues) ? configs.lowerCaseValues?.some((path) => key === path || key?.startsWith(`${path}.`)) : configs?.lowerCaseValues === "*";
|
|
278
|
+
};
|
|
279
|
+
/** - Transforms key to lowercase if needed */
|
|
280
|
+
const _transformKey = (key) => {
|
|
281
|
+
return _shouldLowercaseKeys(key) ? key.toLowerCase() : key;
|
|
282
|
+
};
|
|
283
|
+
/** - Helper function to check if a key matches a breakArray key. */
|
|
284
|
+
const _isRequiredKey = (key) => {
|
|
285
|
+
const transformedKey = _transformKey(key);
|
|
286
|
+
return Array.isArray(configs?.requiredKeys) ? configs?.requiredKeys?.some((path) => transformedKey === path || transformedKey?.startsWith(`${path}.`)) : configs?.requiredKeys === "*";
|
|
287
|
+
};
|
|
288
|
+
/** - Helper function to check if a key matches a dotNotation path to preserve. */
|
|
289
|
+
const _shouldDotNotate = (key) => {
|
|
290
|
+
const transformedKey = _transformKey(key);
|
|
291
|
+
return Array.isArray(configs?.dotNotateNested) ? configs.dotNotateNested.includes(transformedKey) : configs?.dotNotateNested === "*";
|
|
292
|
+
};
|
|
293
|
+
/** - Helper function to check if a key matches a stringifyNested key. */
|
|
294
|
+
const _shouldStringify = (key) => {
|
|
295
|
+
const transformedKey = _transformKey(key);
|
|
296
|
+
return Array.isArray(stringifyNested) ? stringifyNested.includes(transformedKey) : stringifyNested === "*";
|
|
297
|
+
};
|
|
298
|
+
/** - Helper function to check if a key matches a breakArray key. */
|
|
299
|
+
const _shouldBreakArray = (key) => {
|
|
300
|
+
const transformedKey = _transformKey(key);
|
|
301
|
+
return Array.isArray(configs?.breakArray) ? configs.breakArray.includes(transformedKey) : configs?.breakArray === "*";
|
|
302
|
+
};
|
|
303
|
+
/** - Helper to clean object by removing null/undefined/empty values while respecting required keys */
|
|
304
|
+
const _cleanObject = (obj, parentKey = "") => {
|
|
305
|
+
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
306
|
+
const transformedKey = _transformKey(key);
|
|
307
|
+
const fullKey = parentKey ? `${parentKey}.${transformedKey}` : transformedKey;
|
|
308
|
+
if (configs?.ignoreKeys?.includes(fullKey)) return acc;
|
|
309
|
+
if (value != null && value !== "" || _isRequiredKey(fullKey) || require_primitives.isNonEmptyString(value) || require_specials.isValidArray(value) || require_specials.isNotEmptyObject(value)) if (require_specials.isNotEmptyObject(value)) if (require_guards.isDateLike(value)) acc[transformedKey] = value;
|
|
310
|
+
else {
|
|
311
|
+
const cleaned = _cleanObject(value, fullKey);
|
|
312
|
+
if (_isRequiredKey(fullKey) || require_specials.isNotEmptyObject(cleaned)) acc[transformedKey] = cleaned;
|
|
313
|
+
}
|
|
314
|
+
else if (require_primitives.isString(value)) if (require_primitives.isNonEmptyString(value)) {
|
|
315
|
+
let cleanString = value;
|
|
316
|
+
if (configs?.trimStrings) cleanString = cleanString?.trim();
|
|
317
|
+
if (_shouldLowercaseValue(fullKey)) cleanString = cleanString?.toLowerCase();
|
|
318
|
+
acc[transformedKey] = cleanString;
|
|
319
|
+
} else acc[transformedKey] = value;
|
|
320
|
+
else if (Array.isArray(value)) {
|
|
321
|
+
if (_isRequiredKey(fullKey) || require_specials.isValidArray(value)) acc[transformedKey] = value;
|
|
322
|
+
} else if (require_specials.isEmptyObject(value)) {
|
|
323
|
+
if (_isRequiredKey(fullKey)) acc[transformedKey] = value;
|
|
324
|
+
} else acc[transformedKey] = value;
|
|
325
|
+
return acc;
|
|
326
|
+
}, {});
|
|
327
|
+
};
|
|
328
|
+
/** * Helper function to add values to formData */
|
|
329
|
+
const _addToFormData = (key, value) => {
|
|
330
|
+
const transformedKey = _transformKey(key);
|
|
331
|
+
if (require_objectify.isCustomFileArray(value)) value?.forEach((file) => formData.append(transformedKey, file?.originFileObj));
|
|
332
|
+
else if (require_objectify.isFileUpload(value)) {
|
|
333
|
+
if (value?.fileList) value?.fileList.forEach((file) => formData.append(transformedKey, file?.originFileObj));
|
|
334
|
+
else if (value?.file) if (require_objectify.isCustomFile(value?.file)) formData.append(transformedKey, value?.file?.originFileObj);
|
|
335
|
+
else formData.append(transformedKey, value?.file);
|
|
336
|
+
} else if (require_objectify.isFileOrBlob(value)) formData.append(transformedKey, value);
|
|
337
|
+
else if (require_objectify.isFileList(value)) for (let i = 0; i < value?.length; i++) formData.append(transformedKey, value.item(i));
|
|
338
|
+
else if (Array.isArray(value)) {
|
|
339
|
+
if (require_objectify.isFileArray(value)) if (_shouldBreakArray(key)) value?.forEach((item, index) => {
|
|
340
|
+
_addToFormData(`${transformedKey}[${index}]`, item);
|
|
341
|
+
});
|
|
342
|
+
else value?.forEach((item) => {
|
|
343
|
+
formData.append(transformedKey, item);
|
|
344
|
+
});
|
|
345
|
+
else if (require_specials.isValidArray(value)) if (_shouldBreakArray(key)) value?.forEach((item, index) => {
|
|
346
|
+
_addToFormData(`${transformedKey}[${index}]`, item);
|
|
347
|
+
});
|
|
348
|
+
else formData.append(transformedKey, JSON.stringify(value));
|
|
349
|
+
else if (_isRequiredKey(key)) formData.append(transformedKey, JSON.stringify(value));
|
|
350
|
+
} else if (require_guards.isDateLike(value)) formData.append(transformedKey, JSON.parse(JSON.stringify(value)));
|
|
351
|
+
else if (require_specials.isNotEmptyObject(value)) if (_shouldStringify(key) && !_shouldDotNotate(key)) {
|
|
352
|
+
const cleanedValue = _cleanObject(value, key);
|
|
353
|
+
if (require_specials.isNotEmptyObject(cleanedValue) || _isRequiredKey(key)) formData.append(transformedKey, JSON.stringify(cleanedValue));
|
|
354
|
+
} else Object.entries(value).forEach(([nestedKey, nestedValue]) => {
|
|
355
|
+
_addToFormData(`${key}.${nestedKey}`, nestedValue);
|
|
356
|
+
});
|
|
357
|
+
else if (value != null && value !== "" || _isRequiredKey(key)) if (require_primitives.isString(value) && _shouldLowercaseValue(key)) formData.append(transformedKey, value?.toLowerCase());
|
|
358
|
+
else formData.append(transformedKey, value);
|
|
359
|
+
};
|
|
360
|
+
/** - Helper to process object */
|
|
361
|
+
const _processObject = (obj, parentKey = "") => {
|
|
362
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
363
|
+
const transformedKey = _transformKey(key);
|
|
364
|
+
const fullKey = parentKey ? `${parentKey}.${transformedKey}` : transformedKey;
|
|
365
|
+
if (configs?.ignoreKeys?.includes(fullKey)) return;
|
|
366
|
+
if (configs?.trimStrings && require_primitives.isNonEmptyString(value)) value = value?.trim();
|
|
367
|
+
if (_shouldDotNotate(fullKey)) _addToFormData(fullKey, value);
|
|
368
|
+
else if (require_specials.isNotEmptyObject(value) && !_shouldStringify(fullKey)) if (require_guards.isDateLike(value)) _addToFormData(key, JSON.parse(JSON.stringify(value)));
|
|
369
|
+
else _processObject(value, key);
|
|
370
|
+
else if (require_objectify.isFileOrBlob(value)) _addToFormData(key, value);
|
|
371
|
+
else if (require_guards.isDateLike(value)) _addToFormData(key, JSON.parse(JSON.stringify(value)));
|
|
372
|
+
else if (require_specials.isEmptyObject(value)) {
|
|
373
|
+
if (_isRequiredKey(fullKey)) _addToFormData(key, JSON.stringify(value));
|
|
374
|
+
} else _addToFormData(key, value);
|
|
375
|
+
});
|
|
376
|
+
};
|
|
377
|
+
if (require_specials.isNotEmptyObject(data)) _processObject(data);
|
|
378
|
+
return formData;
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
//#endregion
|
|
382
|
+
//#region src/form/transform.ts
|
|
383
|
+
/**
|
|
384
|
+
* * Serialize form data into an object or a query string.
|
|
385
|
+
* - **N.B.** Be cautious when using this in SSR (Server-Side Rendering) environments (such as `Next.js` Server Components), as it may not work as expected.
|
|
386
|
+
*
|
|
387
|
+
* @param form - The form element to serialize.
|
|
388
|
+
* @param toQueryString - Whether to return the result as a query string. If false, returns an object.
|
|
389
|
+
* @returns The serialized form data as an object or query string.
|
|
390
|
+
*/
|
|
391
|
+
function serializeForm(form, toQueryString = false) {
|
|
392
|
+
const formData = new FormData(form);
|
|
393
|
+
const data = {};
|
|
394
|
+
formData?.forEach((value, key) => {
|
|
395
|
+
if (data[key]) data[key] = Array.isArray(data[key]) ? [...data[key], value.toString()] : [data[key], value.toString()];
|
|
396
|
+
else data[key] = value.toString();
|
|
397
|
+
});
|
|
398
|
+
if (toQueryString) return generateQueryParams(data);
|
|
399
|
+
return data;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* * Parse form data from a `FormData` object or query string into a structured object format.
|
|
403
|
+
*
|
|
404
|
+
* @param data - The `FormData` object or query string to parse.
|
|
405
|
+
* @param parsePrimitives - Whether to parse string values into primitive types (e.g., boolean, number, array, object). Defaults to `true`.
|
|
406
|
+
* @returns The parsed form data as an object.
|
|
407
|
+
*/
|
|
408
|
+
function parseFormData(data, parsePrimitives = true) {
|
|
409
|
+
const parsed = {};
|
|
410
|
+
if (require_primitives.isString(data)) new URLSearchParams(data)?.forEach((value, key) => {
|
|
411
|
+
const existing = parsed[key];
|
|
412
|
+
if (require_primitives.isString(existing)) parsed[key] = [existing, value];
|
|
413
|
+
else if (Array.isArray(existing)) parsed[key] = [...existing, value];
|
|
414
|
+
else parsed[key] = value;
|
|
415
|
+
});
|
|
416
|
+
else data?.forEach((value, key) => {
|
|
417
|
+
const existing = parsed[key];
|
|
418
|
+
if (value instanceof File) if (Array.isArray(existing)) parsed[key] = [...existing, value];
|
|
419
|
+
else if (existing instanceof File) parsed[key] = [existing, value];
|
|
420
|
+
else parsed[key] = value;
|
|
421
|
+
else if (require_primitives.isString(existing)) parsed[key] = [existing, value];
|
|
422
|
+
else if (Array.isArray(existing)) parsed[key] = [...existing, value];
|
|
423
|
+
else parsed[key] = value;
|
|
424
|
+
});
|
|
425
|
+
if (parsePrimitives) return require_objectify.parseObjectValues(parsed);
|
|
426
|
+
else return parsed;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
//#endregion
|
|
430
|
+
exports.convertIntoFormData = createControlledFormData;
|
|
431
|
+
exports.copyToClipboard = copyToClipboard;
|
|
432
|
+
exports.createControlledFormData = createControlledFormData;
|
|
433
|
+
exports.createFormData = createControlledFormData;
|
|
434
|
+
exports.createQueryParams = generateQueryParams;
|
|
435
|
+
exports.formatQueryParams = generateQueryParams;
|
|
436
|
+
exports.generateQueryParams = generateQueryParams;
|
|
437
|
+
exports.getFromLocalStorage = getFromLocalStorage;
|
|
438
|
+
exports.getFromSessionStorage = getFromSessionStorage;
|
|
439
|
+
exports.getQueryParams = getQueryParams;
|
|
440
|
+
exports.getQueryStringAsObject = parseQueryString;
|
|
441
|
+
exports.isCustomFile = require_objectify.isCustomFile;
|
|
442
|
+
exports.isCustomFileArray = require_objectify.isCustomFileArray;
|
|
443
|
+
exports.isFileArray = require_objectify.isFileArray;
|
|
444
|
+
exports.isFileList = require_objectify.isFileList;
|
|
445
|
+
exports.isFileOrBlob = require_objectify.isFileOrBlob;
|
|
446
|
+
exports.isFileUpload = require_objectify.isFileUpload;
|
|
447
|
+
exports.isOriginFileObj = require_objectify.isOriginFileObj;
|
|
448
|
+
exports.isValidFormData = require_objectify.isValidFormData;
|
|
449
|
+
exports.literalQueryStringToObject = parseQueryStringLiteral;
|
|
450
|
+
exports.parseFormData = parseFormData;
|
|
451
|
+
exports.parseQueryString = parseQueryString;
|
|
452
|
+
exports.parseQueryStringLiteral = parseQueryStringLiteral;
|
|
453
|
+
exports.queryStringToObject = parseQueryString;
|
|
454
|
+
exports.removeFromLocalStorage = removeFromLocalStorage;
|
|
455
|
+
exports.removeFromSessionStorage = removeFromSessionStorage;
|
|
456
|
+
exports.saveToLocalStorage = saveToLocalStorage;
|
|
457
|
+
exports.saveToSessionStorage = saveToSessionStorage;
|
|
458
|
+
exports.serializeForm = serializeForm;
|
|
459
|
+
exports.smoothScrollTo = smoothScrollTo;
|
|
460
|
+
exports.toggleFullScreen = toggleFullScreen;
|
|
461
|
+
exports.updateQueryParam = updateQueryParam;
|