@cripty2001/utils 0.0.50 → 0.0.52
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 +2 -0
- package/dist/index.js +7 -0
- package/dist/react-whispr.d.ts +1 -1
- package/dist/react-whispr.js +29 -7
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Whispr } from "@cripty2001/whispr";
|
|
1
2
|
export type JSONEncodable = number | string | boolean | JSONEncodable[] | null | {
|
|
2
3
|
[key: string]: JSONEncodable;
|
|
3
4
|
};
|
|
@@ -82,3 +83,4 @@ export declare function randBetween(min: number, max: number): number;
|
|
|
82
83
|
* @returns The environment variable value, or the default value if provided
|
|
83
84
|
*/
|
|
84
85
|
export declare function getEnv(key: string, defaultValue?: string): string;
|
|
86
|
+
export declare const CURRENT_TS_MS: Whispr<number>;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CURRENT_TS_MS = void 0;
|
|
3
4
|
exports.getRandom = getRandom;
|
|
4
5
|
exports.getRandomId = getRandomId;
|
|
5
6
|
exports.getRandomOtp = getRandomOtp;
|
|
@@ -14,6 +15,7 @@ exports.stableLog = stableLog;
|
|
|
14
15
|
exports.parseQuery = parseQuery;
|
|
15
16
|
exports.randBetween = randBetween;
|
|
16
17
|
exports.getEnv = getEnv;
|
|
18
|
+
const whispr_1 = require("@cripty2001/whispr");
|
|
17
19
|
const lodash_1 = require("lodash");
|
|
18
20
|
/**
|
|
19
21
|
* Generate a random string from the given alphabeth.
|
|
@@ -199,3 +201,8 @@ function getEnv(key, defaultValue) {
|
|
|
199
201
|
throw new Error(`Environment variable ${key} is not defined and no default value was provided.`);
|
|
200
202
|
return value;
|
|
201
203
|
}
|
|
204
|
+
const [currentTsMs, setCurrentTsMs] = whispr_1.Whispr.create(Date.now());
|
|
205
|
+
setInterval(() => {
|
|
206
|
+
setCurrentTsMs(Date.now());
|
|
207
|
+
}, 200);
|
|
208
|
+
exports.CURRENT_TS_MS = currentTsMs;
|
package/dist/react-whispr.d.ts
CHANGED
|
@@ -33,7 +33,6 @@ export declare function useWhispr<T>(data: T | Whispr<T>): Whispr<T>;
|
|
|
33
33
|
export declare function useOnWhispr<T>(w: Whispr<T>, cb: (value: T) => void): void;
|
|
34
34
|
/**
|
|
35
35
|
* Return a reactive current timestamp (ms), updated at the given interval.
|
|
36
|
-
* @param refresh The refresh interval
|
|
37
36
|
* @returns The current timestamp
|
|
38
37
|
*/
|
|
39
38
|
export declare function useCurrentTimestamp(refresh?: number): number;
|
|
@@ -63,3 +62,4 @@ export declare function useAsync<I, O>(f: (input: I, setProgress: (p: number) =>
|
|
|
63
62
|
* @returns A callback (reactive, will change on refresh) that formats a given timestamp into a relative time string.
|
|
64
63
|
*/
|
|
65
64
|
export declare function useRelTime(refresh?: number): (ts: Date | number) => string;
|
|
65
|
+
export declare function timediff2HumanReadable(diffMs: number): string;
|
package/dist/react-whispr.js
CHANGED
|
@@ -7,10 +7,12 @@ exports.useCurrentTimestamp = useCurrentTimestamp;
|
|
|
7
7
|
exports.useDebounced = useDebounced;
|
|
8
8
|
exports.useAsync = useAsync;
|
|
9
9
|
exports.useRelTime = useRelTime;
|
|
10
|
+
exports.timediff2HumanReadable = timediff2HumanReadable;
|
|
10
11
|
const whispr_1 = require("@cripty2001/whispr");
|
|
11
12
|
const react_1 = require("react");
|
|
12
13
|
const lodash_1 = require("lodash");
|
|
13
14
|
const Dispatcher_1 = require("./Dispatcher");
|
|
15
|
+
const _1 = require(".");
|
|
14
16
|
/**
|
|
15
17
|
* Convert a Whispr value into a reactive react value, usable in function components with the standard react reactive system.
|
|
16
18
|
* If the value is not a Whispr, it is returned as is, thus allowing for progressive migration and adoption
|
|
@@ -69,16 +71,10 @@ function useOnWhispr(w, cb) {
|
|
|
69
71
|
}
|
|
70
72
|
/**
|
|
71
73
|
* Return a reactive current timestamp (ms), updated at the given interval.
|
|
72
|
-
* @param refresh The refresh interval
|
|
73
74
|
* @returns The current timestamp
|
|
74
75
|
*/
|
|
75
76
|
function useCurrentTimestamp(refresh = 1000) {
|
|
76
|
-
|
|
77
|
-
(0, react_1.useEffect)(() => {
|
|
78
|
-
const id = setInterval(() => setCurrTs(Date.now()), refresh);
|
|
79
|
-
return () => clearInterval(id);
|
|
80
|
-
}, [setCurrTs]);
|
|
81
|
-
return currTs;
|
|
77
|
+
return useWhisprValue(_1.CURRENT_TS_MS);
|
|
82
78
|
}
|
|
83
79
|
/**
|
|
84
80
|
* Debounce a reactive value, deep checking for equality, and stopping updates until the value changes.
|
|
@@ -137,3 +133,29 @@ function useRelTime(refresh = 1000) {
|
|
|
137
133
|
}, [currTs, rtf]);
|
|
138
134
|
return cb;
|
|
139
135
|
}
|
|
136
|
+
function timediff2HumanReadable(diffMs) {
|
|
137
|
+
const { unit, diff } = (() => {
|
|
138
|
+
let toReturn = diffMs;
|
|
139
|
+
const OPTIONS = [
|
|
140
|
+
{ divisor: 1000, unit: 'milliseconds' },
|
|
141
|
+
{ divisor: 60, unit: 'seconds' },
|
|
142
|
+
{ divisor: 60, unit: 'minutes' },
|
|
143
|
+
{ divisor: 24, unit: 'hours' },
|
|
144
|
+
{ divisor: 30, unit: 'days' },
|
|
145
|
+
{ divisor: 12, unit: 'months' },
|
|
146
|
+
{ divisor: 1, unit: 'years' },
|
|
147
|
+
];
|
|
148
|
+
while (OPTIONS.length) {
|
|
149
|
+
const { divisor, unit } = OPTIONS.shift();
|
|
150
|
+
if (Math.abs(toReturn) < divisor)
|
|
151
|
+
return {
|
|
152
|
+
unit,
|
|
153
|
+
diff: toReturn
|
|
154
|
+
};
|
|
155
|
+
toReturn = toReturn / divisor;
|
|
156
|
+
}
|
|
157
|
+
return { unit: 'years', diff: toReturn };
|
|
158
|
+
})();
|
|
159
|
+
const rtf = new Intl.RelativeTimeFormat();
|
|
160
|
+
return rtf.format(Math.round(diff), unit);
|
|
161
|
+
}
|
package/package.json
CHANGED