@cripty2001/utils 0.0.51 → 0.0.53

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 CHANGED
@@ -84,3 +84,4 @@ 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;
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ exports.stableLog = stableLog;
15
15
  exports.parseQuery = parseQuery;
16
16
  exports.randBetween = randBetween;
17
17
  exports.getEnv = getEnv;
18
+ exports.timediff2HumanReadable = timediff2HumanReadable;
18
19
  const whispr_1 = require("@cripty2001/whispr");
19
20
  const lodash_1 = require("lodash");
20
21
  /**
@@ -206,3 +207,29 @@ setInterval(() => {
206
207
  setCurrentTsMs(Date.now());
207
208
  }, 200);
208
209
  exports.CURRENT_TS_MS = currentTsMs;
210
+ function timediff2HumanReadable(diffMs) {
211
+ const { unit, diff } = (() => {
212
+ let toReturn = diffMs;
213
+ const OPTIONS = [
214
+ { divisor: 1000, unit: 'milliseconds' },
215
+ { divisor: 60, unit: 'seconds' },
216
+ { divisor: 60, unit: 'minutes' },
217
+ { divisor: 24, unit: 'hours' },
218
+ { divisor: 30, unit: 'days' },
219
+ { divisor: 12, unit: 'months' },
220
+ { divisor: 1, unit: 'years' },
221
+ ];
222
+ while (OPTIONS.length) {
223
+ const { divisor, unit } = OPTIONS.shift();
224
+ if (Math.abs(toReturn) < divisor)
225
+ return {
226
+ unit,
227
+ diff: toReturn
228
+ };
229
+ toReturn = toReturn / divisor;
230
+ }
231
+ return { unit: 'years', diff: toReturn };
232
+ })();
233
+ const rtf = new Intl.RelativeTimeFormat();
234
+ return rtf.format(Math.round(diff), unit);
235
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.51",
3
+ "version": "0.0.53",
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": {