@cripty2001/utils 0.0.38 → 0.0.40
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/react-whispr.d.ts +7 -0
- package/dist/react-whispr.js +19 -0
- package/package.json +2 -2
package/dist/react-whispr.d.ts
CHANGED
|
@@ -56,3 +56,10 @@ export declare function useDebounced<T>(value: T): T;
|
|
|
56
56
|
* @type O Output for the async function
|
|
57
57
|
*/
|
|
58
58
|
export declare function useAsync<I, O>(f: (input: I, setProgress: (p: number) => void, signal: AbortSignal) => Promise<O>, data: I, debouce?: number): Dispatcher<I, O>;
|
|
59
|
+
/**
|
|
60
|
+
* Format a timestamp into a relative time string (e.g. "5 minutes ago", "in 2 hours"), using the browser locale.
|
|
61
|
+
* The refreshed time is reactive.
|
|
62
|
+
* @param refresh The refresh interval, in milliseconds. Default to 1000ms.
|
|
63
|
+
* @returns A callback (reactive, will change on refresh) that formats a given timestamp into a relative time string.
|
|
64
|
+
*/
|
|
65
|
+
export declare function useRelTime(refresh?: number): (ts: Date | number) => string;
|
package/dist/react-whispr.js
CHANGED
|
@@ -6,6 +6,7 @@ exports.useOnWhispr = useOnWhispr;
|
|
|
6
6
|
exports.useCurrentTimestamp = useCurrentTimestamp;
|
|
7
7
|
exports.useDebounced = useDebounced;
|
|
8
8
|
exports.useAsync = useAsync;
|
|
9
|
+
exports.useRelTime = useRelTime;
|
|
9
10
|
const whispr_1 = require("@cripty2001/whispr");
|
|
10
11
|
const react_1 = require("react");
|
|
11
12
|
const lodash_1 = require("lodash");
|
|
@@ -118,3 +119,21 @@ function useAsync(f, data, debouce = 200) {
|
|
|
118
119
|
// Returning dispatcher
|
|
119
120
|
return dispatcher;
|
|
120
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Format a timestamp into a relative time string (e.g. "5 minutes ago", "in 2 hours"), using the browser locale.
|
|
124
|
+
* The refreshed time is reactive.
|
|
125
|
+
* @param refresh The refresh interval, in milliseconds. Default to 1000ms.
|
|
126
|
+
* @returns A callback (reactive, will change on refresh) that formats a given timestamp into a relative time string.
|
|
127
|
+
*/
|
|
128
|
+
function useRelTime(refresh = 1000) {
|
|
129
|
+
const currTs = useCurrentTimestamp(refresh);
|
|
130
|
+
const rtf = (0, react_1.useRef)(new Intl.RelativeTimeFormat(navigator.language, { numeric: "auto" })).current;
|
|
131
|
+
const cb = (0, react_1.useCallback)((ts) => {
|
|
132
|
+
const now = currTs;
|
|
133
|
+
const then = ts instanceof Date ? ts.getTime() : ts;
|
|
134
|
+
const delta = then - now;
|
|
135
|
+
const seconds = Math.round(delta / 1000);
|
|
136
|
+
return rtf.format(seconds, "second");
|
|
137
|
+
}, [currTs, rtf]);
|
|
138
|
+
return cb;
|
|
139
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cripty2001/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.40",
|
|
4
4
|
"description": "Internal Set of utils. If you need them use them, otherwise go to the next package ;)",
|
|
5
5
|
"homepage": "https://github.com/cripty2001/utils#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"typescript": "^5.9.3"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@cripty2001/whispr": "^0.
|
|
32
|
+
"@cripty2001/whispr": "^0.2.0",
|
|
33
33
|
"@types/lodash": "^4.17.20",
|
|
34
34
|
"lodash": "^4.17.21"
|
|
35
35
|
},
|