@ls-stack/utils 2.6.0 → 2.7.0

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/cache.cjs ADDED
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/cache.ts
21
+ var cache_exports = {};
22
+ __export(cache_exports, {
23
+ cachedGetter: () => cachedGetter,
24
+ createCache: () => createCache
25
+ });
26
+ module.exports = __toCommonJS(cache_exports);
27
+
28
+ // src/assertions.ts
29
+ function isObject(value) {
30
+ return typeof value === "object" && value !== null && !Array.isArray(value);
31
+ }
32
+ function isPromise(value) {
33
+ return isObject(value) && "then" in value;
34
+ }
35
+
36
+ // src/cache.ts
37
+ function cachedGetter(getter) {
38
+ return {
39
+ get value() {
40
+ const value = getter();
41
+ Object.defineProperty(this, "value", { value });
42
+ return value;
43
+ }
44
+ };
45
+ }
46
+ function createCache({
47
+ maxCacheSize = 1e3,
48
+ maxItemAge,
49
+ expirationThrottle = 1e4
50
+ } = {}) {
51
+ const cache = /* @__PURE__ */ new Map();
52
+ let lastExpirationCheck = 0;
53
+ function checkExpiredItems() {
54
+ const now = Date.now();
55
+ if (!maxItemAge || now - lastExpirationCheck < expirationThrottle) return;
56
+ lastExpirationCheck = now;
57
+ const maxAgeMs = maxItemAge * 1e3;
58
+ for (const [key, item] of cache.entries()) {
59
+ if (now - item.timestamp > maxAgeMs) {
60
+ cache.delete(key);
61
+ }
62
+ }
63
+ }
64
+ function trimToSize() {
65
+ const currentSize = cache.size;
66
+ if (currentSize > maxCacheSize) {
67
+ const keysToRemove = currentSize - maxCacheSize;
68
+ const iterator = cache.keys();
69
+ for (let i = 0; i < keysToRemove; i++) {
70
+ const { value: key } = iterator.next();
71
+ if (key) {
72
+ cache.delete(key);
73
+ }
74
+ }
75
+ }
76
+ }
77
+ function isExpired(timestamp, now) {
78
+ return maxItemAge !== void 0 && now - timestamp > maxItemAge * 1e3;
79
+ }
80
+ function getOrInsert(cacheKey, val) {
81
+ const now = Date.now();
82
+ const entry = cache.get(cacheKey);
83
+ if (!entry || isExpired(entry.timestamp, now)) {
84
+ const value = val();
85
+ cache.set(cacheKey, { value, timestamp: now });
86
+ trimToSize();
87
+ checkExpiredItems();
88
+ return value;
89
+ }
90
+ return entry.value;
91
+ }
92
+ async function getOrInsertAsync(cacheKey, val) {
93
+ const entry = cache.get(cacheKey);
94
+ if (entry && isPromise(entry.value)) {
95
+ return entry.value;
96
+ }
97
+ const now = Date.now();
98
+ if (entry && !isExpired(entry.timestamp, now)) {
99
+ return entry.value;
100
+ }
101
+ const promise = val().then((result) => {
102
+ cache.set(cacheKey, { value: result, timestamp: Date.now() });
103
+ return result;
104
+ }).catch((error) => {
105
+ cache.delete(cacheKey);
106
+ throw error;
107
+ });
108
+ cache.set(cacheKey, { value: promise, timestamp: now });
109
+ trimToSize();
110
+ checkExpiredItems();
111
+ return promise;
112
+ }
113
+ return {
114
+ getOrInsert,
115
+ getOrInsertAsync,
116
+ clear: () => cache.clear(),
117
+ /** @internal */
118
+ "~cache": cache
119
+ };
120
+ }
121
+ // Annotate the CommonJS export names for ESM import in node:
122
+ 0 && (module.exports = {
123
+ cachedGetter,
124
+ createCache
125
+ });
@@ -0,0 +1,31 @@
1
+ declare function cachedGetter<T>(getter: () => T): {
2
+ value: T;
3
+ };
4
+ type Options = {
5
+ /**
6
+ * The maximum number of items in the cache.
7
+ * @default 1000
8
+ */
9
+ maxCacheSize?: number;
10
+ /**
11
+ * The maximum age of items in the cache in seconds.
12
+ */
13
+ maxItemAge?: number;
14
+ /**
15
+ * The maximum number of items to check for expiration in a single call.
16
+ * @default 10_000
17
+ */
18
+ expirationThrottle?: number;
19
+ };
20
+ declare function createCache({ maxCacheSize, maxItemAge, expirationThrottle, }?: Options): {
21
+ getOrInsert: <T>(cacheKey: string, val: () => T) => T;
22
+ getOrInsertAsync: <T>(cacheKey: string, val: () => Promise<T>) => Promise<T>;
23
+ clear: () => void;
24
+ /** @internal */
25
+ '~cache': Map<string, {
26
+ value: unknown;
27
+ timestamp: number;
28
+ }>;
29
+ };
30
+
31
+ export { cachedGetter, createCache };
@@ -0,0 +1,31 @@
1
+ declare function cachedGetter<T>(getter: () => T): {
2
+ value: T;
3
+ };
4
+ type Options = {
5
+ /**
6
+ * The maximum number of items in the cache.
7
+ * @default 1000
8
+ */
9
+ maxCacheSize?: number;
10
+ /**
11
+ * The maximum age of items in the cache in seconds.
12
+ */
13
+ maxItemAge?: number;
14
+ /**
15
+ * The maximum number of items to check for expiration in a single call.
16
+ * @default 10_000
17
+ */
18
+ expirationThrottle?: number;
19
+ };
20
+ declare function createCache({ maxCacheSize, maxItemAge, expirationThrottle, }?: Options): {
21
+ getOrInsert: <T>(cacheKey: string, val: () => T) => T;
22
+ getOrInsertAsync: <T>(cacheKey: string, val: () => Promise<T>) => Promise<T>;
23
+ clear: () => void;
24
+ /** @internal */
25
+ '~cache': Map<string, {
26
+ value: unknown;
27
+ timestamp: number;
28
+ }>;
29
+ };
30
+
31
+ export { cachedGetter, createCache };
package/dist/cache.js ADDED
@@ -0,0 +1,93 @@
1
+ import {
2
+ isPromise
3
+ } from "./chunk-4UGSP3L3.js";
4
+
5
+ // src/cache.ts
6
+ function cachedGetter(getter) {
7
+ return {
8
+ get value() {
9
+ const value = getter();
10
+ Object.defineProperty(this, "value", { value });
11
+ return value;
12
+ }
13
+ };
14
+ }
15
+ function createCache({
16
+ maxCacheSize = 1e3,
17
+ maxItemAge,
18
+ expirationThrottle = 1e4
19
+ } = {}) {
20
+ const cache = /* @__PURE__ */ new Map();
21
+ let lastExpirationCheck = 0;
22
+ function checkExpiredItems() {
23
+ const now = Date.now();
24
+ if (!maxItemAge || now - lastExpirationCheck < expirationThrottle) return;
25
+ lastExpirationCheck = now;
26
+ const maxAgeMs = maxItemAge * 1e3;
27
+ for (const [key, item] of cache.entries()) {
28
+ if (now - item.timestamp > maxAgeMs) {
29
+ cache.delete(key);
30
+ }
31
+ }
32
+ }
33
+ function trimToSize() {
34
+ const currentSize = cache.size;
35
+ if (currentSize > maxCacheSize) {
36
+ const keysToRemove = currentSize - maxCacheSize;
37
+ const iterator = cache.keys();
38
+ for (let i = 0; i < keysToRemove; i++) {
39
+ const { value: key } = iterator.next();
40
+ if (key) {
41
+ cache.delete(key);
42
+ }
43
+ }
44
+ }
45
+ }
46
+ function isExpired(timestamp, now) {
47
+ return maxItemAge !== void 0 && now - timestamp > maxItemAge * 1e3;
48
+ }
49
+ function getOrInsert(cacheKey, val) {
50
+ const now = Date.now();
51
+ const entry = cache.get(cacheKey);
52
+ if (!entry || isExpired(entry.timestamp, now)) {
53
+ const value = val();
54
+ cache.set(cacheKey, { value, timestamp: now });
55
+ trimToSize();
56
+ checkExpiredItems();
57
+ return value;
58
+ }
59
+ return entry.value;
60
+ }
61
+ async function getOrInsertAsync(cacheKey, val) {
62
+ const entry = cache.get(cacheKey);
63
+ if (entry && isPromise(entry.value)) {
64
+ return entry.value;
65
+ }
66
+ const now = Date.now();
67
+ if (entry && !isExpired(entry.timestamp, now)) {
68
+ return entry.value;
69
+ }
70
+ const promise = val().then((result) => {
71
+ cache.set(cacheKey, { value: result, timestamp: Date.now() });
72
+ return result;
73
+ }).catch((error) => {
74
+ cache.delete(cacheKey);
75
+ throw error;
76
+ });
77
+ cache.set(cacheKey, { value: promise, timestamp: now });
78
+ trimToSize();
79
+ checkExpiredItems();
80
+ return promise;
81
+ }
82
+ return {
83
+ getOrInsert,
84
+ getOrInsertAsync,
85
+ clear: () => cache.clear(),
86
+ /** @internal */
87
+ "~cache": cache
88
+ };
89
+ }
90
+ export {
91
+ cachedGetter,
92
+ createCache
93
+ };
package/dist/main.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  ///<reference path="arrayUtils.d.ts" />
2
2
  ///<reference path="assertions.d.ts" />
