@alibarbar/common 1.0.9 → 1.0.10
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/{chunk-O3O67R4I.js → algorithm.cjs} +0 -2
- package/dist/algorithm.js +130 -44
- package/dist/{chunk-LOAZSPGQ.js → array.cjs} +0 -2
- package/dist/array.js +135 -84
- package/dist/{chunk-Y364QIQH.js → color.cjs} +0 -2
- package/dist/color.js +127 -40
- package/dist/{chunk-DYBSRI7V.js → crypto.cjs} +0 -2
- package/dist/crypto.js +172 -60
- package/dist/{chunk-BHCRFURU.js → data-structure.cjs} +0 -2
- package/dist/data-structure.js +481 -32
- package/dist/{chunk-I3L42475.js → date.cjs} +0 -2
- package/dist/date.js +125 -72
- package/dist/{chunk-JK2SE3I2.js → dom.cjs} +0 -2
- package/dist/dom.js +84 -56
- package/dist/{chunk-7V5UQXIO.js → file.cjs} +0 -2
- package/dist/file.js +79 -32
- package/dist/{chunk-OX5PLOWB.js → i18n.cjs} +0 -2
- package/dist/i18n.js +78 -40
- package/dist/index.cjs +4132 -0
- package/dist/index.d.mts +3 -159
- package/dist/index.d.ts +3 -159
- package/dist/index.js +3364 -17427
- package/dist/{chunk-4RGXV4SJ.js → network.cjs} +0 -2
- package/dist/network.js +97 -28
- package/dist/{chunk-3FRYJPHG.js → number.cjs} +0 -2
- package/dist/number.js +77 -56
- package/dist/{chunk-27UDDVLZ.js → object.cjs} +0 -2
- package/dist/object.js +237 -80
- package/dist/{chunk-JQZBPAPO.js → performance.cjs} +0 -2
- package/dist/performance.js +145 -40
- package/dist/services.cjs +291 -0
- package/dist/services.d.mts +161 -0
- package/dist/services.d.ts +161 -0
- package/dist/services.js +281 -0
- package/dist/{chunk-WZDOPUJW.js → storage.cjs} +216 -17
- package/dist/storage.js +550 -31
- package/dist/{chunk-D7CS5EKF.js → string.cjs} +0 -2
- package/dist/string.js +91 -68
- package/dist/{chunk-56W6YECK.js → tracking.cjs} +0 -2
- package/dist/tracking.js +359 -52
- package/dist/{chunk-ZDMFMUDR.js → transform.cjs} +0 -2
- package/dist/transform.js +299 -32
- package/dist/{chunk-NJARVI6X.mjs → upload.cjs} +41 -14
- package/dist/upload.js +402 -20
- package/dist/{chunk-KGFTD255.js → url.cjs} +0 -2
- package/dist/url.js +91 -44
- package/dist/{chunk-TQN37HIN.js → validation.cjs} +0 -2
- package/dist/validation.js +77 -60
- package/package.json +7 -2
- package/dist/algorithm.js.map +0 -1
- package/dist/algorithm.mjs +0 -4
- package/dist/algorithm.mjs.map +0 -1
- package/dist/array.js.map +0 -1
- package/dist/array.mjs +0 -4
- package/dist/array.mjs.map +0 -1
- package/dist/chunk-27UDDVLZ.js.map +0 -1
- package/dist/chunk-2R2QWFJC.mjs +0 -138
- package/dist/chunk-2R2QWFJC.mjs.map +0 -1
- package/dist/chunk-3FRYJPHG.js.map +0 -1
- package/dist/chunk-4RGXV4SJ.js.map +0 -1
- package/dist/chunk-56W6YECK.js.map +0 -1
- package/dist/chunk-5BGSUGTI.mjs +0 -128
- package/dist/chunk-5BGSUGTI.mjs.map +0 -1
- package/dist/chunk-7E6GELHJ.mjs +0 -302
- package/dist/chunk-7E6GELHJ.mjs.map +0 -1
- package/dist/chunk-7V5UQXIO.js.map +0 -1
- package/dist/chunk-A4SWQXX7.mjs +0 -484
- package/dist/chunk-A4SWQXX7.mjs.map +0 -1
- package/dist/chunk-BHCRFURU.js.map +0 -1
- package/dist/chunk-CDSGEAOK.mjs +0 -80
- package/dist/chunk-CDSGEAOK.mjs.map +0 -1
- package/dist/chunk-D7CS5EKF.js.map +0 -1
- package/dist/chunk-DYBSRI7V.js.map +0 -1
- package/dist/chunk-FEBKPX5A.js +0 -386
- package/dist/chunk-FEBKPX5A.js.map +0 -1
- package/dist/chunk-FJ6ZGZIA.mjs +0 -39
- package/dist/chunk-FJ6ZGZIA.mjs.map +0 -1
- package/dist/chunk-HLDFI7R2.mjs +0 -175
- package/dist/chunk-HLDFI7R2.mjs.map +0 -1
- package/dist/chunk-I3L42475.js.map +0 -1
- package/dist/chunk-JBLX27WD.mjs +0 -240
- package/dist/chunk-JBLX27WD.mjs.map +0 -1
- package/dist/chunk-JHZ7M2MR.mjs +0 -133
- package/dist/chunk-JHZ7M2MR.mjs.map +0 -1
- package/dist/chunk-JK2SE3I2.js.map +0 -1
- package/dist/chunk-JQZBPAPO.js.map +0 -1
- package/dist/chunk-JXYGC2C5.mjs +0 -100
- package/dist/chunk-JXYGC2C5.mjs.map +0 -1
- package/dist/chunk-KGFTD255.js.map +0 -1
- package/dist/chunk-LBHBNPNJ.mjs +0 -148
- package/dist/chunk-LBHBNPNJ.mjs.map +0 -1
- package/dist/chunk-LF4CILQS.mjs +0 -87
- package/dist/chunk-LF4CILQS.mjs.map +0 -1
- package/dist/chunk-LOAZSPGQ.js.map +0 -1
- package/dist/chunk-NJARVI6X.mjs.map +0 -1
- package/dist/chunk-NSSDYX2U.mjs +0 -80
- package/dist/chunk-NSSDYX2U.mjs.map +0 -1
- package/dist/chunk-O3O67R4I.js.map +0 -1
- package/dist/chunk-OIXQ3E6W.mjs +0 -354
- package/dist/chunk-OIXQ3E6W.mjs.map +0 -1
- package/dist/chunk-OX5PLOWB.js.map +0 -1
- package/dist/chunk-PJ7UCTX4.mjs +0 -362
- package/dist/chunk-PJ7UCTX4.mjs.map +0 -1
- package/dist/chunk-PR4QN5HX.js +0 -44
- package/dist/chunk-PR4QN5HX.js.map +0 -1
- package/dist/chunk-QIBE7GVN.mjs +0 -81
- package/dist/chunk-QIBE7GVN.mjs.map +0 -1
- package/dist/chunk-QIOC54LQ.mjs +0 -130
- package/dist/chunk-QIOC54LQ.mjs.map +0 -1
- package/dist/chunk-TQN37HIN.js.map +0 -1
- package/dist/chunk-WZDOPUJW.js.map +0 -1
- package/dist/chunk-XJTZDXSR.mjs +0 -94
- package/dist/chunk-XJTZDXSR.mjs.map +0 -1
- package/dist/chunk-Y364QIQH.js.map +0 -1
- package/dist/chunk-YXM6Q4JS.mjs +0 -94
- package/dist/chunk-YXM6Q4JS.mjs.map +0 -1
- package/dist/chunk-ZDMFMUDR.js.map +0 -1
- package/dist/chunk-ZVJ6NQUM.mjs +0 -82
- package/dist/chunk-ZVJ6NQUM.mjs.map +0 -1
- package/dist/color.js.map +0 -1
- package/dist/color.mjs +0 -4
- package/dist/color.mjs.map +0 -1
- package/dist/crypto.js.map +0 -1
- package/dist/crypto.mjs +0 -4
- package/dist/crypto.mjs.map +0 -1
- package/dist/data-structure.js.map +0 -1
- package/dist/data-structure.mjs +0 -4
- package/dist/data-structure.mjs.map +0 -1
- package/dist/date.js.map +0 -1
- package/dist/date.mjs +0 -4
- package/dist/date.mjs.map +0 -1
- package/dist/dom.js.map +0 -1
- package/dist/dom.mjs +0 -4
- package/dist/dom.mjs.map +0 -1
- package/dist/file.js.map +0 -1
- package/dist/file.mjs +0 -4
- package/dist/file.mjs.map +0 -1
- package/dist/i18n.js.map +0 -1
- package/dist/i18n.mjs +0 -4
- package/dist/i18n.mjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -17155
- package/dist/index.mjs.map +0 -1
- package/dist/network.js.map +0 -1
- package/dist/network.mjs +0 -4
- package/dist/network.mjs.map +0 -1
- package/dist/number.js.map +0 -1
- package/dist/number.mjs +0 -4
- package/dist/number.mjs.map +0 -1
- package/dist/object.js.map +0 -1
- package/dist/object.mjs +0 -4
- package/dist/object.mjs.map +0 -1
- package/dist/performance.js.map +0 -1
- package/dist/performance.mjs +0 -4
- package/dist/performance.mjs.map +0 -1
- package/dist/storage.js.map +0 -1
- package/dist/storage.mjs +0 -5
- package/dist/storage.mjs.map +0 -1
- package/dist/string.js.map +0 -1
- package/dist/string.mjs +0 -4
- package/dist/string.mjs.map +0 -1
- package/dist/tracking.js.map +0 -1
- package/dist/tracking.mjs +0 -4
- package/dist/tracking.mjs.map +0 -1
- package/dist/transform.js.map +0 -1
- package/dist/transform.mjs +0 -4
- package/dist/transform.mjs.map +0 -1
- package/dist/upload.js.map +0 -1
- package/dist/upload.mjs +0 -5
- package/dist/upload.mjs.map +0 -1
- package/dist/url.js.map +0 -1
- package/dist/url.mjs +0 -4
- package/dist/url.mjs.map +0 -1
- package/dist/validation.js.map +0 -1
- package/dist/validation.mjs +0 -4
- package/dist/validation.mjs.map +0 -1
package/dist/network.js
CHANGED
|
@@ -1,29 +1,98 @@
|
|
|
1
|
-
|
|
1
|
+
// src/browser/network.ts
|
|
2
|
+
async function fetchWithRetry(url, options = {}, retryCount = 3, retryDelay = 1e3) {
|
|
3
|
+
let lastError = null;
|
|
4
|
+
for (let i = 0; i <= retryCount; i++) {
|
|
5
|
+
try {
|
|
6
|
+
const response = await fetch(url, options);
|
|
7
|
+
if (response.ok) {
|
|
8
|
+
return response;
|
|
9
|
+
}
|
|
10
|
+
if (i < retryCount) {
|
|
11
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
return response;
|
|
15
|
+
} catch (error) {
|
|
16
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
17
|
+
if (i < retryCount) {
|
|
18
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
throw lastError || new Error("Fetch failed after retries");
|
|
23
|
+
}
|
|
24
|
+
async function fetchWithTimeout(url, options = {}, timeout = 5e3) {
|
|
25
|
+
const controller = new AbortController();
|
|
26
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
27
|
+
try {
|
|
28
|
+
const response = await fetch(url, {
|
|
29
|
+
...options,
|
|
30
|
+
signal: controller.signal
|
|
31
|
+
});
|
|
32
|
+
clearTimeout(timeoutId);
|
|
33
|
+
return response;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
clearTimeout(timeoutId);
|
|
36
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
37
|
+
throw new Error(`Request timeout after ${timeout}ms`);
|
|
38
|
+
}
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function downloadFile(url, filename) {
|
|
43
|
+
if (typeof window === "undefined") {
|
|
44
|
+
throw new Error("downloadFile is only available in browser environment");
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
const response = await fetch(url);
|
|
48
|
+
if (!response.ok) {
|
|
49
|
+
throw new Error(`Failed to download file: ${response.statusText}`);
|
|
50
|
+
}
|
|
51
|
+
const blob = await response.blob();
|
|
52
|
+
const downloadUrl = window.URL.createObjectURL(blob);
|
|
53
|
+
const link = document.createElement("a");
|
|
54
|
+
link.href = downloadUrl;
|
|
55
|
+
link.download = filename || url.split("/").pop() || "download";
|
|
56
|
+
document.body.appendChild(link);
|
|
57
|
+
link.click();
|
|
58
|
+
document.body.removeChild(link);
|
|
59
|
+
window.URL.revokeObjectURL(downloadUrl);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function checkOnline() {
|
|
65
|
+
if (typeof navigator === "undefined") return true;
|
|
66
|
+
return navigator.onLine;
|
|
67
|
+
}
|
|
68
|
+
async function request(url, options = {}) {
|
|
69
|
+
const { method = "GET", headers = {}, body, timeout, retry = 0 } = options;
|
|
70
|
+
const fetchOptions = {
|
|
71
|
+
method,
|
|
72
|
+
headers: {
|
|
73
|
+
"Content-Type": "application/json",
|
|
74
|
+
...headers
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
if (body) {
|
|
78
|
+
fetchOptions.body = typeof body === "string" ? body : JSON.stringify(body);
|
|
79
|
+
}
|
|
80
|
+
let fetchFn = fetch;
|
|
81
|
+
if (timeout) {
|
|
82
|
+
fetchFn = (url2, opts) => fetchWithTimeout(url2, opts, timeout);
|
|
83
|
+
}
|
|
84
|
+
if (retry > 0) {
|
|
85
|
+
fetchFn = (url2, opts) => fetchWithRetry(url2, opts, retry);
|
|
86
|
+
}
|
|
87
|
+
const response = await fetchFn(url, fetchOptions);
|
|
88
|
+
if (!response.ok) {
|
|
89
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
90
|
+
}
|
|
91
|
+
const contentType = response.headers.get("content-type");
|
|
92
|
+
if (contentType && contentType.includes("application/json")) {
|
|
93
|
+
return response.json();
|
|
94
|
+
}
|
|
95
|
+
return response.text();
|
|
96
|
+
}
|
|
2
97
|
|
|
3
|
-
|
|
4
|
-
require('./chunk-PR4QN5HX.js');
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Object.defineProperty(exports, "checkOnline", {
|
|
9
|
-
enumerable: true,
|
|
10
|
-
get: function () { return chunk4RGXV4SJ_js.checkOnline; }
|
|
11
|
-
});
|
|
12
|
-
Object.defineProperty(exports, "downloadFile", {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
get: function () { return chunk4RGXV4SJ_js.downloadFile; }
|
|
15
|
-
});
|
|
16
|
-
Object.defineProperty(exports, "fetchWithRetry", {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
get: function () { return chunk4RGXV4SJ_js.fetchWithRetry; }
|
|
19
|
-
});
|
|
20
|
-
Object.defineProperty(exports, "fetchWithTimeout", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function () { return chunk4RGXV4SJ_js.fetchWithTimeout; }
|
|
23
|
-
});
|
|
24
|
-
Object.defineProperty(exports, "request", {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: function () { return chunk4RGXV4SJ_js.request; }
|
|
27
|
-
});
|
|
28
|
-
//# sourceMappingURL=network.js.map
|
|
29
|
-
//# sourceMappingURL=network.js.map
|
|
98
|
+
export { checkOnline, downloadFile, fetchWithRetry, fetchWithTimeout, request };
|
package/dist/number.js
CHANGED
|
@@ -1,57 +1,78 @@
|
|
|
1
|
-
|
|
1
|
+
// src/format/number.ts
|
|
2
|
+
function formatNumber(num, options = {}) {
|
|
3
|
+
const { decimals, separator = ",", decimalPoint = ".", minimumFractionDigits } = options;
|
|
4
|
+
let numStr;
|
|
5
|
+
if (decimals !== void 0) {
|
|
6
|
+
numStr = num.toFixed(decimals);
|
|
7
|
+
} else if (minimumFractionDigits !== void 0) {
|
|
8
|
+
numStr = num.toFixed(minimumFractionDigits);
|
|
9
|
+
} else {
|
|
10
|
+
const str = num.toString();
|
|
11
|
+
if (str.includes(".")) {
|
|
12
|
+
numStr = str;
|
|
13
|
+
} else {
|
|
14
|
+
numStr = num.toFixed(0);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const parts = numStr.split(".");
|
|
18
|
+
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, separator);
|
|
19
|
+
return parts.join(decimalPoint);
|
|
20
|
+
}
|
|
21
|
+
function random(min, max, integer = false) {
|
|
22
|
+
const value = Math.random() * (max - min) + min;
|
|
23
|
+
return integer ? Math.floor(value) : value;
|
|
24
|
+
}
|
|
25
|
+
function clamp(num, min, max) {
|
|
26
|
+
return Math.min(Math.max(num, min), max);
|
|
27
|
+
}
|
|
28
|
+
function round(num, decimals = 0) {
|
|
29
|
+
const factor = Math.pow(10, decimals);
|
|
30
|
+
return Math.round(num * factor) / factor;
|
|
31
|
+
}
|
|
32
|
+
function floor(num, decimals = 0) {
|
|
33
|
+
const factor = Math.pow(10, decimals);
|
|
34
|
+
return Math.floor(num * factor) / factor;
|
|
35
|
+
}
|
|
36
|
+
function ceil(num, decimals = 0) {
|
|
37
|
+
const factor = Math.pow(10, decimals);
|
|
38
|
+
return Math.ceil(num * factor) / factor;
|
|
39
|
+
}
|
|
40
|
+
function toFixed(num, decimals) {
|
|
41
|
+
return num.toFixed(decimals);
|
|
42
|
+
}
|
|
43
|
+
function parseNumber(str, defaultValue = 0) {
|
|
44
|
+
const num = Number(str);
|
|
45
|
+
return isNaN(num) ? defaultValue : num;
|
|
46
|
+
}
|
|
47
|
+
function isBetween(num, min, max, inclusive = true) {
|
|
48
|
+
if (inclusive) {
|
|
49
|
+
return num >= min && num <= max;
|
|
50
|
+
}
|
|
51
|
+
return num > min && num < max;
|
|
52
|
+
}
|
|
53
|
+
function percent(value, decimalsOrTotal, decimals) {
|
|
54
|
+
if (value >= 0 && value <= 1 && (decimalsOrTotal === void 0 || decimalsOrTotal < 10)) {
|
|
55
|
+
const dec2 = decimalsOrTotal ?? 0;
|
|
56
|
+
return Number((value * 100).toFixed(dec2));
|
|
57
|
+
}
|
|
58
|
+
if (decimalsOrTotal !== void 0 && decimalsOrTotal >= 10) {
|
|
59
|
+
const total = decimalsOrTotal;
|
|
60
|
+
const dec2 = decimals ?? 2;
|
|
61
|
+
if (total === 0) return "0%";
|
|
62
|
+
return `${(value / total * 100).toFixed(dec2)}%`;
|
|
63
|
+
}
|
|
64
|
+
const dec = decimalsOrTotal ?? 0;
|
|
65
|
+
return Number((value * 100).toFixed(dec));
|
|
66
|
+
}
|
|
67
|
+
function formatCurrency(num, currency = "\xA5", decimals = 2) {
|
|
68
|
+
return `${currency}${formatNumber(num, { decimals, separator: "," })}`;
|
|
69
|
+
}
|
|
70
|
+
function formatBytes(bytes, decimals = 2) {
|
|
71
|
+
if (bytes === 0) return "0 Bytes";
|
|
72
|
+
const k = 1024;
|
|
73
|
+
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB"];
|
|
74
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
75
|
+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${sizes[i]}`;
|
|
76
|
+
}
|
|
2
77
|
|
|
3
|
-
|
|
4
|
-
require('./chunk-PR4QN5HX.js');
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Object.defineProperty(exports, "ceil", {
|
|
9
|
-
enumerable: true,
|
|
10
|
-
get: function () { return chunk3FRYJPHG_js.ceil; }
|
|
11
|
-
});
|
|
12
|
-
Object.defineProperty(exports, "clamp", {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
get: function () { return chunk3FRYJPHG_js.clamp; }
|
|
15
|
-
});
|
|
16
|
-
Object.defineProperty(exports, "floor", {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
get: function () { return chunk3FRYJPHG_js.floor; }
|
|
19
|
-
});
|
|
20
|
-
Object.defineProperty(exports, "formatBytes", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function () { return chunk3FRYJPHG_js.formatBytes; }
|
|
23
|
-
});
|
|
24
|
-
Object.defineProperty(exports, "formatCurrency", {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: function () { return chunk3FRYJPHG_js.formatCurrency; }
|
|
27
|
-
});
|
|
28
|
-
Object.defineProperty(exports, "formatNumber", {
|
|
29
|
-
enumerable: true,
|
|
30
|
-
get: function () { return chunk3FRYJPHG_js.formatNumber; }
|
|
31
|
-
});
|
|
32
|
-
Object.defineProperty(exports, "isBetween", {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
get: function () { return chunk3FRYJPHG_js.isBetween; }
|
|
35
|
-
});
|
|
36
|
-
Object.defineProperty(exports, "parseNumber", {
|
|
37
|
-
enumerable: true,
|
|
38
|
-
get: function () { return chunk3FRYJPHG_js.parseNumber; }
|
|
39
|
-
});
|
|
40
|
-
Object.defineProperty(exports, "percent", {
|
|
41
|
-
enumerable: true,
|
|
42
|
-
get: function () { return chunk3FRYJPHG_js.percent; }
|
|
43
|
-
});
|
|
44
|
-
Object.defineProperty(exports, "random", {
|
|
45
|
-
enumerable: true,
|
|
46
|
-
get: function () { return chunk3FRYJPHG_js.random; }
|
|
47
|
-
});
|
|
48
|
-
Object.defineProperty(exports, "round", {
|
|
49
|
-
enumerable: true,
|
|
50
|
-
get: function () { return chunk3FRYJPHG_js.round; }
|
|
51
|
-
});
|
|
52
|
-
Object.defineProperty(exports, "toFixed", {
|
|
53
|
-
enumerable: true,
|
|
54
|
-
get: function () { return chunk3FRYJPHG_js.toFixed; }
|
|
55
|
-
});
|
|
56
|
-
//# sourceMappingURL=number.js.map
|
|
57
|
-
//# sourceMappingURL=number.js.map
|
|
78
|
+
export { ceil, clamp, floor, formatBytes, formatCurrency, formatNumber, isBetween, parseNumber, percent, random, round, toFixed };
|
package/dist/object.js
CHANGED
|
@@ -1,81 +1,238 @@
|
|
|
1
|
-
|
|
1
|
+
// src/core/object.ts
|
|
2
|
+
function deepClone(obj) {
|
|
3
|
+
if (obj === null || typeof obj !== "object") {
|
|
4
|
+
return obj;
|
|
5
|
+
}
|
|
6
|
+
if (obj instanceof Date) {
|
|
7
|
+
return new Date(obj.getTime());
|
|
8
|
+
}
|
|
9
|
+
if (obj instanceof RegExp) {
|
|
10
|
+
return new RegExp(obj.source, obj.flags);
|
|
11
|
+
}
|
|
12
|
+
if (Array.isArray(obj)) {
|
|
13
|
+
return obj.map((item) => deepClone(item));
|
|
14
|
+
}
|
|
15
|
+
if (obj instanceof Map) {
|
|
16
|
+
const clonedMap = /* @__PURE__ */ new Map();
|
|
17
|
+
obj.forEach((value, key) => {
|
|
18
|
+
clonedMap.set(deepClone(key), deepClone(value));
|
|
19
|
+
});
|
|
20
|
+
return clonedMap;
|
|
21
|
+
}
|
|
22
|
+
if (obj instanceof Set) {
|
|
23
|
+
const clonedSet = /* @__PURE__ */ new Set();
|
|
24
|
+
obj.forEach((value) => {
|
|
25
|
+
clonedSet.add(deepClone(value));
|
|
26
|
+
});
|
|
27
|
+
return clonedSet;
|
|
28
|
+
}
|
|
29
|
+
if (typeof obj === "object") {
|
|
30
|
+
const clonedObj = {};
|
|
31
|
+
for (const key in obj) {
|
|
32
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
33
|
+
clonedObj[key] = deepClone(obj[key]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return clonedObj;
|
|
37
|
+
}
|
|
38
|
+
return obj;
|
|
39
|
+
}
|
|
40
|
+
function merge(target, ...sources) {
|
|
41
|
+
return Object.assign({}, target, ...sources);
|
|
42
|
+
}
|
|
43
|
+
function deepMerge(target, ...sources) {
|
|
44
|
+
if (!sources.length) return target;
|
|
45
|
+
const source = sources.shift();
|
|
46
|
+
if (!source) {
|
|
47
|
+
return deepMerge(target, ...sources);
|
|
48
|
+
}
|
|
49
|
+
if (isObject(target) && isObject(source)) {
|
|
50
|
+
for (const key in source) {
|
|
51
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
52
|
+
const sourceValue = source[key];
|
|
53
|
+
const targetValue = target[key];
|
|
54
|
+
if (isObject(sourceValue) && isObject(targetValue)) {
|
|
55
|
+
target[key] = deepMerge(
|
|
56
|
+
targetValue,
|
|
57
|
+
sourceValue
|
|
58
|
+
);
|
|
59
|
+
} else if (sourceValue !== void 0) {
|
|
60
|
+
target[key] = sourceValue;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return deepMerge(target, ...sources);
|
|
66
|
+
}
|
|
67
|
+
function isObject(item) {
|
|
68
|
+
return item !== null && typeof item === "object" && !Array.isArray(item) && !(item instanceof Date) && !(item instanceof RegExp) && !(item instanceof Map) && !(item instanceof Set);
|
|
69
|
+
}
|
|
70
|
+
function get(obj, path, defaultValue) {
|
|
71
|
+
if (!obj || typeof obj !== "object") {
|
|
72
|
+
return defaultValue;
|
|
73
|
+
}
|
|
74
|
+
const keys2 = Array.isArray(path) ? path : path.split(".");
|
|
75
|
+
if (keys2.length === 0) {
|
|
76
|
+
return defaultValue;
|
|
77
|
+
}
|
|
78
|
+
let result = obj;
|
|
79
|
+
for (const key of keys2) {
|
|
80
|
+
if (result == null || typeof result !== "object" || Array.isArray(result)) {
|
|
81
|
+
return defaultValue;
|
|
82
|
+
}
|
|
83
|
+
const record = result;
|
|
84
|
+
if (!(key in record)) {
|
|
85
|
+
return defaultValue;
|
|
86
|
+
}
|
|
87
|
+
result = record[key];
|
|
88
|
+
}
|
|
89
|
+
return result !== void 0 ? result : defaultValue;
|
|
90
|
+
}
|
|
91
|
+
function set(obj, path, value) {
|
|
92
|
+
const keys2 = Array.isArray(path) ? path : path.split(".");
|
|
93
|
+
if (keys2.length === 0) return obj;
|
|
94
|
+
const result = deepClone(obj);
|
|
95
|
+
let current = result;
|
|
96
|
+
for (let i = 0; i < keys2.length - 1; i++) {
|
|
97
|
+
const key = keys2[i];
|
|
98
|
+
if (!(key in current) || typeof current[key] !== "object" || current[key] === null) {
|
|
99
|
+
current[key] = {};
|
|
100
|
+
}
|
|
101
|
+
current = current[key];
|
|
102
|
+
}
|
|
103
|
+
current[keys2[keys2.length - 1]] = value;
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
function pick(obj, keys2) {
|
|
107
|
+
const result = {};
|
|
108
|
+
keys2.forEach((key) => {
|
|
109
|
+
if (key in obj) {
|
|
110
|
+
result[key] = obj[key];
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
function omit(obj, keys2) {
|
|
116
|
+
const result = { ...obj };
|
|
117
|
+
keys2.forEach((key) => {
|
|
118
|
+
delete result[key];
|
|
119
|
+
});
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
function keys(obj, deep = false) {
|
|
123
|
+
if (!deep) return Object.keys(obj);
|
|
124
|
+
const result = [];
|
|
125
|
+
const traverse = (current, prefix = "") => {
|
|
126
|
+
Object.keys(current).forEach((key) => {
|
|
127
|
+
const fullKey = prefix ? `${prefix}.${key}` : key;
|
|
128
|
+
result.push(fullKey);
|
|
129
|
+
const value = current[key];
|
|
130
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value) && !(value instanceof Date) && !(value instanceof RegExp)) {
|
|
131
|
+
traverse(value, fullKey);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
traverse(obj);
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
function values(obj, deep = false) {
|
|
139
|
+
if (!deep) return Object.values(obj);
|
|
140
|
+
const result = [];
|
|
141
|
+
const traverse = (current) => {
|
|
142
|
+
Object.values(current).forEach((value) => {
|
|
143
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value) && !(value instanceof Date) && !(value instanceof RegExp)) {
|
|
144
|
+
traverse(value);
|
|
145
|
+
} else {
|
|
146
|
+
result.push(value);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
traverse(obj);
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
function mapKeys(obj, fn) {
|
|
154
|
+
const result = {};
|
|
155
|
+
Object.keys(obj).forEach((key) => {
|
|
156
|
+
const newKey = fn(key, obj[key]);
|
|
157
|
+
result[newKey] = obj[key];
|
|
158
|
+
});
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
function mapValues(obj, fn) {
|
|
162
|
+
const result = {};
|
|
163
|
+
Object.keys(obj).forEach((key) => {
|
|
164
|
+
result[key] = fn(obj[key], key);
|
|
165
|
+
});
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
function invert(obj) {
|
|
169
|
+
const result = {};
|
|
170
|
+
Object.keys(obj).forEach((key) => {
|
|
171
|
+
result[String(obj[key])] = key;
|
|
172
|
+
});
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
function defaults(obj, ...defaults2) {
|
|
176
|
+
const result = { ...obj };
|
|
177
|
+
defaults2.reverse().forEach((defaultObj) => {
|
|
178
|
+
Object.keys(defaultObj).forEach((key) => {
|
|
179
|
+
const typedKey = key;
|
|
180
|
+
if (!(typedKey in result) || result[typedKey] === void 0) {
|
|
181
|
+
result[typedKey] = defaultObj[typedKey];
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
function isEmptyObject(obj) {
|
|
188
|
+
return Object.keys(obj).length === 0;
|
|
189
|
+
}
|
|
190
|
+
function isEqual(obj1, obj2) {
|
|
191
|
+
if (obj1 === obj2) return true;
|
|
192
|
+
if (obj1 === null || obj2 === null) return false;
|
|
193
|
+
if (typeof obj1 !== "object" || typeof obj2 !== "object") return false;
|
|
194
|
+
if (obj1 instanceof Date && obj2 instanceof Date) {
|
|
195
|
+
return obj1.getTime() === obj2.getTime();
|
|
196
|
+
}
|
|
197
|
+
if (Array.isArray(obj1) && Array.isArray(obj2)) {
|
|
198
|
+
if (obj1.length !== obj2.length) return false;
|
|
199
|
+
return obj1.every((item, index) => isEqual(item, obj2[index]));
|
|
200
|
+
}
|
|
201
|
+
if (Array.isArray(obj1) || Array.isArray(obj2)) return false;
|
|
202
|
+
const keys1 = Object.keys(obj1);
|
|
203
|
+
const keys2 = Object.keys(obj2);
|
|
204
|
+
if (keys1.length !== keys2.length) return false;
|
|
205
|
+
return keys1.every((key) => {
|
|
206
|
+
const val1 = obj1[key];
|
|
207
|
+
const val2 = obj2[key];
|
|
208
|
+
return isEqual(val1, val2);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
function transform(obj, iteratee, accumulator = {}) {
|
|
212
|
+
Object.keys(obj).forEach((key) => {
|
|
213
|
+
iteratee(accumulator, obj[key], key);
|
|
214
|
+
});
|
|
215
|
+
return accumulator;
|
|
216
|
+
}
|
|
217
|
+
function pickBy(obj, predicate) {
|
|
218
|
+
const result = {};
|
|
219
|
+
Object.keys(obj).forEach((key) => {
|
|
220
|
+
const typedKey = key;
|
|
221
|
+
if (predicate(obj[typedKey], key)) {
|
|
222
|
+
result[typedKey] = obj[typedKey];
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
function omitBy(obj, predicate) {
|
|
228
|
+
const result = {};
|
|
229
|
+
Object.keys(obj).forEach((key) => {
|
|
230
|
+
const typedKey = key;
|
|
231
|
+
if (!predicate(obj[typedKey], key)) {
|
|
232
|
+
result[typedKey] = obj[typedKey];
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
2
237
|
|
|
3
|
-
|
|
4
|
-
require('./chunk-PR4QN5HX.js');
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Object.defineProperty(exports, "deepClone", {
|
|
9
|
-
enumerable: true,
|
|
10
|
-
get: function () { return chunk27UDDVLZ_js.deepClone; }
|
|
11
|
-
});
|
|
12
|
-
Object.defineProperty(exports, "deepMerge", {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
get: function () { return chunk27UDDVLZ_js.deepMerge; }
|
|
15
|
-
});
|
|
16
|
-
Object.defineProperty(exports, "defaults", {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
get: function () { return chunk27UDDVLZ_js.defaults; }
|
|
19
|
-
});
|
|
20
|
-
Object.defineProperty(exports, "get", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function () { return chunk27UDDVLZ_js.get; }
|
|
23
|
-
});
|
|
24
|
-
Object.defineProperty(exports, "invert", {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: function () { return chunk27UDDVLZ_js.invert; }
|
|
27
|
-
});
|
|
28
|
-
Object.defineProperty(exports, "isEmptyObject", {
|
|
29
|
-
enumerable: true,
|
|
30
|
-
get: function () { return chunk27UDDVLZ_js.isEmptyObject; }
|
|
31
|
-
});
|
|
32
|
-
Object.defineProperty(exports, "isEqual", {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
get: function () { return chunk27UDDVLZ_js.isEqual; }
|
|
35
|
-
});
|
|
36
|
-
Object.defineProperty(exports, "keys", {
|
|
37
|
-
enumerable: true,
|
|
38
|
-
get: function () { return chunk27UDDVLZ_js.keys; }
|
|
39
|
-
});
|
|
40
|
-
Object.defineProperty(exports, "mapKeys", {
|
|
41
|
-
enumerable: true,
|
|
42
|
-
get: function () { return chunk27UDDVLZ_js.mapKeys; }
|
|
43
|
-
});
|
|
44
|
-
Object.defineProperty(exports, "mapValues", {
|
|
45
|
-
enumerable: true,
|
|
46
|
-
get: function () { return chunk27UDDVLZ_js.mapValues; }
|
|
47
|
-
});
|
|
48
|
-
Object.defineProperty(exports, "merge", {
|
|
49
|
-
enumerable: true,
|
|
50
|
-
get: function () { return chunk27UDDVLZ_js.merge; }
|
|
51
|
-
});
|
|
52
|
-
Object.defineProperty(exports, "omit", {
|
|
53
|
-
enumerable: true,
|
|
54
|
-
get: function () { return chunk27UDDVLZ_js.omit; }
|
|
55
|
-
});
|
|
56
|
-
Object.defineProperty(exports, "omitBy", {
|
|
57
|
-
enumerable: true,
|
|
58
|
-
get: function () { return chunk27UDDVLZ_js.omitBy; }
|
|
59
|
-
});
|
|
60
|
-
Object.defineProperty(exports, "pick", {
|
|
61
|
-
enumerable: true,
|
|
62
|
-
get: function () { return chunk27UDDVLZ_js.pick; }
|
|
63
|
-
});
|
|
64
|
-
Object.defineProperty(exports, "pickBy", {
|
|
65
|
-
enumerable: true,
|
|
66
|
-
get: function () { return chunk27UDDVLZ_js.pickBy; }
|
|
67
|
-
});
|
|
68
|
-
Object.defineProperty(exports, "set", {
|
|
69
|
-
enumerable: true,
|
|
70
|
-
get: function () { return chunk27UDDVLZ_js.set; }
|
|
71
|
-
});
|
|
72
|
-
Object.defineProperty(exports, "transform", {
|
|
73
|
-
enumerable: true,
|
|
74
|
-
get: function () { return chunk27UDDVLZ_js.transform; }
|
|
75
|
-
});
|
|
76
|
-
Object.defineProperty(exports, "values", {
|
|
77
|
-
enumerable: true,
|
|
78
|
-
get: function () { return chunk27UDDVLZ_js.values; }
|
|
79
|
-
});
|
|
80
|
-
//# sourceMappingURL=object.js.map
|
|
81
|
-
//# sourceMappingURL=object.js.map
|
|
238
|
+
export { deepClone, deepMerge, defaults, get, invert, isEmptyObject, isEqual, keys, mapKeys, mapValues, merge, omit, omitBy, pick, pickBy, set, transform, values };
|