@cripty2001/utils 0.0.52 → 0.0.54
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 +34 -0
- package/dist/react-whispr.d.ts +0 -1
- package/dist/react-whispr.js +0 -27
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -84,3 +84,5 @@ export declare function randBetween(min: number, max: number): number;
|
|
|
84
84
|
*/
|
|
85
85
|
export declare function getEnv(key: string, defaultValue?: string): string;
|
|
86
86
|
export declare const CURRENT_TS_MS: Whispr<number>;
|
|
87
|
+
export declare function timediff2HumanReadable(diffMs: number): string;
|
|
88
|
+
export declare function fn2promise<T>(fn: () => T | Promise<T>): Promise<T>;
|
package/dist/index.js
CHANGED
|
@@ -15,6 +15,8 @@ exports.stableLog = stableLog;
|
|
|
15
15
|
exports.parseQuery = parseQuery;
|
|
16
16
|
exports.randBetween = randBetween;
|
|
17
17
|
exports.getEnv = getEnv;
|
|
18
|
+
exports.timediff2HumanReadable = timediff2HumanReadable;
|
|
19
|
+
exports.fn2promise = fn2promise;
|
|
18
20
|
const whispr_1 = require("@cripty2001/whispr");
|
|
19
21
|
const lodash_1 = require("lodash");
|
|
20
22
|
/**
|
|
@@ -206,3 +208,35 @@ setInterval(() => {
|
|
|
206
208
|
setCurrentTsMs(Date.now());
|
|
207
209
|
}, 200);
|
|
208
210
|
exports.CURRENT_TS_MS = currentTsMs;
|
|
211
|
+
function timediff2HumanReadable(diffMs) {
|
|
212
|
+
const { unit, diff } = (() => {
|
|
213
|
+
let toReturn = diffMs;
|
|
214
|
+
const OPTIONS = [
|
|
215
|
+
{ divisor: 1000, unit: 'milliseconds' },
|
|
216
|
+
{ divisor: 60, unit: 'seconds' },
|
|
217
|
+
{ divisor: 60, unit: 'minutes' },
|
|
218
|
+
{ divisor: 24, unit: 'hours' },
|
|
219
|
+
{ divisor: 30, unit: 'days' },
|
|
220
|
+
{ divisor: 12, unit: 'months' },
|
|
221
|
+
{ divisor: 1, unit: 'years' },
|
|
222
|
+
];
|
|
223
|
+
while (OPTIONS.length) {
|
|
224
|
+
const { divisor, unit } = OPTIONS.shift();
|
|
225
|
+
if (Math.abs(toReturn) < divisor)
|
|
226
|
+
return {
|
|
227
|
+
unit,
|
|
228
|
+
diff: toReturn
|
|
229
|
+
};
|
|
230
|
+
toReturn = toReturn / divisor;
|
|
231
|
+
}
|
|
232
|
+
return { unit: 'years', diff: toReturn };
|
|
233
|
+
})();
|
|
234
|
+
const rtf = new Intl.RelativeTimeFormat();
|
|
235
|
+
return rtf.format(Math.round(diff), unit);
|
|
236
|
+
}
|
|
237
|
+
function fn2promise(fn) {
|
|
238
|
+
const result = fn();
|
|
239
|
+
return result instanceof Promise ?
|
|
240
|
+
result :
|
|
241
|
+
Promise.resolve(result);
|
|
242
|
+
}
|
package/dist/react-whispr.d.ts
CHANGED
|
@@ -62,4 +62,3 @@ 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,7 +7,6 @@ exports.useCurrentTimestamp = useCurrentTimestamp;
|
|
|
7
7
|
exports.useDebounced = useDebounced;
|
|
8
8
|
exports.useAsync = useAsync;
|
|
9
9
|
exports.useRelTime = useRelTime;
|
|
10
|
-
exports.timediff2HumanReadable = timediff2HumanReadable;
|
|
11
10
|
const whispr_1 = require("@cripty2001/whispr");
|
|
12
11
|
const react_1 = require("react");
|
|
13
12
|
const lodash_1 = require("lodash");
|
|
@@ -133,29 +132,3 @@ function useRelTime(refresh = 1000) {
|
|
|
133
132
|
}, [currTs, rtf]);
|
|
134
133
|
return cb;
|
|
135
134
|
}
|
|
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