3
+ ///<reference path="cache.d.ts" />
3
4
  ///<reference path="castValues.d.ts" />
4
5
  ///<reference path="consoleFmt.d.ts" />
5
6
  ///<reference path="conversions.d.ts" />
@@ -1,11 +1,11 @@
1
+ import {
2
+ sleep
3
+ } from "./chunk-5DZT3Z5Z.js";
1
4
  import {
2
5
  Result,
3
6
  unknownToError
4
7
  } from "./chunk-KBFP7INB.js";
5
8
  import "./chunk-VAAMRG4K.js";
6
- import {
7
- sleep
8
- } from "./chunk-5DZT3Z5Z.js";
9
9
  import {
10
10
  invariant,
11
11
  isObject
package/dist/testUtils.js CHANGED
@@ -1,13 +1,13 @@
1
+ import {
2
+ omit,
3
+ pick
4
+ } from "./chunk-ZE3DLPMN.js";
1
5
  import {
2
6
  deepEqual
3
7
  } from "./chunk-JQFUKJU5.js";
4
8
  import {
5
9
  clampMin
6
10
  } from "./chunk-HTCYUMDR.js";
7
- import {
8
- omit,
9
- pick
10
- } from "./chunk-ZE3DLPMN.js";
11
11
  import {
12
12
  arrayWithPrevAndIndex,
13
13
  filterAndMap
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "2.6.0",
4
+ "version": "2.7.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"
@@ -29,6 +29,11 @@
29
29
  "types": "./dist/assertions.d.ts",
30
30
  "require": "./dist/assertions.cjs"
31
31
  },
32
+ "./cache": {
33
+ "import": "./dist/cache.js",
34
+ "types": "./dist/cache.d.ts",
35
+ "require": "./dist/cache.cjs"
36
+ },
32
37
  "./castValues": {
33
38
  "import": "./dist/castValues.js",
34
39
  "types": "./dist/castValues.d.ts",
@@ -208,6 +213,9 @@
208
213
  "assertions": [
209
214
  "./dist/assertions.d.ts"
210
215
  ],
216
+ "cache": [
217
+ "./dist/cache.d.ts"
218
+ ],
211
219
  "castValues": [
212
220
  "./dist/castValues.d.ts"
213
221
  ],