@cripty2001/utils 0.0.8 → 0.0.10

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.
@@ -0,0 +1,22 @@
1
+ import { Whispr } from "@cripty2001/whispr";
2
+ type LoggerItem = {
3
+ id: string;
4
+ date: Date;
5
+ message: string;
6
+ severity: "success" | "info" | "warning" | "error";
7
+ context: string | undefined;
8
+ dismiss: () => void;
9
+ trace: string | undefined;
10
+ };
11
+ declare class Logger {
12
+ private static instance;
13
+ lines: Whispr<LoggerItem[]>;
14
+ private setLines;
15
+ private constructor();
16
+ private reconstructError;
17
+ static getInstance(): Logger;
18
+ log(message: string, severity?: LoggerItem['severity'], context?: string): void;
19
+ logError(error: Error | DOMException | any): void;
20
+ }
21
+ declare const _default: Logger;
22
+ export default _default;
package/dist/Logger.js ADDED
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const whispr_1 = require("@cripty2001/whispr");
4
+ const _1 = require(".");
5
+ class Logger {
6
+ static instance;
7
+ lines;
8
+ setLines;
9
+ constructor() {
10
+ [this.lines, this.setLines] = whispr_1.Whispr.create([]);
11
+ window.addEventListener("unhandledrejection", (e) => this.log(e.reason, "error"));
12
+ window.addEventListener("error", (e) => this.logError(this.reconstructError(e)));
13
+ }
14
+ reconstructError(e) {
15
+ if (e.error && e.error instanceof Error) {
16
+ return e.error;
17
+ }
18
+ if (e.message && typeof e.message === 'string') {
19
+ return new Error(e.message);
20
+ }
21
+ return new Error("Unknown error");
22
+ }
23
+ static getInstance() {
24
+ if (!Logger.instance) {
25
+ Logger.instance = new Logger();
26
+ }
27
+ return Logger.instance;
28
+ }
29
+ log(message, severity = "info", context) {
30
+ const item = {
31
+ id: (0, _1.getRandomId)(),
32
+ date: new Date(),
33
+ message,
34
+ severity,
35
+ context,
36
+ dismiss: () => {
37
+ this.setLines(this.lines.value.filter(i => i.id !== item.id));
38
+ },
39
+ trace: (new Error()).stack
40
+ };
41
+ this.setLines([...this.lines.value, item]);
42
+ }
43
+ logError(error) {
44
+ if (error instanceof DOMException ||
45
+ error instanceof Error ||
46
+ false) {
47
+ return this.log(error.message, "error", error.stack);
48
+ }
49
+ console.trace('UNKNOWN ERROR', error);
50
+ return this.log("Unknown error - See Console", "error");
51
+ }
52
+ }
53
+ exports.default = Logger.getInstance();
package/dist/index.d.ts CHANGED
@@ -9,3 +9,10 @@ export declare function getRandomOtp(length?: number, char?: boolean): string;
9
9
  export declare function sleep(ms: number): Promise<void>;
10
10
  export declare function parseHash(fields: string[]): URLSearchParams;
11
11
  export declare function loop(cb: () => Promise<void>, interval: number, onError?: (e: any) => Promise<void>): Promise<void>;
12
+ /**
13
+ * Float aware deep equality check between two values.
14
+ */
15
+ export declare function isEqual(a: any, b: any): boolean;
16
+ export declare function arrayStep(from: number, to: number, step: number): number[];
17
+ export declare function copyToClipboard(text: string): void;
18
+ export declare function stableLog(obj: any, message?: string): void;
package/dist/index.js CHANGED
@@ -6,6 +6,11 @@ exports.getRandomOtp = getRandomOtp;
6
6
  exports.sleep = sleep;
7
7
  exports.parseHash = parseHash;
8
8
  exports.loop = loop;
9
+ exports.isEqual = isEqual;
10
+ exports.arrayStep = arrayStep;
11
+ exports.copyToClipboard = copyToClipboard;
12
+ exports.stableLog = stableLog;
13
+ const lodash_1 = require("lodash");
9
14
  function getRandom(_alphabeth, length) {
10
15
  const alphabeth = _alphabeth.split("");
11
16
  const toReturn = [];
@@ -57,3 +62,28 @@ async function loop(cb, interval, onError = async (e) => { console.error(e); })
57
62
  }
58
63
  }
59
64
  }
65
+ /**
66
+ * Float aware deep equality check between two values.
67
+ */
68
+ function isEqual(a, b) {
69
+ const TOLERANCE = 1e-9;
70
+ const toReturn = (0, lodash_1.isEqualWith)(a, b, (a, b) => {
71
+ if (typeof a === 'number' && typeof b === 'number')
72
+ return Math.abs(a - b) < TOLERANCE;
73
+ return undefined;
74
+ });
75
+ return toReturn;
76
+ }
77
+ function arrayStep(from, to, step) {
78
+ const result = [];
79
+ for (let i = from; i <= to; i += step) {
80
+ result.push(i);
81
+ }
82
+ return result;
83
+ }
84
+ function copyToClipboard(text) {
85
+ navigator.clipboard.writeText(text);
86
+ }
87
+ function stableLog(obj, message = '') {
88
+ console.log(message, JSON.parse(JSON.stringify(obj)));
89
+ }
@@ -24,3 +24,5 @@ export declare function useWhisprValue<I, O = I>(w: Whispr<I>, computer?: (data:
24
24
  * @remarks The returned whispr has already been ref-fed, so it can be directly used without worrying about react recreating it or similar bad things
25
25
  */
26
26
  export declare function useWhispr<T>(data: T | Whispr<T>): Whispr<T>;
27
+ export declare function useCurrentTimestamp(): number;
28
+ export declare function useDebounced<T>(value: T): T;
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useWhisprValue = useWhisprValue;
4
4
  exports.useWhispr = useWhispr;
5
+ exports.useCurrentTimestamp = useCurrentTimestamp;
6
+ exports.useDebounced = useDebounced;
5
7
  const whispr_1 = require("@cripty2001/whispr");
6
8
  const react_1 = require("react");
7
9
  const lodash_1 = require("lodash");
@@ -50,3 +52,22 @@ function useWhispr(data) {
50
52
  return data;
51
53
  return w;
52
54
  }
55
+ function useCurrentTimestamp() {
56
+ const [currTs, setCurrTs] = (0, react_1.useState)(Date.now());
57
+ (0, react_1.useEffect)(() => {
58
+ const id = setInterval(() => setCurrTs(Date.now()), 1000);
59
+ return () => clearInterval(id);
60
+ }, [setCurrTs]);
61
+ return currTs;
62
+ }
63
+ function useDebounced(value) {
64
+ const lastEmitted = (0, react_1.useRef)(value);
65
+ const [debounced, setDebounced] = (0, react_1.useState)(value);
66
+ (0, react_1.useEffect)(() => {
67
+ if ((0, lodash_1.isEqual)(lastEmitted.current, value))
68
+ return;
69
+ lastEmitted.current = value;
70
+ setDebounced(value);
71
+ }, [value]);
72
+ return debounced;
73
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
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": {
@@ -51,6 +51,10 @@
51
51
  "./react-whispr": {
52
52
  "import": "./dist/react-whispr.js",
53
53
  "types": "./dist/react-whispr.d.ts"
54
+ },
55
+ "./logger": {
56
+ "import": "./dist/logger.js",
57
+ "types": "./dist/logger.d.ts"
54
58
  }
55
59
  }
56
60
  }