@visitor-analytics-sdk/utils 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 +21 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +120 -0
- package/package.json +21 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Visitor Analytics
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
declare const SDK_VERSION = "1.0.0";
|
|
2
|
+
declare function generateId(): string;
|
|
3
|
+
declare function generateShortId(): string;
|
|
4
|
+
declare function debounce<T extends (...args: readonly unknown[]) => void>(fn: T, delay: number): T & {
|
|
5
|
+
cancel(): void;
|
|
6
|
+
};
|
|
7
|
+
declare function throttle<T extends (...args: readonly unknown[]) => void>(fn: T, limit: number): T & {
|
|
8
|
+
cancel(): void;
|
|
9
|
+
};
|
|
10
|
+
declare function safeCall<T>(fn: () => T, fallback: T): T;
|
|
11
|
+
declare function safeCallAsync<T>(fn: () => Promise<T>, fallback: T): Promise<T>;
|
|
12
|
+
declare function requestIdle(callback: () => void, timeout?: number): void;
|
|
13
|
+
declare function deepFreeze<T extends object>(obj: T): Readonly<T>;
|
|
14
|
+
declare function cloneRecord<T>(record: T): T;
|
|
15
|
+
declare function isBrowser(): boolean;
|
|
16
|
+
declare function noop(): void;
|
|
17
|
+
declare function padZero(n: number): string;
|
|
18
|
+
|
|
19
|
+
export { SDK_VERSION, cloneRecord, debounce, deepFreeze, generateId, generateShortId, isBrowser, noop, padZero, requestIdle, safeCall, safeCallAsync, throttle };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var SDK_VERSION = "1.0.0";
|
|
3
|
+
function generateId() {
|
|
4
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
5
|
+
return crypto.randomUUID();
|
|
6
|
+
}
|
|
7
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
8
|
+
const r = Math.random() * 16 | 0;
|
|
9
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
10
|
+
return v.toString(16);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
function generateShortId() {
|
|
14
|
+
if (typeof crypto !== "undefined" && crypto.getRandomValues) {
|
|
15
|
+
const bytes = new Uint8Array(8);
|
|
16
|
+
crypto.getRandomValues(bytes);
|
|
17
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
18
|
+
}
|
|
19
|
+
return generateId().replace(/-/g, "").slice(0, 16);
|
|
20
|
+
}
|
|
21
|
+
function debounce(fn, delay) {
|
|
22
|
+
let timer = null;
|
|
23
|
+
const debounced = (...args) => {
|
|
24
|
+
if (timer !== null) {
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
}
|
|
27
|
+
timer = setTimeout(() => {
|
|
28
|
+
fn(...args);
|
|
29
|
+
timer = null;
|
|
30
|
+
}, delay);
|
|
31
|
+
};
|
|
32
|
+
debounced.cancel = () => {
|
|
33
|
+
if (timer !== null) {
|
|
34
|
+
clearTimeout(timer);
|
|
35
|
+
timer = null;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
return debounced;
|
|
39
|
+
}
|
|
40
|
+
function throttle(fn, limit) {
|
|
41
|
+
let inThrottle = false;
|
|
42
|
+
let lastArgs = null;
|
|
43
|
+
const throttled = (...args) => {
|
|
44
|
+
if (!inThrottle) {
|
|
45
|
+
fn(...args);
|
|
46
|
+
inThrottle = true;
|
|
47
|
+
setTimeout(() => {
|
|
48
|
+
inThrottle = false;
|
|
49
|
+
if (lastArgs) {
|
|
50
|
+
fn(...lastArgs);
|
|
51
|
+
lastArgs = null;
|
|
52
|
+
}
|
|
53
|
+
}, limit);
|
|
54
|
+
} else {
|
|
55
|
+
lastArgs = args;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
throttled.cancel = () => {
|
|
59
|
+
inThrottle = false;
|
|
60
|
+
lastArgs = null;
|
|
61
|
+
};
|
|
62
|
+
return throttled;
|
|
63
|
+
}
|
|
64
|
+
function safeCall(fn, fallback) {
|
|
65
|
+
try {
|
|
66
|
+
return fn();
|
|
67
|
+
} catch {
|
|
68
|
+
return fallback;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function safeCallAsync(fn, fallback) {
|
|
72
|
+
try {
|
|
73
|
+
return await fn();
|
|
74
|
+
} catch {
|
|
75
|
+
return fallback;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function requestIdle(callback, timeout = 5e3) {
|
|
79
|
+
if (typeof window !== "undefined" && "requestIdleCallback" in window) {
|
|
80
|
+
window.requestIdleCallback(callback, { timeout });
|
|
81
|
+
} else {
|
|
82
|
+
setTimeout(callback, 0);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function deepFreeze(obj) {
|
|
86
|
+
Object.freeze(obj);
|
|
87
|
+
for (const key of Object.keys(obj)) {
|
|
88
|
+
const val = obj[key];
|
|
89
|
+
if (val !== null && typeof val === "object" && !Object.isFrozen(val)) {
|
|
90
|
+
deepFreeze(val);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return obj;
|
|
94
|
+
}
|
|
95
|
+
function cloneRecord(record) {
|
|
96
|
+
return JSON.parse(JSON.stringify(record));
|
|
97
|
+
}
|
|
98
|
+
function isBrowser() {
|
|
99
|
+
return typeof window !== "undefined" && typeof document !== "undefined" && typeof navigator !== "undefined";
|
|
100
|
+
}
|
|
101
|
+
function noop() {
|
|
102
|
+
}
|
|
103
|
+
function padZero(n) {
|
|
104
|
+
return n < 10 ? `0${n}` : `${n}`;
|
|
105
|
+
}
|
|
106
|
+
export {
|
|
107
|
+
SDK_VERSION,
|
|
108
|
+
cloneRecord,
|
|
109
|
+
debounce,
|
|
110
|
+
deepFreeze,
|
|
111
|
+
generateId,
|
|
112
|
+
generateShortId,
|
|
113
|
+
isBrowser,
|
|
114
|
+
noop,
|
|
115
|
+
padZero,
|
|
116
|
+
requestIdle,
|
|
117
|
+
safeCall,
|
|
118
|
+
safeCallAsync,
|
|
119
|
+
throttle
|
|
120
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@visitor-analytics-sdk/utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
19
|
+
"typecheck": "tsc --noEmit"
|
|
20
|
+
}
|
|
21
|
+
}
|