@cripty2001/utils 0.0.7 → 0.0.9
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/index.d.ts +7 -0
- package/dist/index.js +30 -0
- package/dist/react-whispr.d.ts +2 -0
- package/dist/react-whispr.js +21 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -9,3 +9,10 @@ export declare function getRandomOtp(length?: number, char?: boolean): string;
|
|
|
9
9
|
export declare function sleep(ms: number): Promise<void>;
|
|
10
10
|
export declare function parseHash(fields: string[]): URLSearchParams;
|
|
11
11
|
export declare function loop(cb: () => Promise<void>, interval: number, onError?: (e: any) => Promise<void>): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Float aware deep equality check between two values.
|
|
14
|
+
*/
|
|
15
|
+
export declare function isEqual(a: any, b: any): boolean;
|
|
16
|
+
export declare function arrayStep(from: number, to: number, step: number): number[];
|
|
17
|
+
export declare function copyToClipboard(text: string): void;
|
|
18
|
+
export declare function stableLog(obj: any, message?: string): void;
|
package/dist/index.js
CHANGED
|
@@ -6,6 +6,11 @@ exports.getRandomOtp = getRandomOtp;
|
|
|
6
6
|
exports.sleep = sleep;
|
|
7
7
|
exports.parseHash = parseHash;
|
|
8
8
|
exports.loop = loop;
|
|
9
|
+
exports.isEqual = isEqual;
|
|
10
|
+
exports.arrayStep = arrayStep;
|
|
11
|
+
exports.copyToClipboard = copyToClipboard;
|
|
12
|
+
exports.stableLog = stableLog;
|
|
13
|
+
const lodash_1 = require("lodash");
|
|
9
14
|
function getRandom(_alphabeth, length) {
|
|
10
15
|
const alphabeth = _alphabeth.split("");
|
|
11
16
|
const toReturn = [];
|
|
@@ -57,3 +62,28 @@ async function loop(cb, interval, onError = async (e) => { console.error(e); })
|
|
|
57
62
|
}
|
|
58
63
|
}
|
|
59
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Float aware deep equality check between two values.
|
|
67
|
+
*/
|
|
68
|
+
function isEqual(a, b) {
|
|
69
|
+
const TOLERANCE = 1e-9;
|
|
70
|
+
const toReturn = (0, lodash_1.isEqualWith)(a, b, (a, b) => {
|
|
71
|
+
if (typeof a === 'number' && typeof b === 'number')
|
|
72
|
+
return Math.abs(a - b) < TOLERANCE;
|
|
73
|
+
return undefined;
|
|
74
|
+
});
|
|
75
|
+
return toReturn;
|
|
76
|
+
}
|
|
77
|
+
function arrayStep(from, to, step) {
|
|
78
|
+
const result = [];
|
|
79
|
+
for (let i = from; i <= to; i += step) {
|
|
80
|
+
result.push(i);
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
function copyToClipboard(text) {
|
|
85
|
+
navigator.clipboard.writeText(text);
|
|
86
|
+
}
|
|
87
|
+
function stableLog(obj, message = '') {
|
|
88
|
+
console.log(message, JSON.parse(JSON.stringify(obj)));
|
|
89
|
+
}
|
package/dist/react-whispr.d.ts
CHANGED
|
@@ -24,3 +24,5 @@ export declare function useWhisprValue<I, O = I>(w: Whispr<I>, computer?: (data:
|
|
|
24
24
|
* @remarks The returned whispr has already been ref-fed, so it can be directly used without worrying about react recreating it or similar bad things
|
|
25
25
|
*/
|
|
26
26
|
export declare function useWhispr<T>(data: T | Whispr<T>): Whispr<T>;
|
|
27
|
+
export declare function useCurrentTimestamp(): number;
|
|
28
|
+
export declare function useDebounced<T>(value: T): T;
|
package/dist/react-whispr.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useWhisprValue = useWhisprValue;
|
|
4
4
|
exports.useWhispr = useWhispr;
|
|
5
|
+
exports.useCurrentTimestamp = useCurrentTimestamp;
|
|
6
|
+
exports.useDebounced = useDebounced;
|
|
5
7
|
const whispr_1 = require("@cripty2001/whispr");
|
|
6
8
|
const react_1 = require("react");
|
|
7
9
|
const lodash_1 = require("lodash");
|
|
@@ -50,3 +52,22 @@ function useWhispr(data) {
|
|
|
50
52
|
return data;
|
|
51
53
|
return w;
|
|
52
54
|
}
|
|
55
|
+
function useCurrentTimestamp() {
|
|
56
|
+
const [currTs, setCurrTs] = (0, react_1.useState)(Date.now());
|
|
57
|
+
(0, react_1.useEffect)(() => {
|
|
58
|
+
const id = setInterval(() => setCurrTs(Date.now()), 1000);
|
|
59
|
+
return () => clearInterval(id);
|
|
60
|
+
}, [setCurrTs]);
|
|
61
|
+
return currTs;
|
|
62
|
+
}
|
|
63
|
+
function useDebounced(value) {
|
|
64
|
+
const lastEmitted = (0, react_1.useRef)(value);
|
|
65
|
+
const [debounced, setDebounced] = (0, react_1.useState)(value);
|
|
66
|
+
(0, react_1.useEffect)(() => {
|
|
67
|
+
if ((0, lodash_1.isEqual)(lastEmitted.current, value))
|
|
68
|
+
return;
|
|
69
|
+
lastEmitted.current = value;
|
|
70
|
+
setDebounced(value);
|
|
71
|
+
}, [value]);
|
|
72
|
+
return debounced;
|
|
73
|
+
}
|
package/package.json
CHANGED