@stryke/helpers 0.9.35 → 0.9.36
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/CHANGELOG.md +8 -0
- package/dist/arg-identity.cjs +17 -1
- package/dist/arg-identity.mjs +16 -1
- package/dist/arg-identity.mjs.map +1 -1
- package/dist/convert/src/to-string-key.cjs +15 -1
- package/dist/convert/src/to-string-key.mjs +14 -1
- package/dist/convert/src/to-string-key.mjs.map +1 -1
- package/dist/debounce.cjs +62 -1
- package/dist/debounce.mjs +61 -1
- package/dist/debounce.mjs.map +1 -1
- package/dist/deep-clone.cjs +113 -1
- package/dist/deep-clone.mjs +113 -1
- package/dist/deep-clone.mjs.map +1 -1
- package/dist/deep-merge.cjs +60 -1
- package/dist/deep-merge.mjs +60 -1
- package/dist/deep-merge.mjs.map +1 -1
- package/dist/delay.cjs +90 -1
- package/dist/delay.mjs +89 -1
- package/dist/delay.mjs.map +1 -1
- package/dist/errors.cjs +24 -1
- package/dist/errors.mjs +22 -1
- package/dist/errors.mjs.map +1 -1
- package/dist/filter-empty.cjs +7 -1
- package/dist/filter-empty.mjs +7 -1
- package/dist/filter-empty.mjs.map +1 -1
- package/dist/flatten-object.cjs +48 -1
- package/dist/flatten-object.mjs +48 -1
- package/dist/flatten-object.mjs.map +1 -1
- package/dist/get-field.cjs +54 -1
- package/dist/get-field.mjs +54 -1
- package/dist/get-field.mjs.map +1 -1
- package/dist/get-ordered-by.cjs +54 -1
- package/dist/get-ordered-by.mjs +53 -1
- package/dist/get-ordered-by.mjs.map +1 -1
- package/dist/get-unique.cjs +37 -1
- package/dist/get-unique.mjs +35 -1
- package/dist/get-unique.mjs.map +1 -1
- package/dist/identity.cjs +21 -1
- package/dist/identity.mjs +20 -1
- package/dist/identity.mjs.map +1 -1
- package/dist/index.cjs +71 -1
- package/dist/index.mjs +33 -1
- package/dist/is-equal.cjs +87 -1
- package/dist/is-equal.mjs +87 -1
- package/dist/is-equal.mjs.map +1 -1
- package/dist/lru-cache.cjs +190 -1
- package/dist/lru-cache.mjs +189 -1
- package/dist/lru-cache.mjs.map +1 -1
- package/dist/match-sorter.cjs +264 -1
- package/dist/match-sorter.mjs +262 -1
- package/dist/match-sorter.mjs.map +1 -1
- package/dist/memoize.cjs +20 -1
- package/dist/memoize.mjs +19 -1
- package/dist/memoize.mjs.map +1 -1
- package/dist/mutex.cjs +80 -1
- package/dist/mutex.mjs +80 -1
- package/dist/mutex.mjs.map +1 -1
- package/dist/noop.cjs +24 -1
- package/dist/noop.mjs +22 -1
- package/dist/noop.mjs.map +1 -1
- package/dist/omit.cjs +30 -1
- package/dist/omit.mjs +29 -1
- package/dist/omit.mjs.map +1 -1
- package/dist/once.cjs +27 -1
- package/dist/once.mjs +25 -1
- package/dist/once.mjs.map +1 -1
- package/dist/pick.cjs +30 -1
- package/dist/pick.mjs +29 -1
- package/dist/pick.mjs.map +1 -1
- package/dist/remove-accents.cjs +416 -1
- package/dist/remove-accents.mjs +414 -1
- package/dist/remove-accents.mjs.map +1 -1
- package/dist/remove-empty-items.cjs +12 -1
- package/dist/remove-empty-items.mjs +11 -1
- package/dist/remove-empty-items.mjs.map +1 -1
- package/dist/semaphore.cjs +105 -1
- package/dist/semaphore.mjs +104 -1
- package/dist/semaphore.mjs.map +1 -1
- package/dist/set-field.cjs +30 -1
- package/dist/set-field.mjs +30 -1
- package/dist/set-field.mjs.map +1 -1
- package/dist/throttle.cjs +43 -1
- package/dist/throttle.mjs +42 -1
- package/dist/throttle.mjs.map +1 -1
- package/dist/timeout.cjs +18 -1
- package/dist/timeout.mjs +18 -1
- package/dist/timeout.mjs.map +1 -1
- package/dist/to-deep-key.cjs +54 -1
- package/dist/to-deep-key.mjs +53 -1
- package/dist/to-deep-key.mjs.map +1 -1
- package/dist/to-path.cjs +43 -1
- package/dist/to-path.mjs +42 -1
- package/dist/to-path.mjs.map +1 -1
- package/dist/type-checks/src/get-object-tag.cjs +15 -1
- package/dist/type-checks/src/get-object-tag.mjs +14 -1
- package/dist/type-checks/src/get-object-tag.mjs.map +1 -1
- package/dist/type-checks/src/is-deep-key.cjs +28 -1
- package/dist/type-checks/src/is-deep-key.mjs +27 -1
- package/dist/type-checks/src/is-deep-key.mjs.map +1 -1
- package/dist/type-checks/src/is-empty.cjs +20 -1
- package/dist/type-checks/src/is-empty.mjs +20 -1
- package/dist/type-checks/src/is-empty.mjs.map +1 -1
- package/dist/type-checks/src/is-function.cjs +25 -1
- package/dist/type-checks/src/is-function.mjs +25 -1
- package/dist/type-checks/src/is-function.mjs.map +1 -1
- package/dist/type-checks/src/is-mergeable-object.cjs +14 -1
- package/dist/type-checks/src/is-mergeable-object.mjs +14 -1
- package/dist/type-checks/src/is-mergeable-object.mjs.map +1 -1
- package/dist/type-checks/src/is-non-null-object.cjs +16 -1
- package/dist/type-checks/src/is-non-null-object.mjs +16 -1
- package/dist/type-checks/src/is-non-null-object.mjs.map +1 -1
- package/dist/type-checks/src/is-null.cjs +12 -1
- package/dist/type-checks/src/is-null.mjs +11 -1
- package/dist/type-checks/src/is-null.mjs.map +1 -1
- package/dist/type-checks/src/is-number.cjs +18 -1
- package/dist/type-checks/src/is-number.mjs +17 -1
- package/dist/type-checks/src/is-number.mjs.map +1 -1
- package/dist/type-checks/src/is-object-index.cjs +19 -1
- package/dist/type-checks/src/is-object-index.mjs +18 -1
- package/dist/type-checks/src/is-object-index.mjs.map +1 -1
- package/dist/type-checks/src/is-object.cjs +19 -1
- package/dist/type-checks/src/is-object.mjs +19 -1
- package/dist/type-checks/src/is-object.mjs.map +1 -1
- package/dist/type-checks/src/is-plain-object.cjs +63 -1
- package/dist/type-checks/src/is-plain-object.mjs +63 -1
- package/dist/type-checks/src/is-plain-object.mjs.map +1 -1
- package/dist/type-checks/src/is-primitive.cjs +12 -1
- package/dist/type-checks/src/is-primitive.mjs +11 -1
- package/dist/type-checks/src/is-primitive.mjs.map +1 -1
- package/dist/type-checks/src/is-react-element.cjs +8 -1
- package/dist/type-checks/src/is-react-element.mjs +7 -1
- package/dist/type-checks/src/is-react-element.mjs.map +1 -1
- package/dist/type-checks/src/is-set-string.cjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs.map +1 -1
- package/dist/type-checks/src/is-set.cjs +19 -1
- package/dist/type-checks/src/is-set.mjs +19 -1
- package/dist/type-checks/src/is-set.mjs.map +1 -1
- package/dist/type-checks/src/is-string.cjs +12 -1
- package/dist/type-checks/src/is-string.mjs +11 -1
- package/dist/type-checks/src/is-string.mjs.map +1 -1
- package/dist/type-checks/src/is-typed-array.cjs +8 -1
- package/dist/type-checks/src/is-typed-array.mjs +7 -1
- package/dist/type-checks/src/is-typed-array.mjs.map +1 -1
- package/dist/type-checks/src/is-undefined.cjs +8 -1
- package/dist/type-checks/src/is-undefined.mjs +7 -1
- package/dist/type-checks/src/is-undefined.mjs.map +1 -1
- package/dist/type-checks/src/property-exists.cjs +30 -1
- package/dist/type-checks/src/property-exists.mjs +30 -1
- package/dist/type-checks/src/property-exists.mjs.map +1 -1
- package/dist/unflatten-object.cjs +38 -1
- package/dist/unflatten-object.mjs +38 -1
- package/dist/unflatten-object.mjs.map +1 -1
- package/dist/union.cjs +28 -1
- package/dist/union.mjs +28 -1
- package/dist/union.mjs.map +1 -1
- package/dist/with-timeout.cjs +28 -1
- package/dist/with-timeout.mjs +28 -1
- package/dist/with-timeout.mjs.map +1 -1
- package/package.json +2 -2
package/dist/is-equal.mjs
CHANGED
|
@@ -1,2 +1,88 @@
|
|
|
1
|
-
import{isSet
|
|
1
|
+
import { isSet } from "./type-checks/src/is-set.mjs";
|
|
2
|
+
import { isSetString } from "./type-checks/src/is-set-string.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/is-equal.ts
|
|
5
|
+
const hasMap = typeof Map === "function";
|
|
6
|
+
const hasSet = typeof Set === "function";
|
|
7
|
+
const hasArrayBuffer = typeof ArrayBuffer === "function" && ArrayBuffer.isView !== void 0;
|
|
8
|
+
function equal(a, b) {
|
|
9
|
+
if (a === b) return true;
|
|
10
|
+
if (a && b && typeof a === "object" && typeof b === "object") {
|
|
11
|
+
if (a.constructor !== b.constructor) return false;
|
|
12
|
+
let length;
|
|
13
|
+
if (Array.isArray(a)) {
|
|
14
|
+
length = a.length;
|
|
15
|
+
if (length != b.length) return false;
|
|
16
|
+
for (let i = length; i-- !== 0;) if (!equal(a[i], b[i])) return false;
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
let it;
|
|
20
|
+
if (hasMap && a instanceof Map && b instanceof Map) {
|
|
21
|
+
if (a.size !== b.size) return false;
|
|
22
|
+
it = a.entries();
|
|
23
|
+
let i;
|
|
24
|
+
while (!(i = it.next()).done) if (!b.has(i.value[0])) return false;
|
|
25
|
+
it = a.entries();
|
|
26
|
+
while (!(i = it.next()).done) if (!equal(i.value[1], b.get(i.value[0]))) return false;
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
if (hasSet && a instanceof Set && b instanceof Set) {
|
|
30
|
+
if (a.size !== b.size) return false;
|
|
31
|
+
it = a.entries();
|
|
32
|
+
let i;
|
|
33
|
+
while (!(i = it.next()).done) if (!b.has(i.value[0])) return false;
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
if (Array.isArray(a) && Array.isArray(b) && hasArrayBuffer && ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
|
|
37
|
+
length = a.length;
|
|
38
|
+
if (length != b.length) return false;
|
|
39
|
+
for (let i = length; i-- !== 0;) if (a[i] !== b[i]) return false;
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
|
|
43
|
+
if (a.valueOf !== Object.prototype.valueOf && typeof a.valueOf === "function" && typeof b.valueOf === "function") return a.valueOf() === b.valueOf();
|
|
44
|
+
if (a.toString !== Object.prototype.toString && typeof a.toString === "function" && typeof b.toString === "function") return a.toString() === b.toString();
|
|
45
|
+
const keys = Object.keys(a);
|
|
46
|
+
length = keys.length;
|
|
47
|
+
if (length !== Object.keys(b).length) return false;
|
|
48
|
+
for (let i = length; i-- !== 0;) if (!isSet(i) || !isSetString(keys[i]) || !Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
|
|
49
|
+
for (let i = length; i-- !== 0;) {
|
|
50
|
+
if (Array.isArray(keys) && (keys[i] === "_owner" || keys[i] === "__v" || keys[i] === "__o") && a.$$typeof) continue;
|
|
51
|
+
if (!isSet(i) || !isSetString(keys[i]) || !equal(a[keys[i]], b[keys[i]])) return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
return a !== a && b !== b;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Checks if two values are equal, including support for `Date`, `RegExp`, and deep object comparison.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* isEqual(1, 1); // true
|
|
63
|
+
* isEqual({ a: 1 }, { a: 1 }); // true
|
|
64
|
+
* isEqual(/abc/g, /abc/g); // true
|
|
65
|
+
* isEqual(new Date('2020-01-01'), new Date('2020-01-01')); // true
|
|
66
|
+
* isEqual([1, 2, 3], [1, 2, 3]); // true
|
|
67
|
+
* isEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 2 } }); // true
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @param a - The first value to compare.
|
|
71
|
+
* @param b - The second value to compare.
|
|
72
|
+
* @returns `true` if the values are equal, otherwise `false`.
|
|
73
|
+
*/
|
|
74
|
+
function isEqual(a, b) {
|
|
75
|
+
try {
|
|
76
|
+
return equal(a, b);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (/stack|recursion/i.test(error?.message || "")) {
|
|
79
|
+
console.warn("isEqual cannot handle circular refs");
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
//#endregion
|
|
87
|
+
export { isEqual };
|
|
2
88
|
//# sourceMappingURL=is-equal.mjs.map
|
package/dist/is-equal.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-equal.mjs","names":[],"sources":["../src/is-equal.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 License, and is\n free for commercial and private use. For more information, please visit\n our licensing page.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://stormsoftware.com/projects/stryke/docs\n Contact: https://stormsoftware.com/contact\n License: https://stormsoftware.com/projects/stryke/license\n\n ------------------------------------------------------------------- */\n\nimport { isSet } from \"@stryke/type-checks/is-set\";\nimport { isSetString } from \"@stryke/type-checks/is-set-string\";\n\nconst hasMap = typeof Map === \"function\";\nconst hasSet = typeof Set === \"function\";\nconst hasArrayBuffer =\n typeof ArrayBuffer === \"function\" && ArrayBuffer.isView !== undefined;\n\nfunction equal(a: any, b: any) {\n if (a === b) {\n return true;\n }\n\n if (a && b && typeof a === \"object\" && typeof b === \"object\") {\n if (a.constructor !== b.constructor) {\n return false;\n }\n\n let length;\n if (Array.isArray(a)) {\n length = a.length;\n\n if (length != b.length) return false;\n for (let i = length; i-- !== 0; ) if (!equal(a[i], b[i])) return false;\n return true;\n }\n\n let it;\n if (hasMap && a instanceof Map && b instanceof Map) {\n if (a.size !== b.size) return false;\n it = a.entries();\n let i;\n while (!(i = it.next()).done) if (!b.has(i.value[0])) return false;\n it = a.entries();\n while (!(i = it.next()).done) {\n if (!equal(i.value[1], b.get(i.value[0]))) return false;\n }\n return true;\n }\n\n if (hasSet && a instanceof Set && b instanceof Set) {\n if (a.size !== b.size) return false;\n it = a.entries();\n let i;\n while (!(i = it.next()).done) if (!b.has(i.value[0])) return false;\n return true;\n }\n\n if (\n Array.isArray(a) &&\n Array.isArray(b) &&\n hasArrayBuffer &&\n ArrayBuffer.isView(a) &&\n ArrayBuffer.isView(b)\n ) {\n length = a.length;\n\n if (length != b.length) return false;\n for (let i = length; i-- !== 0; ) if (a[i] !== b[i]) return false;\n return true;\n }\n\n if (a.constructor === RegExp) {\n return a.source === b.source && a.flags === b.flags;\n }\n\n if (\n a.valueOf !== Object.prototype.valueOf &&\n typeof a.valueOf === \"function\" &&\n typeof b.valueOf === \"function\"\n ) {\n // eslint-disable-next-line ts/no-unsafe-call\n return a.valueOf() === b.valueOf();\n }\n if (\n a.toString !== Object.prototype.toString &&\n typeof a.toString === \"function\" &&\n typeof b.toString === \"function\"\n ) {\n // eslint-disable-next-line ts/no-unsafe-call\n return a.toString() === b.toString();\n }\n\n const keys = Object.keys(a);\n length = keys.length;\n if (length !== Object.keys(b).length) {\n return false;\n }\n\n for (let i = length; i-- !== 0; ) {\n if (\n !isSet(i) ||\n !isSetString(keys[i]) ||\n !Object.prototype.hasOwnProperty.call(b, keys[i] as any)\n ) {\n return false;\n }\n }\n\n for (let i = length; i-- !== 0; ) {\n if (\n Array.isArray(keys) &&\n (keys[i] === \"_owner\" || keys[i] === \"__v\" || keys[i] === \"__o\") &&\n a.$$typeof\n ) {\n continue;\n }\n\n if (\n !isSet(i) ||\n !isSetString(keys[i]) ||\n !equal(a[keys[i] as any], b[keys[i] as any])\n ) {\n return false;\n }\n }\n\n return true;\n }\n\n // eslint-disable-next-line no-self-compare\n return a !== a && b !== b;\n}\n\n/**\n * Checks if two values are equal, including support for `Date`, `RegExp`, and deep object comparison.\n *\n * @example\n * ```ts\n * isEqual(1, 1); // true\n * isEqual({ a: 1 }, { a: 1 }); // true\n * isEqual(/abc/g, /abc/g); // true\n * isEqual(new Date('2020-01-01'), new Date('2020-01-01')); // true\n * isEqual([1, 2, 3], [1, 2, 3]); // true\n * isEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 2 } }); // true\n * ```\n *\n * @param a - The first value to compare.\n * @param b - The second value to compare.\n * @returns `true` if the values are equal, otherwise `false`.\n */\nexport function isEqual(a: any, b: any): boolean {\n try {\n return equal(a, b);\n } catch (error) {\n if (/stack|recursion/i.test((error as any)?.message || \"\")) {\n // eslint-disable-next-line no-console\n console.warn(\"isEqual cannot handle circular refs\");\n\n return false;\n }\n\n throw error;\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"is-equal.mjs","names":[],"sources":["../src/is-equal.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 License, and is\n free for commercial and private use. For more information, please visit\n our licensing page.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://stormsoftware.com/projects/stryke/docs\n Contact: https://stormsoftware.com/contact\n License: https://stormsoftware.com/projects/stryke/license\n\n ------------------------------------------------------------------- */\n\nimport { isSet } from \"@stryke/type-checks/is-set\";\nimport { isSetString } from \"@stryke/type-checks/is-set-string\";\n\nconst hasMap = typeof Map === \"function\";\nconst hasSet = typeof Set === \"function\";\nconst hasArrayBuffer =\n typeof ArrayBuffer === \"function\" && ArrayBuffer.isView !== undefined;\n\nfunction equal(a: any, b: any) {\n if (a === b) {\n return true;\n }\n\n if (a && b && typeof a === \"object\" && typeof b === \"object\") {\n if (a.constructor !== b.constructor) {\n return false;\n }\n\n let length;\n if (Array.isArray(a)) {\n length = a.length;\n\n if (length != b.length) return false;\n for (let i = length; i-- !== 0; ) if (!equal(a[i], b[i])) return false;\n return true;\n }\n\n let it;\n if (hasMap && a instanceof Map && b instanceof Map) {\n if (a.size !== b.size) return false;\n it = a.entries();\n let i;\n while (!(i = it.next()).done) if (!b.has(i.value[0])) return false;\n it = a.entries();\n while (!(i = it.next()).done) {\n if (!equal(i.value[1], b.get(i.value[0]))) return false;\n }\n return true;\n }\n\n if (hasSet && a instanceof Set && b instanceof Set) {\n if (a.size !== b.size) return false;\n it = a.entries();\n let i;\n while (!(i = it.next()).done) if (!b.has(i.value[0])) return false;\n return true;\n }\n\n if (\n Array.isArray(a) &&\n Array.isArray(b) &&\n hasArrayBuffer &&\n ArrayBuffer.isView(a) &&\n ArrayBuffer.isView(b)\n ) {\n length = a.length;\n\n if (length != b.length) return false;\n for (let i = length; i-- !== 0; ) if (a[i] !== b[i]) return false;\n return true;\n }\n\n if (a.constructor === RegExp) {\n return a.source === b.source && a.flags === b.flags;\n }\n\n if (\n a.valueOf !== Object.prototype.valueOf &&\n typeof a.valueOf === \"function\" &&\n typeof b.valueOf === \"function\"\n ) {\n // eslint-disable-next-line ts/no-unsafe-call\n return a.valueOf() === b.valueOf();\n }\n if (\n a.toString !== Object.prototype.toString &&\n typeof a.toString === \"function\" &&\n typeof b.toString === \"function\"\n ) {\n // eslint-disable-next-line ts/no-unsafe-call\n return a.toString() === b.toString();\n }\n\n const keys = Object.keys(a);\n length = keys.length;\n if (length !== Object.keys(b).length) {\n return false;\n }\n\n for (let i = length; i-- !== 0; ) {\n if (\n !isSet(i) ||\n !isSetString(keys[i]) ||\n !Object.prototype.hasOwnProperty.call(b, keys[i] as any)\n ) {\n return false;\n }\n }\n\n for (let i = length; i-- !== 0; ) {\n if (\n Array.isArray(keys) &&\n (keys[i] === \"_owner\" || keys[i] === \"__v\" || keys[i] === \"__o\") &&\n a.$$typeof\n ) {\n continue;\n }\n\n if (\n !isSet(i) ||\n !isSetString(keys[i]) ||\n !equal(a[keys[i] as any], b[keys[i] as any])\n ) {\n return false;\n }\n }\n\n return true;\n }\n\n // eslint-disable-next-line no-self-compare\n return a !== a && b !== b;\n}\n\n/**\n * Checks if two values are equal, including support for `Date`, `RegExp`, and deep object comparison.\n *\n * @example\n * ```ts\n * isEqual(1, 1); // true\n * isEqual({ a: 1 }, { a: 1 }); // true\n * isEqual(/abc/g, /abc/g); // true\n * isEqual(new Date('2020-01-01'), new Date('2020-01-01')); // true\n * isEqual([1, 2, 3], [1, 2, 3]); // true\n * isEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 2 } }); // true\n * ```\n *\n * @param a - The first value to compare.\n * @param b - The second value to compare.\n * @returns `true` if the values are equal, otherwise `false`.\n */\nexport function isEqual(a: any, b: any): boolean {\n try {\n return equal(a, b);\n } catch (error) {\n if (/stack|recursion/i.test((error as any)?.message || \"\")) {\n // eslint-disable-next-line no-console\n console.warn(\"isEqual cannot handle circular refs\");\n\n return false;\n }\n\n throw error;\n }\n}\n"],"mappings":";;;;AAoBA,MAAM,SAAS,OAAO,QAAQ;AAC9B,MAAM,SAAS,OAAO,QAAQ;AAC9B,MAAM,iBACJ,OAAO,gBAAgB,cAAc,YAAY,WAAW;AAE9D,SAAS,MAAM,GAAQ,GAAQ;AAC7B,KAAI,MAAM,EACR,QAAO;AAGT,KAAI,KAAK,KAAK,OAAO,MAAM,YAAY,OAAO,MAAM,UAAU;AAC5D,MAAI,EAAE,gBAAgB,EAAE,YACtB,QAAO;EAGT,IAAI;AACJ,MAAI,MAAM,QAAQ,EAAE,EAAE;AACpB,YAAS,EAAE;AAEX,OAAI,UAAU,EAAE,OAAQ,QAAO;AAC/B,QAAK,IAAI,IAAI,QAAQ,QAAQ,GAAK,KAAI,CAAC,MAAM,EAAE,IAAI,EAAE,GAAG,CAAE,QAAO;AACjE,UAAO;;EAGT,IAAI;AACJ,MAAI,UAAU,aAAa,OAAO,aAAa,KAAK;AAClD,OAAI,EAAE,SAAS,EAAE,KAAM,QAAO;AAC9B,QAAK,EAAE,SAAS;GAChB,IAAI;AACJ,UAAO,EAAE,IAAI,GAAG,MAAM,EAAE,KAAM,KAAI,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,CAAE,QAAO;AAC7D,QAAK,EAAE,SAAS;AAChB,UAAO,EAAE,IAAI,GAAG,MAAM,EAAE,KACtB,KAAI,CAAC,MAAM,EAAE,MAAM,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,CAAE,QAAO;AAEpD,UAAO;;AAGT,MAAI,UAAU,aAAa,OAAO,aAAa,KAAK;AAClD,OAAI,EAAE,SAAS,EAAE,KAAM,QAAO;AAC9B,QAAK,EAAE,SAAS;GAChB,IAAI;AACJ,UAAO,EAAE,IAAI,GAAG,MAAM,EAAE,KAAM,KAAI,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,CAAE,QAAO;AAC7D,UAAO;;AAGT,MACE,MAAM,QAAQ,EAAE,IAChB,MAAM,QAAQ,EAAE,IAChB,kBACA,YAAY,OAAO,EAAE,IACrB,YAAY,OAAO,EAAE,EACrB;AACA,YAAS,EAAE;AAEX,OAAI,UAAU,EAAE,OAAQ,QAAO;AAC/B,QAAK,IAAI,IAAI,QAAQ,QAAQ,GAAK,KAAI,EAAE,OAAO,EAAE,GAAI,QAAO;AAC5D,UAAO;;AAGT,MAAI,EAAE,gBAAgB,OACpB,QAAO,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE;AAGhD,MACE,EAAE,YAAY,OAAO,UAAU,WAC/B,OAAO,EAAE,YAAY,cACrB,OAAO,EAAE,YAAY,WAGrB,QAAO,EAAE,SAAS,KAAK,EAAE,SAAS;AAEpC,MACE,EAAE,aAAa,OAAO,UAAU,YAChC,OAAO,EAAE,aAAa,cACtB,OAAO,EAAE,aAAa,WAGtB,QAAO,EAAE,UAAU,KAAK,EAAE,UAAU;EAGtC,MAAM,OAAO,OAAO,KAAK,EAAE;AAC3B,WAAS,KAAK;AACd,MAAI,WAAW,OAAO,KAAK,EAAE,CAAC,OAC5B,QAAO;AAGT,OAAK,IAAI,IAAI,QAAQ,QAAQ,GAC3B,KACE,CAAC,MAAM,EAAE,IACT,CAAC,YAAY,KAAK,GAAG,IACrB,CAAC,OAAO,UAAU,eAAe,KAAK,GAAG,KAAK,GAAU,CAExD,QAAO;AAIX,OAAK,IAAI,IAAI,QAAQ,QAAQ,IAAK;AAChC,OACE,MAAM,QAAQ,KAAK,KAClB,KAAK,OAAO,YAAY,KAAK,OAAO,SAAS,KAAK,OAAO,UAC1D,EAAE,SAEF;AAGF,OACE,CAAC,MAAM,EAAE,IACT,CAAC,YAAY,KAAK,GAAG,IACrB,CAAC,MAAM,EAAE,KAAK,KAAY,EAAE,KAAK,IAAW,CAE5C,QAAO;;AAIX,SAAO;;AAIT,QAAO,MAAM,KAAK,MAAM;;;;;;;;;;;;;;;;;;;AAoB1B,SAAgB,QAAQ,GAAQ,GAAiB;AAC/C,KAAI;AACF,SAAO,MAAM,GAAG,EAAE;UACX,OAAO;AACd,MAAI,mBAAmB,KAAM,OAAe,WAAW,GAAG,EAAE;AAE1D,WAAQ,KAAK,sCAAsC;AAEnD,UAAO;;AAGT,QAAM"}
|
package/dist/lru-cache.cjs
CHANGED
|
@@ -1 +1,190 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/lru-cache.ts
|
|
3
|
+
var LRUNode = class {
|
|
4
|
+
key;
|
|
5
|
+
data;
|
|
6
|
+
size;
|
|
7
|
+
prev = null;
|
|
8
|
+
next = null;
|
|
9
|
+
constructor(key, data, size) {
|
|
10
|
+
this.key = key;
|
|
11
|
+
this.data = data;
|
|
12
|
+
this.size = size;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Sentinel node used for head/tail boundaries.
|
|
17
|
+
* These nodes don't contain actual cache data but simplify list operations.
|
|
18
|
+
*/
|
|
19
|
+
var SentinelNode = class {
|
|
20
|
+
prev = null;
|
|
21
|
+
next = null;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* LRU (Least Recently Used) Cache implementation using a doubly-linked list
|
|
25
|
+
* and hash map for O(1) operations.
|
|
26
|
+
*
|
|
27
|
+
* Algorithm:
|
|
28
|
+
* - Uses a doubly-linked list to maintain access order (most recent at head)
|
|
29
|
+
* - Hash map provides O(1) key-to-node lookup
|
|
30
|
+
* - Sentinel head/tail nodes simplify edge case handling
|
|
31
|
+
* - Size-based eviction supports custom size calculation functions
|
|
32
|
+
*
|
|
33
|
+
* Data Structure Layout:
|
|
34
|
+
* HEAD \<-\> [most recent] \<-\> ... \<-\> [least recent] \<-\> TAIL
|
|
35
|
+
*
|
|
36
|
+
* Operations:
|
|
37
|
+
* - get(): Move accessed node to head (mark as most recent)
|
|
38
|
+
* - set(): Add new node at head, evict from tail if over capacity
|
|
39
|
+
* - Eviction: Remove least recent node (tail.prev) when size exceeds limit
|
|
40
|
+
*/
|
|
41
|
+
var LRUCache = class {
|
|
42
|
+
cache = /* @__PURE__ */ new Map();
|
|
43
|
+
head;
|
|
44
|
+
tail;
|
|
45
|
+
totalSize = 0;
|
|
46
|
+
maxSize;
|
|
47
|
+
calculateSize;
|
|
48
|
+
constructor(maxSize, calculateSize) {
|
|
49
|
+
this.maxSize = maxSize;
|
|
50
|
+
this.calculateSize = calculateSize;
|
|
51
|
+
this.head = new SentinelNode();
|
|
52
|
+
this.tail = new SentinelNode();
|
|
53
|
+
this.head.next = this.tail;
|
|
54
|
+
this.tail.prev = this.head;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Adds a node immediately after the head (marks as most recently used). Used when inserting new items or when an item is accessed. **PRECONDITION:** node must be disconnected (prev/next should be null)
|
|
58
|
+
*
|
|
59
|
+
* @param node - The node to add after the head.
|
|
60
|
+
*/
|
|
61
|
+
addToHead(node) {
|
|
62
|
+
node.prev = this.head;
|
|
63
|
+
node.next = this.head.next;
|
|
64
|
+
this.head.next.prev = node;
|
|
65
|
+
this.head.next = node;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Removes a node from its current position in the doubly-linked list. Updates the prev/next pointers of adjacent nodes to maintain list integrity. **PRECONDITION:** node must be connected (prev/next are non-null)
|
|
69
|
+
*
|
|
70
|
+
* @param node - The node to remove from the list.
|
|
71
|
+
*/
|
|
72
|
+
removeNode(node) {
|
|
73
|
+
node.prev.next = node.next;
|
|
74
|
+
node.next.prev = node.prev;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Moves an existing node to the head position (marks as most recently used). This is the core LRU operation - accessed items become most recent.
|
|
78
|
+
*
|
|
79
|
+
* @param node - The node to move to the head.
|
|
80
|
+
*/
|
|
81
|
+
moveToHead(node) {
|
|
82
|
+
this.removeNode(node);
|
|
83
|
+
this.addToHead(node);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Removes and returns the least recently used node (the one before tail). This is called during eviction when the cache exceeds capacity. **PRECONDITION:** cache is not empty (ensured by caller)
|
|
87
|
+
*
|
|
88
|
+
* @returns The removed least recently used node.
|
|
89
|
+
*/
|
|
90
|
+
removeTail() {
|
|
91
|
+
const lastNode = this.tail.prev;
|
|
92
|
+
this.removeNode(lastNode);
|
|
93
|
+
return lastNode;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Sets a key-value pair in the cache.
|
|
97
|
+
* If the key exists, updates the value and moves to head.
|
|
98
|
+
* If new, adds at head and evicts from tail if necessary.
|
|
99
|
+
*
|
|
100
|
+
* Time Complexity:
|
|
101
|
+
* - O(1) for uniform item sizes
|
|
102
|
+
* - O(k) where k is the number of items evicted (can be O(N) for variable sizes)
|
|
103
|
+
*
|
|
104
|
+
* @param key - The key to set.
|
|
105
|
+
* @param value - The value to set.
|
|
106
|
+
*/
|
|
107
|
+
set(key, value) {
|
|
108
|
+
const size = this.calculateSize?.(value) ?? 1;
|
|
109
|
+
if (size > this.maxSize) return;
|
|
110
|
+
const existing = this.cache.get(key);
|
|
111
|
+
if (existing) {
|
|
112
|
+
existing.data = value;
|
|
113
|
+
this.totalSize = this.totalSize - existing.size + size;
|
|
114
|
+
existing.size = size;
|
|
115
|
+
this.moveToHead(existing);
|
|
116
|
+
} else {
|
|
117
|
+
const newNode = new LRUNode(key, value, size);
|
|
118
|
+
this.cache.set(key, newNode);
|
|
119
|
+
this.addToHead(newNode);
|
|
120
|
+
this.totalSize += size;
|
|
121
|
+
}
|
|
122
|
+
while (this.totalSize > this.maxSize && this.cache.size > 0) {
|
|
123
|
+
const tail = this.removeTail();
|
|
124
|
+
this.cache.delete(tail.key);
|
|
125
|
+
this.totalSize -= tail.size;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Checks if a key exists in the cache.
|
|
130
|
+
* This is a pure query operation - does NOT update LRU order.
|
|
131
|
+
*
|
|
132
|
+
* Time Complexity: O(1)
|
|
133
|
+
*/
|
|
134
|
+
has(key) {
|
|
135
|
+
return this.cache.has(key);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Retrieves a value by key and marks it as most recently used.
|
|
139
|
+
* Moving to head maintains the LRU property for future evictions.
|
|
140
|
+
*
|
|
141
|
+
* Time Complexity: O(1)
|
|
142
|
+
*/
|
|
143
|
+
get(key) {
|
|
144
|
+
const node = this.cache.get(key);
|
|
145
|
+
if (!node) return void 0;
|
|
146
|
+
this.moveToHead(node);
|
|
147
|
+
return node.data;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Returns an iterator over the cache entries. The order is outputted in the
|
|
151
|
+
* order of most recently used to least recently used.
|
|
152
|
+
*/
|
|
153
|
+
*[Symbol.iterator]() {
|
|
154
|
+
let current = this.head.next;
|
|
155
|
+
while (current && current !== this.tail) {
|
|
156
|
+
const node = current;
|
|
157
|
+
yield [node.key, node.data];
|
|
158
|
+
current = current.next;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Removes a specific key from the cache.
|
|
163
|
+
* Updates both the hash map and doubly-linked list.
|
|
164
|
+
*
|
|
165
|
+
* Time Complexity: O(1)
|
|
166
|
+
*/
|
|
167
|
+
remove(key) {
|
|
168
|
+
const node = this.cache.get(key);
|
|
169
|
+
if (!node) return;
|
|
170
|
+
this.removeNode(node);
|
|
171
|
+
this.cache.delete(key);
|
|
172
|
+
this.totalSize -= node.size;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Returns the number of items in the cache.
|
|
176
|
+
*/
|
|
177
|
+
get size() {
|
|
178
|
+
return this.cache.size;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Returns the current total size of all cached items.
|
|
182
|
+
* This uses the custom size calculation if provided.
|
|
183
|
+
*/
|
|
184
|
+
get currentSize() {
|
|
185
|
+
return this.totalSize;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
//#endregion
|
|
190
|
+
exports.LRUCache = LRUCache;
|
package/dist/lru-cache.mjs
CHANGED
|
@@ -1,2 +1,190 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/lru-cache.ts
|
|
2
|
+
var LRUNode = class {
|
|
3
|
+
key;
|
|
4
|
+
data;
|
|
5
|
+
size;
|
|
6
|
+
prev = null;
|
|
7
|
+
next = null;
|
|
8
|
+
constructor(key, data, size) {
|
|
9
|
+
this.key = key;
|
|
10
|
+
this.data = data;
|
|
11
|
+
this.size = size;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Sentinel node used for head/tail boundaries.
|
|
16
|
+
* These nodes don't contain actual cache data but simplify list operations.
|
|
17
|
+
*/
|
|
18
|
+
var SentinelNode = class {
|
|
19
|
+
prev = null;
|
|
20
|
+
next = null;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* LRU (Least Recently Used) Cache implementation using a doubly-linked list
|
|
24
|
+
* and hash map for O(1) operations.
|
|
25
|
+
*
|
|
26
|
+
* Algorithm:
|
|
27
|
+
* - Uses a doubly-linked list to maintain access order (most recent at head)
|
|
28
|
+
* - Hash map provides O(1) key-to-node lookup
|
|
29
|
+
* - Sentinel head/tail nodes simplify edge case handling
|
|
30
|
+
* - Size-based eviction supports custom size calculation functions
|
|
31
|
+
*
|
|
32
|
+
* Data Structure Layout:
|
|
33
|
+
* HEAD \<-\> [most recent] \<-\> ... \<-\> [least recent] \<-\> TAIL
|
|
34
|
+
*
|
|
35
|
+
* Operations:
|
|
36
|
+
* - get(): Move accessed node to head (mark as most recent)
|
|
37
|
+
* - set(): Add new node at head, evict from tail if over capacity
|
|
38
|
+
* - Eviction: Remove least recent node (tail.prev) when size exceeds limit
|
|
39
|
+
*/
|
|
40
|
+
var LRUCache = class {
|
|
41
|
+
cache = /* @__PURE__ */ new Map();
|
|
42
|
+
head;
|
|
43
|
+
tail;
|
|
44
|
+
totalSize = 0;
|
|
45
|
+
maxSize;
|
|
46
|
+
calculateSize;
|
|
47
|
+
constructor(maxSize, calculateSize) {
|
|
48
|
+
this.maxSize = maxSize;
|
|
49
|
+
this.calculateSize = calculateSize;
|
|
50
|
+
this.head = new SentinelNode();
|
|
51
|
+
this.tail = new SentinelNode();
|
|
52
|
+
this.head.next = this.tail;
|
|
53
|
+
this.tail.prev = this.head;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Adds a node immediately after the head (marks as most recently used). Used when inserting new items or when an item is accessed. **PRECONDITION:** node must be disconnected (prev/next should be null)
|
|
57
|
+
*
|
|
58
|
+
* @param node - The node to add after the head.
|
|
59
|
+
*/
|
|
60
|
+
addToHead(node) {
|
|
61
|
+
node.prev = this.head;
|
|
62
|
+
node.next = this.head.next;
|
|
63
|
+
this.head.next.prev = node;
|
|
64
|
+
this.head.next = node;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Removes a node from its current position in the doubly-linked list. Updates the prev/next pointers of adjacent nodes to maintain list integrity. **PRECONDITION:** node must be connected (prev/next are non-null)
|
|
68
|
+
*
|
|
69
|
+
* @param node - The node to remove from the list.
|
|
70
|
+
*/
|
|
71
|
+
removeNode(node) {
|
|
72
|
+
node.prev.next = node.next;
|
|
73
|
+
node.next.prev = node.prev;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Moves an existing node to the head position (marks as most recently used). This is the core LRU operation - accessed items become most recent.
|
|
77
|
+
*
|
|
78
|
+
* @param node - The node to move to the head.
|
|
79
|
+
*/
|
|
80
|
+
moveToHead(node) {
|
|
81
|
+
this.removeNode(node);
|
|
82
|
+
this.addToHead(node);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Removes and returns the least recently used node (the one before tail). This is called during eviction when the cache exceeds capacity. **PRECONDITION:** cache is not empty (ensured by caller)
|
|
86
|
+
*
|
|
87
|
+
* @returns The removed least recently used node.
|
|
88
|
+
*/
|
|
89
|
+
removeTail() {
|
|
90
|
+
const lastNode = this.tail.prev;
|
|
91
|
+
this.removeNode(lastNode);
|
|
92
|
+
return lastNode;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Sets a key-value pair in the cache.
|
|
96
|
+
* If the key exists, updates the value and moves to head.
|
|
97
|
+
* If new, adds at head and evicts from tail if necessary.
|
|
98
|
+
*
|
|
99
|
+
* Time Complexity:
|
|
100
|
+
* - O(1) for uniform item sizes
|
|
101
|
+
* - O(k) where k is the number of items evicted (can be O(N) for variable sizes)
|
|
102
|
+
*
|
|
103
|
+
* @param key - The key to set.
|
|
104
|
+
* @param value - The value to set.
|
|
105
|
+
*/
|
|
106
|
+
set(key, value) {
|
|
107
|
+
const size = this.calculateSize?.(value) ?? 1;
|
|
108
|
+
if (size > this.maxSize) return;
|
|
109
|
+
const existing = this.cache.get(key);
|
|
110
|
+
if (existing) {
|
|
111
|
+
existing.data = value;
|
|
112
|
+
this.totalSize = this.totalSize - existing.size + size;
|
|
113
|
+
existing.size = size;
|
|
114
|
+
this.moveToHead(existing);
|
|
115
|
+
} else {
|
|
116
|
+
const newNode = new LRUNode(key, value, size);
|
|
117
|
+
this.cache.set(key, newNode);
|
|
118
|
+
this.addToHead(newNode);
|
|
119
|
+
this.totalSize += size;
|
|
120
|
+
}
|
|
121
|
+
while (this.totalSize > this.maxSize && this.cache.size > 0) {
|
|
122
|
+
const tail = this.removeTail();
|
|
123
|
+
this.cache.delete(tail.key);
|
|
124
|
+
this.totalSize -= tail.size;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Checks if a key exists in the cache.
|
|
129
|
+
* This is a pure query operation - does NOT update LRU order.
|
|
130
|
+
*
|
|
131
|
+
* Time Complexity: O(1)
|
|
132
|
+
*/
|
|
133
|
+
has(key) {
|
|
134
|
+
return this.cache.has(key);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Retrieves a value by key and marks it as most recently used.
|
|
138
|
+
* Moving to head maintains the LRU property for future evictions.
|
|
139
|
+
*
|
|
140
|
+
* Time Complexity: O(1)
|
|
141
|
+
*/
|
|
142
|
+
get(key) {
|
|
143
|
+
const node = this.cache.get(key);
|
|
144
|
+
if (!node) return void 0;
|
|
145
|
+
this.moveToHead(node);
|
|
146
|
+
return node.data;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Returns an iterator over the cache entries. The order is outputted in the
|
|
150
|
+
* order of most recently used to least recently used.
|
|
151
|
+
*/
|
|
152
|
+
*[Symbol.iterator]() {
|
|
153
|
+
let current = this.head.next;
|
|
154
|
+
while (current && current !== this.tail) {
|
|
155
|
+
const node = current;
|
|
156
|
+
yield [node.key, node.data];
|
|
157
|
+
current = current.next;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Removes a specific key from the cache.
|
|
162
|
+
* Updates both the hash map and doubly-linked list.
|
|
163
|
+
*
|
|
164
|
+
* Time Complexity: O(1)
|
|
165
|
+
*/
|
|
166
|
+
remove(key) {
|
|
167
|
+
const node = this.cache.get(key);
|
|
168
|
+
if (!node) return;
|
|
169
|
+
this.removeNode(node);
|
|
170
|
+
this.cache.delete(key);
|
|
171
|
+
this.totalSize -= node.size;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Returns the number of items in the cache.
|
|
175
|
+
*/
|
|
176
|
+
get size() {
|
|
177
|
+
return this.cache.size;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Returns the current total size of all cached items.
|
|
181
|
+
* This uses the custom size calculation if provided.
|
|
182
|
+
*/
|
|
183
|
+
get currentSize() {
|
|
184
|
+
return this.totalSize;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
//#endregion
|
|
189
|
+
export { LRUCache };
|
|
2
190
|
//# sourceMappingURL=lru-cache.mjs.map
|
package/dist/lru-cache.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lru-cache.mjs","names":[],"sources":["../src/lru-cache.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nclass LRUNode<T> {\n public readonly key: string;\n\n public data: T;\n\n public size: number;\n\n public prev: LRUNode<T> | SentinelNode<T> | null = null;\n\n public next: LRUNode<T> | SentinelNode<T> | null = null;\n\n constructor(key: string, data: T, size: number) {\n this.key = key;\n this.data = data;\n this.size = size;\n }\n}\n\n/**\n * Sentinel node used for head/tail boundaries.\n * These nodes don't contain actual cache data but simplify list operations.\n */\nclass SentinelNode<T> {\n public prev: LRUNode<T> | SentinelNode<T> | null = null;\n\n public next: LRUNode<T> | SentinelNode<T> | null = null;\n}\n\n/**\n * LRU (Least Recently Used) Cache implementation using a doubly-linked list\n * and hash map for O(1) operations.\n *\n * Algorithm:\n * - Uses a doubly-linked list to maintain access order (most recent at head)\n * - Hash map provides O(1) key-to-node lookup\n * - Sentinel head/tail nodes simplify edge case handling\n * - Size-based eviction supports custom size calculation functions\n *\n * Data Structure Layout:\n * HEAD \\<-\\> [most recent] \\<-\\> ... \\<-\\> [least recent] \\<-\\> TAIL\n *\n * Operations:\n * - get(): Move accessed node to head (mark as most recent)\n * - set(): Add new node at head, evict from tail if over capacity\n * - Eviction: Remove least recent node (tail.prev) when size exceeds limit\n */\nexport class LRUCache<T> {\n private readonly cache: Map<string, LRUNode<T>> = new Map();\n\n private readonly head: SentinelNode<T>;\n\n private readonly tail: SentinelNode<T>;\n\n private totalSize: number = 0;\n\n private readonly maxSize: number;\n\n private readonly calculateSize: ((value: T) => number) | undefined;\n\n constructor(maxSize: number, calculateSize?: (value: T) => number) {\n this.maxSize = maxSize;\n this.calculateSize = calculateSize;\n\n // Create sentinel nodes to simplify doubly-linked list operations\n // HEAD <-> TAIL (empty list)\n this.head = new SentinelNode<T>();\n this.tail = new SentinelNode<T>();\n this.head.next = this.tail;\n this.tail.prev = this.head;\n }\n\n /**\n * Adds a node immediately after the head (marks as most recently used). Used when inserting new items or when an item is accessed. **PRECONDITION:** node must be disconnected (prev/next should be null)\n *\n * @param node - The node to add after the head.\n */\n private addToHead(node: LRUNode<T>): void {\n node.prev = this.head;\n node.next = this.head.next;\n // head.next is always non-null (points to tail or another node)\n this.head.next!.prev = node;\n this.head.next = node;\n }\n\n /**\n * Removes a node from its current position in the doubly-linked list. Updates the prev/next pointers of adjacent nodes to maintain list integrity. **PRECONDITION:** node must be connected (prev/next are non-null)\n *\n * @param node - The node to remove from the list.\n */\n private removeNode(node: LRUNode<T>): void {\n // Connected nodes always have non-null prev/next\n node.prev!.next = node.next;\n node.next!.prev = node.prev;\n }\n\n /**\n * Moves an existing node to the head position (marks as most recently used). This is the core LRU operation - accessed items become most recent.\n *\n * @param node - The node to move to the head.\n */\n private moveToHead(node: LRUNode<T>): void {\n this.removeNode(node);\n this.addToHead(node);\n }\n\n /**\n * Removes and returns the least recently used node (the one before tail). This is called during eviction when the cache exceeds capacity. **PRECONDITION:** cache is not empty (ensured by caller)\n *\n * @returns The removed least recently used node.\n */\n private removeTail(): LRUNode<T> {\n const lastNode = this.tail.prev as LRUNode<T>;\n // tail.prev is always non-null and always LRUNode when cache is not empty\n this.removeNode(lastNode);\n return lastNode;\n }\n\n /**\n * Sets a key-value pair in the cache.\n * If the key exists, updates the value and moves to head.\n * If new, adds at head and evicts from tail if necessary.\n *\n * Time Complexity:\n * - O(1) for uniform item sizes\n * - O(k) where k is the number of items evicted (can be O(N) for variable sizes)\n *\n * @param key - The key to set.\n * @param value - The value to set.\n */\n public set(key: string, value: T): void {\n const size = this.calculateSize?.(value) ?? 1;\n if (size > this.maxSize) {\n // console.warn(\"Single item size exceeds maxSize\");\n return;\n }\n\n const existing = this.cache.get(key);\n if (existing) {\n // Update existing node: adjust size and move to head (most recent)\n existing.data = value;\n this.totalSize = this.totalSize - existing.size + size;\n existing.size = size;\n this.moveToHead(existing);\n } else {\n // Add new node at head (most recent position)\n const newNode = new LRUNode(key, value, size);\n this.cache.set(key, newNode);\n this.addToHead(newNode);\n this.totalSize += size;\n }\n\n // Evict least recently used items until under capacity\n while (this.totalSize > this.maxSize && this.cache.size > 0) {\n const tail = this.removeTail();\n this.cache.delete(tail.key);\n this.totalSize -= tail.size;\n }\n }\n\n /**\n * Checks if a key exists in the cache.\n * This is a pure query operation - does NOT update LRU order.\n *\n * Time Complexity: O(1)\n */\n public has(key: string): boolean {\n return this.cache.has(key);\n }\n\n /**\n * Retrieves a value by key and marks it as most recently used.\n * Moving to head maintains the LRU property for future evictions.\n *\n * Time Complexity: O(1)\n */\n public get(key: string): T | undefined {\n const node = this.cache.get(key);\n if (!node) return undefined;\n\n // Mark as most recently used by moving to head\n this.moveToHead(node);\n\n return node.data;\n }\n\n /**\n * Returns an iterator over the cache entries. The order is outputted in the\n * order of most recently used to least recently used.\n */\n public *[Symbol.iterator](): IterableIterator<[string, T]> {\n let current = this.head.next;\n while (current && current !== this.tail) {\n // Between head and tail, current is always LRUNode\n const node = current as LRUNode<T>;\n yield [node.key, node.data];\n current = current.next;\n }\n }\n\n /**\n * Removes a specific key from the cache.\n * Updates both the hash map and doubly-linked list.\n *\n * Time Complexity: O(1)\n */\n public remove(key: string): void {\n const node = this.cache.get(key);\n if (!node) return;\n\n this.removeNode(node);\n this.cache.delete(key);\n this.totalSize -= node.size;\n }\n\n /**\n * Returns the number of items in the cache.\n */\n public get size(): number {\n return this.cache.size;\n }\n\n /**\n * Returns the current total size of all cached items.\n * This uses the custom size calculation if provided.\n */\n public get currentSize(): number {\n return this.totalSize;\n }\n}\n"],"mappings":"AAkBA,IAAM,EAAN,KAAiB,CACf,IAEA,KAEA,KAEA,KAAmD,KAEnD,KAAmD,KAEnD,YAAY,EAAa,EAAS,EAAc,CAC9C,KAAK,IAAM,EACX,KAAK,KAAO,EACZ,KAAK,KAAO,IAQV,EAAN,KAAsB,CACpB,KAAmD,KAEnD,KAAmD,MAqBxC,EAAb,KAAyB,CACvB,MAAkD,IAAI,IAEtD,KAEA,KAEA,UAA4B,EAE5B,QAEA,cAEA,YAAY,EAAiB,EAAsC,CACjE,KAAK,QAAU,EACf,KAAK,cAAgB,EAIrB,KAAK,KAAO,IAAI,EAChB,KAAK,KAAO,IAAI,EAChB,KAAK,KAAK,KAAO,KAAK,KACtB,KAAK,KAAK,KAAO,KAAK,KAQxB,UAAkB,EAAwB,CACxC,EAAK,KAAO,KAAK,KACjB,EAAK,KAAO,KAAK,KAAK,KAEtB,KAAK,KAAK,KAAM,KAAO,EACvB,KAAK,KAAK,KAAO,EAQnB,WAAmB,EAAwB,CAEzC,EAAK,KAAM,KAAO,EAAK,KACvB,EAAK,KAAM,KAAO,EAAK,KAQzB,WAAmB,EAAwB,CACzC,KAAK,WAAW,EAAK,CACrB,KAAK,UAAU,EAAK,CAQtB,YAAiC,CAC/B,IAAM,EAAW,KAAK,KAAK,KAG3B,OADA,KAAK,WAAW,EAAS,CAClB,EAeT,IAAW,EAAa,EAAgB,CACtC,IAAM,EAAO,KAAK,gBAAgB,EAAM,EAAI,EAC5C,GAAI,EAAO,KAAK,QAEd,OAGF,IAAM,EAAW,KAAK,MAAM,IAAI,EAAI,CACpC,GAAI,EAEF,EAAS,KAAO,EAChB,KAAK,UAAY,KAAK,UAAY,EAAS,KAAO,EAClD,EAAS,KAAO,EAChB,KAAK,WAAW,EAAS,KACpB,CAEL,IAAM,EAAU,IAAI,EAAQ,EAAK,EAAO,EAAK,CAC7C,KAAK,MAAM,IAAI,EAAK,EAAQ,CAC5B,KAAK,UAAU,EAAQ,CACvB,KAAK,WAAa,EAIpB,KAAO,KAAK,UAAY,KAAK,SAAW,KAAK,MAAM,KAAO,GAAG,CAC3D,IAAM,EAAO,KAAK,YAAY,CAC9B,KAAK,MAAM,OAAO,EAAK,IAAI,CAC3B,KAAK,WAAa,EAAK,MAU3B,IAAW,EAAsB,CAC/B,OAAO,KAAK,MAAM,IAAI,EAAI,CAS5B,IAAW,EAA4B,CACrC,IAAM,EAAO,KAAK,MAAM,IAAI,EAAI,CAC3B,KAKL,OAFA,KAAK,WAAW,EAAK,CAEd,EAAK,KAOd,EAAS,OAAO,WAA2C,CACzD,IAAI,EAAU,KAAK,KAAK,KACxB,KAAO,GAAW,IAAY,KAAK,MAAM,CAEvC,IAAM,EAAO,EACb,KAAM,CAAC,EAAK,IAAK,EAAK,KAAK,CAC3B,EAAU,EAAQ,MAUtB,OAAc,EAAmB,CAC/B,IAAM,EAAO,KAAK,MAAM,IAAI,EAAI,CAC3B,IAEL,KAAK,WAAW,EAAK,CACrB,KAAK,MAAM,OAAO,EAAI,CACtB,KAAK,WAAa,EAAK,MAMzB,IAAW,MAAe,CACxB,OAAO,KAAK,MAAM,KAOpB,IAAW,aAAsB,CAC/B,OAAO,KAAK"}
|
|
1
|
+
{"version":3,"file":"lru-cache.mjs","names":[],"sources":["../src/lru-cache.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nclass LRUNode<T> {\n public readonly key: string;\n\n public data: T;\n\n public size: number;\n\n public prev: LRUNode<T> | SentinelNode<T> | null = null;\n\n public next: LRUNode<T> | SentinelNode<T> | null = null;\n\n constructor(key: string, data: T, size: number) {\n this.key = key;\n this.data = data;\n this.size = size;\n }\n}\n\n/**\n * Sentinel node used for head/tail boundaries.\n * These nodes don't contain actual cache data but simplify list operations.\n */\nclass SentinelNode<T> {\n public prev: LRUNode<T> | SentinelNode<T> | null = null;\n\n public next: LRUNode<T> | SentinelNode<T> | null = null;\n}\n\n/**\n * LRU (Least Recently Used) Cache implementation using a doubly-linked list\n * and hash map for O(1) operations.\n *\n * Algorithm:\n * - Uses a doubly-linked list to maintain access order (most recent at head)\n * - Hash map provides O(1) key-to-node lookup\n * - Sentinel head/tail nodes simplify edge case handling\n * - Size-based eviction supports custom size calculation functions\n *\n * Data Structure Layout:\n * HEAD \\<-\\> [most recent] \\<-\\> ... \\<-\\> [least recent] \\<-\\> TAIL\n *\n * Operations:\n * - get(): Move accessed node to head (mark as most recent)\n * - set(): Add new node at head, evict from tail if over capacity\n * - Eviction: Remove least recent node (tail.prev) when size exceeds limit\n */\nexport class LRUCache<T> {\n private readonly cache: Map<string, LRUNode<T>> = new Map();\n\n private readonly head: SentinelNode<T>;\n\n private readonly tail: SentinelNode<T>;\n\n private totalSize: number = 0;\n\n private readonly maxSize: number;\n\n private readonly calculateSize: ((value: T) => number) | undefined;\n\n constructor(maxSize: number, calculateSize?: (value: T) => number) {\n this.maxSize = maxSize;\n this.calculateSize = calculateSize;\n\n // Create sentinel nodes to simplify doubly-linked list operations\n // HEAD <-> TAIL (empty list)\n this.head = new SentinelNode<T>();\n this.tail = new SentinelNode<T>();\n this.head.next = this.tail;\n this.tail.prev = this.head;\n }\n\n /**\n * Adds a node immediately after the head (marks as most recently used). Used when inserting new items or when an item is accessed. **PRECONDITION:** node must be disconnected (prev/next should be null)\n *\n * @param node - The node to add after the head.\n */\n private addToHead(node: LRUNode<T>): void {\n node.prev = this.head;\n node.next = this.head.next;\n // head.next is always non-null (points to tail or another node)\n this.head.next!.prev = node;\n this.head.next = node;\n }\n\n /**\n * Removes a node from its current position in the doubly-linked list. Updates the prev/next pointers of adjacent nodes to maintain list integrity. **PRECONDITION:** node must be connected (prev/next are non-null)\n *\n * @param node - The node to remove from the list.\n */\n private removeNode(node: LRUNode<T>): void {\n // Connected nodes always have non-null prev/next\n node.prev!.next = node.next;\n node.next!.prev = node.prev;\n }\n\n /**\n * Moves an existing node to the head position (marks as most recently used). This is the core LRU operation - accessed items become most recent.\n *\n * @param node - The node to move to the head.\n */\n private moveToHead(node: LRUNode<T>): void {\n this.removeNode(node);\n this.addToHead(node);\n }\n\n /**\n * Removes and returns the least recently used node (the one before tail). This is called during eviction when the cache exceeds capacity. **PRECONDITION:** cache is not empty (ensured by caller)\n *\n * @returns The removed least recently used node.\n */\n private removeTail(): LRUNode<T> {\n const lastNode = this.tail.prev as LRUNode<T>;\n // tail.prev is always non-null and always LRUNode when cache is not empty\n this.removeNode(lastNode);\n return lastNode;\n }\n\n /**\n * Sets a key-value pair in the cache.\n * If the key exists, updates the value and moves to head.\n * If new, adds at head and evicts from tail if necessary.\n *\n * Time Complexity:\n * - O(1) for uniform item sizes\n * - O(k) where k is the number of items evicted (can be O(N) for variable sizes)\n *\n * @param key - The key to set.\n * @param value - The value to set.\n */\n public set(key: string, value: T): void {\n const size = this.calculateSize?.(value) ?? 1;\n if (size > this.maxSize) {\n // console.warn(\"Single item size exceeds maxSize\");\n return;\n }\n\n const existing = this.cache.get(key);\n if (existing) {\n // Update existing node: adjust size and move to head (most recent)\n existing.data = value;\n this.totalSize = this.totalSize - existing.size + size;\n existing.size = size;\n this.moveToHead(existing);\n } else {\n // Add new node at head (most recent position)\n const newNode = new LRUNode(key, value, size);\n this.cache.set(key, newNode);\n this.addToHead(newNode);\n this.totalSize += size;\n }\n\n // Evict least recently used items until under capacity\n while (this.totalSize > this.maxSize && this.cache.size > 0) {\n const tail = this.removeTail();\n this.cache.delete(tail.key);\n this.totalSize -= tail.size;\n }\n }\n\n /**\n * Checks if a key exists in the cache.\n * This is a pure query operation - does NOT update LRU order.\n *\n * Time Complexity: O(1)\n */\n public has(key: string): boolean {\n return this.cache.has(key);\n }\n\n /**\n * Retrieves a value by key and marks it as most recently used.\n * Moving to head maintains the LRU property for future evictions.\n *\n * Time Complexity: O(1)\n */\n public get(key: string): T | undefined {\n const node = this.cache.get(key);\n if (!node) return undefined;\n\n // Mark as most recently used by moving to head\n this.moveToHead(node);\n\n return node.data;\n }\n\n /**\n * Returns an iterator over the cache entries. The order is outputted in the\n * order of most recently used to least recently used.\n */\n public *[Symbol.iterator](): IterableIterator<[string, T]> {\n let current = this.head.next;\n while (current && current !== this.tail) {\n // Between head and tail, current is always LRUNode\n const node = current as LRUNode<T>;\n yield [node.key, node.data];\n current = current.next;\n }\n }\n\n /**\n * Removes a specific key from the cache.\n * Updates both the hash map and doubly-linked list.\n *\n * Time Complexity: O(1)\n */\n public remove(key: string): void {\n const node = this.cache.get(key);\n if (!node) return;\n\n this.removeNode(node);\n this.cache.delete(key);\n this.totalSize -= node.size;\n }\n\n /**\n * Returns the number of items in the cache.\n */\n public get size(): number {\n return this.cache.size;\n }\n\n /**\n * Returns the current total size of all cached items.\n * This uses the custom size calculation if provided.\n */\n public get currentSize(): number {\n return this.totalSize;\n }\n}\n"],"mappings":";AAkBA,IAAM,UAAN,MAAiB;CACf,AAAgB;CAEhB,AAAO;CAEP,AAAO;CAEP,AAAO,OAA4C;CAEnD,AAAO,OAA4C;CAEnD,YAAY,KAAa,MAAS,MAAc;AAC9C,OAAK,MAAM;AACX,OAAK,OAAO;AACZ,OAAK,OAAO;;;;;;;AAQhB,IAAM,eAAN,MAAsB;CACpB,AAAO,OAA4C;CAEnD,AAAO,OAA4C;;;;;;;;;;;;;;;;;;;;AAqBrD,IAAa,WAAb,MAAyB;CACvB,AAAiB,wBAAiC,IAAI,KAAK;CAE3D,AAAiB;CAEjB,AAAiB;CAEjB,AAAQ,YAAoB;CAE5B,AAAiB;CAEjB,AAAiB;CAEjB,YAAY,SAAiB,eAAsC;AACjE,OAAK,UAAU;AACf,OAAK,gBAAgB;AAIrB,OAAK,OAAO,IAAI,cAAiB;AACjC,OAAK,OAAO,IAAI,cAAiB;AACjC,OAAK,KAAK,OAAO,KAAK;AACtB,OAAK,KAAK,OAAO,KAAK;;;;;;;CAQxB,AAAQ,UAAU,MAAwB;AACxC,OAAK,OAAO,KAAK;AACjB,OAAK,OAAO,KAAK,KAAK;AAEtB,OAAK,KAAK,KAAM,OAAO;AACvB,OAAK,KAAK,OAAO;;;;;;;CAQnB,AAAQ,WAAW,MAAwB;AAEzC,OAAK,KAAM,OAAO,KAAK;AACvB,OAAK,KAAM,OAAO,KAAK;;;;;;;CAQzB,AAAQ,WAAW,MAAwB;AACzC,OAAK,WAAW,KAAK;AACrB,OAAK,UAAU,KAAK;;;;;;;CAQtB,AAAQ,aAAyB;EAC/B,MAAM,WAAW,KAAK,KAAK;AAE3B,OAAK,WAAW,SAAS;AACzB,SAAO;;;;;;;;;;;;;;CAeT,AAAO,IAAI,KAAa,OAAgB;EACtC,MAAM,OAAO,KAAK,gBAAgB,MAAM,IAAI;AAC5C,MAAI,OAAO,KAAK,QAEd;EAGF,MAAM,WAAW,KAAK,MAAM,IAAI,IAAI;AACpC,MAAI,UAAU;AAEZ,YAAS,OAAO;AAChB,QAAK,YAAY,KAAK,YAAY,SAAS,OAAO;AAClD,YAAS,OAAO;AAChB,QAAK,WAAW,SAAS;SACpB;GAEL,MAAM,UAAU,IAAI,QAAQ,KAAK,OAAO,KAAK;AAC7C,QAAK,MAAM,IAAI,KAAK,QAAQ;AAC5B,QAAK,UAAU,QAAQ;AACvB,QAAK,aAAa;;AAIpB,SAAO,KAAK,YAAY,KAAK,WAAW,KAAK,MAAM,OAAO,GAAG;GAC3D,MAAM,OAAO,KAAK,YAAY;AAC9B,QAAK,MAAM,OAAO,KAAK,IAAI;AAC3B,QAAK,aAAa,KAAK;;;;;;;;;CAU3B,AAAO,IAAI,KAAsB;AAC/B,SAAO,KAAK,MAAM,IAAI,IAAI;;;;;;;;CAS5B,AAAO,IAAI,KAA4B;EACrC,MAAM,OAAO,KAAK,MAAM,IAAI,IAAI;AAChC,MAAI,CAAC,KAAM,QAAO;AAGlB,OAAK,WAAW,KAAK;AAErB,SAAO,KAAK;;;;;;CAOd,EAAS,OAAO,YAA2C;EACzD,IAAI,UAAU,KAAK,KAAK;AACxB,SAAO,WAAW,YAAY,KAAK,MAAM;GAEvC,MAAM,OAAO;AACb,SAAM,CAAC,KAAK,KAAK,KAAK,KAAK;AAC3B,aAAU,QAAQ;;;;;;;;;CAUtB,AAAO,OAAO,KAAmB;EAC/B,MAAM,OAAO,KAAK,MAAM,IAAI,IAAI;AAChC,MAAI,CAAC,KAAM;AAEX,OAAK,WAAW,KAAK;AACrB,OAAK,MAAM,OAAO,IAAI;AACtB,OAAK,aAAa,KAAK;;;;;CAMzB,IAAW,OAAe;AACxB,SAAO,KAAK,MAAM;;;;;;CAOpB,IAAW,cAAsB;AAC/B,SAAO,KAAK"}
|