@choksheak/ts-utils 0.1.3 → 0.1.5

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/assert.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Type asserts that `t` is truthy. Throws an error with `errorMessage` if
3
+ * `t` is falsy.
4
+ */
5
+ export declare function assert<T>(t: T | null | undefined | "" | 0 | -0 | 0n | false | typeof NaN, errorMessage: string): asserts t is T;
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/assert.ts"],"sourcesContent":["/**\n * Type asserts that `t` is truthy. Throws an error with `errorMessage` if\n * `t` is falsy.\n */\nexport function assert<T>(\n t: T | null | undefined | \"\" | 0 | -0 | 0n | false | typeof NaN,\n errorMessage: string,\n): asserts t is T {\n if (!t) {\n throw new Error(errorMessage);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIO,SAAS,OACd,GACA,cACgB;AAChB,MAAI,CAAC,GAAG;AACN,UAAM,IAAI,MAAM,YAAY;AAAA,EAC9B;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/assert.ts"],"sourcesContent":["/**\n * Type asserts that `t` is truthy. Throws an error with `errorMessage` if\n * `t` is falsy.\n */\nexport function assert<T>(\n t: T | null | undefined | \"\" | 0 | -0 | 0n | false | typeof NaN,\n errorMessage: string,\n): asserts t is T {\n if (!t) {\n throw new Error(errorMessage);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIO,SAAS,OACd,GACA,cACgB;AAChB,MAAI,CAAC,GAAG;AACN,UAAM,IAAI,MAAM,YAAY;AAAA,EAC9B;AACF;","names":[]}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Simple local storage cache with support for auto-expiration.
3
+ * Note that this works in the browser context only because nodejs does not
4
+ * have local storage.
5
+ */
6
+ export declare class LocalStorageCache {
7
+ static setValue<T>(key: string, value: T, expireDeltaMs: number): void;
8
+ static getValue<T>(key: string, logError?: boolean): T | undefined;
9
+ }
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/localStorageCache.ts"],"sourcesContent":["/**\n * Simple local storage cache with support for auto-expiration.\n * Note that this works in the browser context only because nodejs does not\n * have local storage.\n */\nexport class LocalStorageCache {\n public static setValue<T>(key: string, value: T, expireDeltaMs: number) {\n const expireMs = Date.now() + expireDeltaMs;\n const valueStr = JSON.stringify({ value, expireMs });\n\n globalThis.localStorage.setItem(key, valueStr);\n }\n\n public static getValue<T>(key: string, logError = true): T | undefined {\n const jsonStr = globalThis.localStorage.getItem(key);\n if (!jsonStr) return undefined;\n\n try {\n const obj: { value: T; expireMs: number } | undefined =\n JSON.parse(jsonStr);\n if (\n !obj ||\n typeof obj !== \"object\" ||\n !(\"value\" in obj) ||\n !(\"expireMs\" in obj)\n ) {\n globalThis.localStorage.removeItem(key);\n return undefined;\n }\n\n if (Date.now() >= obj.expireMs) {\n globalThis.localStorage.removeItem(key);\n return undefined;\n }\n\n return obj.value;\n } catch (e) {\n if (logError) {\n console.error(`Found invalid storage value: ${key}=${jsonStr}:`, e);\n }\n globalThis.localStorage.removeItem(key);\n return undefined;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKO,IAAM,oBAAN,MAAwB;AAAA,EAC7B,OAAc,SAAY,KAAa,OAAU,eAAuB;AACtE,UAAM,WAAW,KAAK,IAAI,IAAI;AAC9B,UAAM,WAAW,KAAK,UAAU,EAAE,OAAO,SAAS,CAAC;AAEnD,eAAW,aAAa,QAAQ,KAAK,QAAQ;AAAA,EAC/C;AAAA,EAEA,OAAc,SAAY,KAAa,WAAW,MAAqB;AACrE,UAAM,UAAU,WAAW,aAAa,QAAQ,GAAG;AACnD,QAAI,CAAC,QAAS,QAAO;AAErB,QAAI;AACF,YAAM,MACJ,KAAK,MAAM,OAAO;AACpB,UACE,CAAC,OACD,OAAO,QAAQ,YACf,EAAE,WAAW,QACb,EAAE,cAAc,MAChB;AACA,mBAAW,aAAa,WAAW,GAAG;AACtC,eAAO;AAAA,MACT;AAEA,UAAI,KAAK,IAAI,KAAK,IAAI,UAAU;AAC9B,mBAAW,aAAa,WAAW,GAAG;AACtC,eAAO;AAAA,MACT;AAEA,aAAO,IAAI;AAAA,IACb,SAAS,GAAG;AACV,UAAI,UAAU;AACZ,gBAAQ,MAAM,gCAAgC,GAAG,IAAI,OAAO,KAAK,CAAC;AAAA,MACpE;AACA,iBAAW,aAAa,WAAW,GAAG;AACtC,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/localStorageCache.ts"],"sourcesContent":["/**\n * Simple local storage cache with support for auto-expiration.\n * Note that this works in the browser context only because nodejs does not\n * have local storage.\n */\nexport class LocalStorageCache {\n public static setValue<T>(key: string, value: T, expireDeltaMs: number) {\n const expireMs = Date.now() + expireDeltaMs;\n const valueStr = JSON.stringify({ value, expireMs });\n\n globalThis.localStorage.setItem(key, valueStr);\n }\n\n public static getValue<T>(key: string, logError = true): T | undefined {\n const jsonStr = globalThis.localStorage.getItem(key);\n if (!jsonStr) return undefined;\n\n try {\n const obj: { value: T; expireMs: number } | undefined =\n JSON.parse(jsonStr);\n if (\n !obj ||\n typeof obj !== \"object\" ||\n !(\"value\" in obj) ||\n !(\"expireMs\" in obj)\n ) {\n globalThis.localStorage.removeItem(key);\n return undefined;\n }\n\n if (Date.now() >= obj.expireMs) {\n globalThis.localStorage.removeItem(key);\n return undefined;\n }\n\n return obj.value;\n } catch (e) {\n if (logError) {\n console.error(`Found invalid storage value: ${key}=${jsonStr}:`, e);\n }\n globalThis.localStorage.removeItem(key);\n return undefined;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKO,IAAM,oBAAN,MAAwB;AAAA,EAC7B,OAAc,SAAY,KAAa,OAAU,eAAuB;AACtE,UAAM,WAAW,KAAK,IAAI,IAAI;AAC9B,UAAM,WAAW,KAAK,UAAU,EAAE,OAAO,SAAS,CAAC;AAEnD,eAAW,aAAa,QAAQ,KAAK,QAAQ;AAAA,EAC/C;AAAA,EAEA,OAAc,SAAY,KAAa,WAAW,MAAqB;AACrE,UAAM,UAAU,WAAW,aAAa,QAAQ,GAAG;AACnD,QAAI,CAAC,QAAS,QAAO;AAErB,QAAI;AACF,YAAM,MACJ,KAAK,MAAM,OAAO;AACpB,UACE,CAAC,OACD,OAAO,QAAQ,YACf,EAAE,WAAW,QACb,EAAE,cAAc,MAChB;AACA,mBAAW,aAAa,WAAW,GAAG;AACtC,eAAO;AAAA,MACT;AAEA,UAAI,KAAK,IAAI,KAAK,IAAI,UAAU;AAC9B,mBAAW,aAAa,WAAW,GAAG;AACtC,eAAO;AAAA,MACT;AAEA,aAAO,IAAI;AAAA,IACb,SAAS,GAAG;AACV,UAAI,UAAU;AACZ,gBAAQ,MAAM,gCAAgC,GAAG,IAAI,OAAO,KAAK,CAAC;AAAA,MACpE;AACA,iBAAW,aAAa,WAAW,GAAG;AACtC,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
package/nonNil.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Type asserts that `t` is neither null nor undefined.
3
+ * Throws an error if `t` is null or undefined.
4
+ */
5
+ export declare function nonNil<T>(t: T | null | undefined, varName?: string): T;
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/nonNil.ts"],"sourcesContent":["/**\n * Type asserts that `t` is neither null nor undefined.\n * Throws an error if `t` is null or undefined.\n */\nexport function nonNil<T>(t: T | null | undefined, varName = \"variable\"): T {\n if (t === null || t === undefined) {\n throw new Error(`Missing ${varName}`);\n }\n return t;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIO,SAAS,OAAU,GAAyB,UAAU,YAAe;AAC1E,MAAI,MAAM,QAAQ,MAAM,QAAW;AACjC,UAAM,IAAI,MAAM,WAAW,OAAO,EAAE;AAAA,EACtC;AACA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../src/nonNil.ts"],"sourcesContent":["/**\n * Type asserts that `t` is neither null nor undefined.\n * Throws an error if `t` is null or undefined.\n */\nexport function nonNil<T>(t: T | null | undefined, varName = \"variable\"): T {\n if (t === null || t === undefined) {\n throw new Error(`Missing ${varName}`);\n }\n return t;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIO,SAAS,OAAU,GAAyB,UAAU,YAAe;AAC1E,MAAI,MAAM,QAAQ,MAAM,QAAW;AACjC,UAAM,IAAI,MAAM,WAAW,OAAO,EAAE;AAAA,EACtC;AACA,SAAO;AACT;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@choksheak/ts-utils",
3
3
  "license": "The Unlicense",
4
- "version": "0.1.3",
4
+ "version": "0.1.5",
5
5
  "description": "Random Typescript utilities with support for full tree-shaking",
6
6
  "private": false,
7
7
  "scripts": {
@@ -9,14 +9,12 @@
9
9
  "format": "prettier --write \"**/*.{js,ts}\"",
10
10
  "test": "jest",
11
11
  "test:watch": "jest --watch",
12
+ "clean": "rm -rf dist",
13
+ "ci": "npm run clean && npm run lint && npm run format && npm run test",
12
14
  "build": "tsup src/*",
13
- "pub": "npm run build && npm publish --access=public"
15
+ "gen-types": "tsc --emitDeclarationOnly --declaration",
16
+ "pub": "scripts/publish.sh"
14
17
  },
15
- "files": [
16
- "README.md",
17
- "dist",
18
- "src"
19
- ],
20
18
  "devDependencies": {
21
19
  "@eslint/js": "^9.14.0",
22
20
  "@jest/globals": "^29.7.0",
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Returns 0 if the string is not a valid number.
3
+ */
4
+ export declare function safeParseInt(s: string, logError?: boolean): number;
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/safeParseInt.ts"],"sourcesContent":["/**\n * Returns 0 if the string is not a valid number.\n */\nexport function safeParseInt(s: string, logError = false): number {\n const i = Number(s);\n\n if (isNaN(i)) {\n if (logError) {\n console.error(`Not a number: \"${s}\"`);\n }\n return 0;\n }\n\n return Math.floor(i);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,SAAS,aAAa,GAAW,WAAW,OAAe;AAChE,QAAM,IAAI,OAAO,CAAC;AAElB,MAAI,MAAM,CAAC,GAAG;AACZ,QAAI,UAAU;AACZ,cAAQ,MAAM,kBAAkB,CAAC,GAAG;AAAA,IACtC;AACA,WAAO;AAAA,EACT;AAEA,SAAO,KAAK,MAAM,CAAC;AACrB;","names":[]}
1
+ {"version":3,"sources":["../src/safeParseInt.ts"],"sourcesContent":["/**\n * Returns 0 if the string is not a valid number.\n */\nexport function safeParseInt(s: string, logError = false): number {\n const i = Number(s);\n\n if (isNaN(i)) {\n if (logError) {\n console.error(`Not a number: \"${s}\"`);\n }\n return 0;\n }\n\n return Math.floor(i);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,SAAS,aAAa,GAAW,WAAW,OAAe;AAChE,QAAM,IAAI,OAAO,CAAC;AAElB,MAAI,MAAM,CAAC,GAAG;AACZ,QAAI,UAAU;AACZ,cAAQ,MAAM,kBAAkB,CAAC,GAAG;AAAA,IACtC;AACA,WAAO;AAAA,EACT;AAEA,SAAO,KAAK,MAAM,CAAC;AACrB;","names":[]}
@@ -1,96 +0,0 @@
1
- // src/localStorageCache.ts
2
- var LocalStorageCache = class {
3
- static setValue(key, value, expireDeltaMs) {
4
- const expireMs = Date.now() + expireDeltaMs;
5
- const valueStr = JSON.stringify({ value, expireMs });
6
- globalThis.localStorage.setItem(key, valueStr);
7
- }
8
- static getValue(key, logError = true) {
9
- const jsonStr = globalThis.localStorage.getItem(key);
10
- if (!jsonStr) return void 0;
11
- try {
12
- const obj = JSON.parse(jsonStr);
13
- if (!obj || typeof obj !== "object" || !("value" in obj) || !("expireMs" in obj)) {
14
- globalThis.localStorage.removeItem(key);
15
- return void 0;
16
- }
17
- if (Date.now() >= obj.expireMs) {
18
- globalThis.localStorage.removeItem(key);
19
- return void 0;
20
- }
21
- return obj.value;
22
- } catch (e) {
23
- if (logError) {
24
- console.error(`Found invalid storage value: ${key}=${jsonStr}:`, e);
25
- }
26
- globalThis.localStorage.removeItem(key);
27
- return void 0;
28
- }
29
- }
30
- };
31
-
32
- // src/localStorageCache.test.ts
33
- describe("LocalStorageCache", () => {
34
- beforeEach(() => {
35
- global.localStorage.clear();
36
- jest.useFakeTimers();
37
- });
38
- afterEach(() => {
39
- jest.useRealTimers();
40
- });
41
- describe("setValue", () => {
42
- it("should store a value with an expiration time", () => {
43
- const key = "testKey";
44
- const value = "testValue";
45
- const expireDeltaMs = 1e3;
46
- LocalStorageCache.setValue(key, value, expireDeltaMs);
47
- const storedValue = JSON.parse(
48
- global.localStorage.getItem(key)
49
- );
50
- expect(storedValue.value).toBe(value);
51
- expect(typeof storedValue.expireMs).toBe("number");
52
- });
53
- });
54
- describe("getValue", () => {
55
- it("should retrieve a stored value if not expired", () => {
56
- const key = "testKey";
57
- const value = "testValue";
58
- const expireDeltaMs = 1e3;
59
- LocalStorageCache.setValue(key, value, expireDeltaMs);
60
- expect(LocalStorageCache.getValue(key)).toBe(value);
61
- });
62
- it("should return undefined if the value has expired", () => {
63
- const key = "testKey";
64
- const value = "testValue";
65
- const expireDeltaMs = 1e3;
66
- LocalStorageCache.setValue(key, value, expireDeltaMs);
67
- jest.advanceTimersByTime(2e3);
68
- expect(LocalStorageCache.getValue(key)).toBeUndefined();
69
- expect(global.localStorage.getItem(key)).toBeNull();
70
- });
71
- it("should return undefined if the key does not exist", () => {
72
- expect(LocalStorageCache.getValue("nonexistentKey")).toBeUndefined();
73
- });
74
- it("should return undefined if the stored data is invalid and remove it from storage", () => {
75
- const consoleError = jest.spyOn(console, "error").mockReturnValue();
76
- const key = "testKey";
77
- global.localStorage.setItem(key, "invalidJSON");
78
- expect(LocalStorageCache.getValue(key)).toBeUndefined();
79
- expect(global.localStorage.getItem(key)).toBeNull();
80
- expect(consoleError).toHaveBeenCalledTimes(1);
81
- expect(consoleError.mock.calls[0][0]).toBe(
82
- "Found invalid storage value: testKey=invalidJSON:"
83
- );
84
- });
85
- it("should return undefined if stored data is missing required fields and remove it from storage", () => {
86
- const key = "testKey";
87
- global.localStorage.setItem(
88
- key,
89
- JSON.stringify({ someOtherField: "data" })
90
- );
91
- expect(LocalStorageCache.getValue(key)).toBeUndefined();
92
- expect(global.localStorage.getItem(key)).toBeNull();
93
- });
94
- });
95
- });
96
- //# sourceMappingURL=localStorageCache.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/localStorageCache.ts","../src/localStorageCache.test.ts"],"sourcesContent":["/**\n * Simple local storage cache with support for auto-expiration.\n * Note that this works in the browser context only because nodejs does not\n * have local storage.\n */\nexport class LocalStorageCache {\n public static setValue<T>(key: string, value: T, expireDeltaMs: number) {\n const expireMs = Date.now() + expireDeltaMs;\n const valueStr = JSON.stringify({ value, expireMs });\n\n globalThis.localStorage.setItem(key, valueStr);\n }\n\n public static getValue<T>(key: string, logError = true): T | undefined {\n const jsonStr = globalThis.localStorage.getItem(key);\n if (!jsonStr) return undefined;\n\n try {\n const obj: { value: T; expireMs: number } | undefined =\n JSON.parse(jsonStr);\n if (\n !obj ||\n typeof obj !== \"object\" ||\n !(\"value\" in obj) ||\n !(\"expireMs\" in obj)\n ) {\n globalThis.localStorage.removeItem(key);\n return undefined;\n }\n\n if (Date.now() >= obj.expireMs) {\n globalThis.localStorage.removeItem(key);\n return undefined;\n }\n\n return obj.value;\n } catch (e) {\n if (logError) {\n console.error(`Found invalid storage value: ${key}=${jsonStr}:`, e);\n }\n globalThis.localStorage.removeItem(key);\n return undefined;\n }\n }\n}\n","import { LocalStorageCache } from \"./localStorageCache\";\n\ndescribe(\"LocalStorageCache\", () => {\n beforeEach(() => {\n // Clear localStorage before each test\n global.localStorage.clear();\n jest.useFakeTimers(); // Use fake timers to control Date.now\n });\n\n afterEach(() => {\n jest.useRealTimers(); // Restore real timers after each test\n });\n\n describe(\"setValue\", () => {\n it(\"should store a value with an expiration time\", () => {\n const key = \"testKey\";\n const value = \"testValue\";\n const expireDeltaMs = 1000; // 1 second\n\n LocalStorageCache.setValue(key, value, expireDeltaMs);\n\n const storedValue = JSON.parse(\n global.localStorage.getItem(key) as string,\n );\n expect(storedValue.value).toBe(value);\n expect(typeof storedValue.expireMs).toBe(\"number\");\n });\n });\n\n describe(\"getValue\", () => {\n it(\"should retrieve a stored value if not expired\", () => {\n const key = \"testKey\";\n const value = \"testValue\";\n const expireDeltaMs = 1000; // 1 second\n\n LocalStorageCache.setValue(key, value, expireDeltaMs);\n\n expect(LocalStorageCache.getValue(key)).toBe(value);\n });\n\n it(\"should return undefined if the value has expired\", () => {\n const key = \"testKey\";\n const value = \"testValue\";\n const expireDeltaMs = 1000; // 1 second\n\n LocalStorageCache.setValue(key, value, expireDeltaMs);\n\n // Advance time by 2 seconds to ensure expiration\n jest.advanceTimersByTime(2000);\n\n expect(LocalStorageCache.getValue(key)).toBeUndefined();\n expect(global.localStorage.getItem(key)).toBeNull(); // Ensure item is removed\n });\n\n it(\"should return undefined if the key does not exist\", () => {\n expect(LocalStorageCache.getValue(\"nonexistentKey\")).toBeUndefined();\n });\n\n it(\"should return undefined if the stored data is invalid and remove it from storage\", () => {\n const consoleError = jest.spyOn(console, \"error\").mockReturnValue();\n\n const key = \"testKey\";\n // Set an invalid JSON format in localStorage\n global.localStorage.setItem(key, \"invalidJSON\");\n\n expect(LocalStorageCache.getValue(key)).toBeUndefined();\n expect(global.localStorage.getItem(key)).toBeNull(); // Ensure item is removed\n expect(consoleError).toHaveBeenCalledTimes(1);\n expect(consoleError.mock.calls[0][0]).toBe(\n \"Found invalid storage value: testKey=invalidJSON:\",\n );\n });\n\n it(\"should return undefined if stored data is missing required fields and remove it from storage\", () => {\n const key = \"testKey\";\n // Set JSON with missing fields\n global.localStorage.setItem(\n key,\n JSON.stringify({ someOtherField: \"data\" }),\n );\n\n expect(LocalStorageCache.getValue(key)).toBeUndefined();\n expect(global.localStorage.getItem(key)).toBeNull(); // Ensure item is removed\n });\n });\n});\n"],"mappings":"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","names":[]}
@@ -1,86 +0,0 @@
1
- import { LocalStorageCache } from "./localStorageCache";
2
-
3
- describe("LocalStorageCache", () => {
4
- beforeEach(() => {
5
- // Clear localStorage before each test
6
- global.localStorage.clear();
7
- jest.useFakeTimers(); // Use fake timers to control Date.now
8
- });
9
-
10
- afterEach(() => {
11
- jest.useRealTimers(); // Restore real timers after each test
12
- });
13
-
14
- describe("setValue", () => {
15
- it("should store a value with an expiration time", () => {
16
- const key = "testKey";
17
- const value = "testValue";
18
- const expireDeltaMs = 1000; // 1 second
19
-
20
- LocalStorageCache.setValue(key, value, expireDeltaMs);
21
-
22
- const storedValue = JSON.parse(
23
- global.localStorage.getItem(key) as string,
24
- );
25
- expect(storedValue.value).toBe(value);
26
- expect(typeof storedValue.expireMs).toBe("number");
27
- });
28
- });
29
-
30
- describe("getValue", () => {
31
- it("should retrieve a stored value if not expired", () => {
32
- const key = "testKey";
33
- const value = "testValue";
34
- const expireDeltaMs = 1000; // 1 second
35
-
36
- LocalStorageCache.setValue(key, value, expireDeltaMs);
37
-
38
- expect(LocalStorageCache.getValue(key)).toBe(value);
39
- });
40
-
41
- it("should return undefined if the value has expired", () => {
42
- const key = "testKey";
43
- const value = "testValue";
44
- const expireDeltaMs = 1000; // 1 second
45
-
46
- LocalStorageCache.setValue(key, value, expireDeltaMs);
47
-
48
- // Advance time by 2 seconds to ensure expiration
49
- jest.advanceTimersByTime(2000);
50
-
51
- expect(LocalStorageCache.getValue(key)).toBeUndefined();
52
- expect(global.localStorage.getItem(key)).toBeNull(); // Ensure item is removed
53
- });
54
-
55
- it("should return undefined if the key does not exist", () => {
56
- expect(LocalStorageCache.getValue("nonexistentKey")).toBeUndefined();
57
- });
58
-
59
- it("should return undefined if the stored data is invalid and remove it from storage", () => {
60
- const consoleError = jest.spyOn(console, "error").mockReturnValue();
61
-
62
- const key = "testKey";
63
- // Set an invalid JSON format in localStorage
64
- global.localStorage.setItem(key, "invalidJSON");
65
-
66
- expect(LocalStorageCache.getValue(key)).toBeUndefined();
67
- expect(global.localStorage.getItem(key)).toBeNull(); // Ensure item is removed
68
- expect(consoleError).toHaveBeenCalledTimes(1);
69
- expect(consoleError.mock.calls[0][0]).toBe(
70
- "Found invalid storage value: testKey=invalidJSON:",
71
- );
72
- });
73
-
74
- it("should return undefined if stored data is missing required fields and remove it from storage", () => {
75
- const key = "testKey";
76
- // Set JSON with missing fields
77
- global.localStorage.setItem(
78
- key,
79
- JSON.stringify({ someOtherField: "data" }),
80
- );
81
-
82
- expect(LocalStorageCache.getValue(key)).toBeUndefined();
83
- expect(global.localStorage.getItem(key)).toBeNull(); // Ensure item is removed
84
- });
85
- });
86
- });