@cripty2001/utils 0.0.51 → 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/react-whispr.d.ts +1 -0
- package/dist/react-whispr.js +27 -0
- package/package.json +1 -1
package/dist/react-whispr.d.ts
CHANGED
|
@@ -62,3 +62,4 @@ export declare function useAsync<I, O>(f: (input: I, setProgress: (p: number) =>
|
|
|
62
62
|
* @returns A callback (reactive, will change on refresh) that formats a given timestamp into a relative time string.
|
|
63
63
|
*/
|
|
64
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,6 +7,7 @@ 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");
|
|
@@ -132,3 +133,29 @@ function useRelTime(refresh = 1000) {
|
|
|
132
133
|
}, [currTs, rtf]);
|
|
133
134
|
return cb;
|
|
134
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