@ls-stack/utils 2.13.0 → 2.14.1

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 CHANGED
@@ -20,7 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/cache.ts
21
21
  var cache_exports = {};
22
22
  __export(cache_exports, {
23
- RejectValue: () => RejectValue,
23
+ SkipCaching: () => SkipCaching,
24
24
  WithExpiration: () => WithExpiration,
25
25
  cachedGetter: () => cachedGetter,
26
26
  createCache: () => createCache
@@ -61,7 +61,7 @@ function cachedGetter(getter) {
61
61
  }
62
62
  };
63
63
  }
64
- var RejectValue = class {
64
+ var SkipCaching = class {
65
65
  value;
66
66
  constructor(value) {
67
67
  this.value = value;
@@ -126,20 +126,23 @@ function createCache({
126
126
  return { value, timestamp: now, expiration: void 0 };
127
127
  }
128
128
  const utils = {
129
- reject: (value) => new RejectValue(value),
129
+ skipCaching: (value) => new SkipCaching(value),
130
130
  withExpiration: (value, expiration) => {
131
131
  return new WithExpiration(value, expiration);
132
132
  }
133
133
  };
134
134
  return {
135
- getOrInsert(cacheKey, val) {
135
+ getOrInsert(cacheKey, val, options) {
136
136
  const now = Date.now();
137
137
  const entry = cache.get(cacheKey);
138
138
  if (!entry || isExpired(entry, now)) {
139
139
  const value = val(utils);
140
- if (value instanceof RejectValue) {
140
+ if (value instanceof SkipCaching) {
141
141
  return value.value;
142
142
  }
143
+ if (options?.skipCachingWhen?.(value)) {
144
+ return value;
145
+ }
143
146
  const unwrappedValue = unwrapValue(value, now);
144
147
  cache.set(cacheKey, unwrappedValue);
145
148
  trimToSize();
@@ -153,7 +156,7 @@ function createCache({
153
156
  }
154
157
  return entry.value;
155
158
  },
156
- async getOrInsertAsync(cacheKey, val) {
159
+ async getOrInsertAsync(cacheKey, val, options) {
157
160
  const entry = cache.get(cacheKey);
158
161
  if (entry && isPromise(entry.value)) {
159
162
  return entry.value;
@@ -163,13 +166,20 @@ function createCache({
163
166
  return entry.value;
164
167
  }
165
168
  const promise = val(utils).then((result) => {
166
- if (result instanceof RejectValue) {
169
+ if (result instanceof SkipCaching) {
167
170
  const cacheValue = cache.get(cacheKey);
168
171
  if (cacheValue?.value === promise) {
169
172
  cache.delete(cacheKey);
170
173
  }
171
174
  return result.value;
172
175
  }
176
+ if (options?.skipCachingWhen?.(result)) {
177
+ const cacheValue = cache.get(cacheKey);
178
+ if (cacheValue?.value === promise) {
179
+ cache.delete(cacheKey);
180
+ }
181
+ return result;
182
+ }
173
183
  const unwrappedValue = unwrapValue(result, Date.now());
174
184
  cache.set(cacheKey, unwrappedValue);
175
185
  return unwrappedValue.value;
@@ -213,7 +223,7 @@ function createCache({
213
223
  },
214
224
  async setAsync(cacheKey, value) {
215
225
  const promise = value(utils).then((result) => {
216
- if (result instanceof RejectValue) {
226
+ if (result instanceof SkipCaching) {
217
227
  const cacheValue = cache.get(cacheKey);
218
228
  if (cacheValue?.value === promise) {
219
229
  cache.delete(cacheKey);
@@ -243,7 +253,7 @@ function createCache({
243
253
  }
244
254
  // Annotate the CommonJS export names for ESM import in node:
245
255
  0 && (module.exports = {
246
- RejectValue,
256
+ SkipCaching,
247
257
  WithExpiration,
248
258
  cachedGetter,
249
259
  createCache
package/dist/cache.d.cts CHANGED
@@ -20,7 +20,7 @@ type Options = {
20
20
  */
21
21
  expirationThrottle?: number;
22
22
  };
23
- declare class RejectValue<T> {
23
+ declare class SkipCaching<T> {
24
24
  value: T;
25
25
  constructor(value: T);
26
26
  }
@@ -34,7 +34,7 @@ declare class WithExpiration<T> {
34
34
  constructor(value: T, expiration: DurationObj);
35
35
  }
36
36
  type Utils<T> = {
37
- reject: (value: T) => RejectValue<T>;
37
+ skipCaching: (value: T) => SkipCaching<T>;
38
38
  /**
39
39
  * Create a new WithExpiration object with the given value and expiration time.
40
40
  * @param value - The value to store in the cache.
@@ -42,9 +42,18 @@ type Utils<T> = {
42
42
  */
43
43
  withExpiration: (value: T, expiration: DurationObj) => WithExpiration<T>;
44
44
  };
45
+ type GetOptions<T> = {
46
+ /**
47
+ * A function that determines whether a value should be rejected from being cached.
48
+ * If the function returns true, the value will be returned but not cached.
49
+ * @param value The value to check
50
+ * @returns true if the value should be rejected, false otherwise
51
+ */
52
+ skipCachingWhen?: (value: T) => boolean;
53
+ };
45
54
  type Cache<T> = {
46
- getOrInsert: (cacheKey: string, val: (utils: Utils<T>) => T | RejectValue<T>) => T;
47
- getOrInsertAsync: (cacheKey: string, val: (utils: Utils<T>) => Promise<T | RejectValue<T>>) => Promise<T>;
55
+ getOrInsert: (cacheKey: string, val: (utils: Utils<T>) => T | SkipCaching<T>, options?: GetOptions<T>) => T;
56
+ getOrInsertAsync: (cacheKey: string, val: (utils: Utils<T>) => Promise<T | SkipCaching<T>>, options?: GetOptions<T>) => Promise<T>;
48
57
  clear: () => void;
49
58
  get: (cacheKey: string) => T | undefined;
50
59
  set: (cacheKey: string, value: T | WithExpiration<T>) => void;
@@ -60,4 +69,4 @@ type Cache<T> = {
60
69
  };
61
70
  declare function createCache<T>({ maxCacheSize, maxItemAge, expirationThrottle, }?: Options): Cache<T>;
62
71
 
63
- export { type Cache, RejectValue, WithExpiration, cachedGetter, createCache };
72
+ export { type Cache, SkipCaching, WithExpiration, cachedGetter, createCache };
package/dist/cache.d.ts CHANGED
@@ -20,7 +20,7 @@ type Options = {
20
20
  */
21
21
  expirationThrottle?: number;
22
22
  };
23
- declare class RejectValue<T> {
23
+ declare class SkipCaching<T> {
24
24
  value: T;
25
25
  constructor(value: T);
26
26
  }
@@ -34,7 +34,7 @@ declare class WithExpiration<T> {
34
34
  constructor(value: T, expiration: DurationObj);
35
35
  }
36
36
  type Utils<T> = {
37
- reject: (value: T) => RejectValue<T>;
37
+ skipCaching: (value: T) => SkipCaching<T>;
38
38
  /**
39
39
  * Create a new WithExpiration object with the given value and expiration time.
40
40
  * @param value - The value to store in the cache.
@@ -42,9 +42,18 @@ type Utils<T> = {
42
42
  */
43
43
  withExpiration: (value: T, expiration: DurationObj) => WithExpiration<T>;
44
44
  };
45
+ type GetOptions<T> = {
46
+ /**
47
+ * A function that determines whether a value should be rejected from being cached.
48
+ * If the function returns true, the value will be returned but not cached.
49
+ * @param value The value to check
50
+ * @returns true if the value should be rejected, false otherwise
51
+ */
52
+ skipCachingWhen?: (value: T) => boolean;
53
+ };
45
54
  type Cache<T> = {
46
- getOrInsert: (cacheKey: string, val: (utils: Utils<T>) => T | RejectValue<T>) => T;
47
- getOrInsertAsync: (cacheKey: string, val: (utils: Utils<T>) => Promise<T | RejectValue<T>>) => Promise<T>;
55
+ getOrInsert: (cacheKey: string, val: (utils: Utils<T>) => T | SkipCaching<T>, options?: GetOptions<T>) => T;
56
+ getOrInsertAsync: (cacheKey: string, val: (utils: Utils<T>) => Promise<T | SkipCaching<T>>, options?: GetOptions<T>) => Promise<T>;
48
57
  clear: () => void;
49
58
  get: (cacheKey: string) => T | undefined;
50
59
  set: (cacheKey: string, value: T | WithExpiration<T>) => void;
@@ -60,4 +69,4 @@ type Cache<T> = {
60
69
  };
61
70
  declare function createCache<T>({ maxCacheSize, maxItemAge, expirationThrottle, }?: Options): Cache<T>;
62
71
 
63
- export { type Cache, RejectValue, WithExpiration, cachedGetter, createCache };
72
+ export { type Cache, SkipCaching, WithExpiration, cachedGetter, createCache };
package/dist/cache.js CHANGED
@@ -17,7 +17,7 @@ function cachedGetter(getter) {
17
17
  }
18
18
  };
19
19
  }
20
- var RejectValue = class {
20
+ var SkipCaching = class {
21
21
  value;
22
22
  constructor(value) {
23
23
  this.value = value;
@@ -82,20 +82,23 @@ function createCache({
82
82
  return { value, timestamp: now, expiration: void 0 };
83
83
  }
84
84
  const utils = {
85
- reject: (value) => new RejectValue(value),
85
+ skipCaching: (value) => new SkipCaching(value),
86
86
  withExpiration: (value, expiration) => {
87
87
  return new WithExpiration(value, expiration);
88
88
  }
89
89
  };
90
90
  return {
91
- getOrInsert(cacheKey, val) {
91
+ getOrInsert(cacheKey, val, options) {
92
92
  const now = Date.now();
93
93
  const entry = cache.get(cacheKey);
94
94
  if (!entry || isExpired(entry, now)) {
95
95
  const value = val(utils);
96
- if (value instanceof RejectValue) {
96
+ if (value instanceof SkipCaching) {
97
97
  return value.value;
98
98
  }
99
+ if (options?.skipCachingWhen?.(value)) {
100
+ return value;
101
+ }
99
102
  const unwrappedValue = unwrapValue(value, now);
100
103
  cache.set(cacheKey, unwrappedValue);
101
104
  trimToSize();
@@ -109,7 +112,7 @@ function createCache({
109
112
  }
110
113
  return entry.value;
111
114
  },
112
- async getOrInsertAsync(cacheKey, val) {
115
+ async getOrInsertAsync(cacheKey, val, options) {
113
116
  const entry = cache.get(cacheKey);
114
117
  if (entry && isPromise(entry.value)) {
115
118
  return entry.value;
@@ -119,13 +122,20 @@ function createCache({
119
122
  return entry.value;
120
123
  }
121
124
  const promise = val(utils).then((result) => {
122
- if (result instanceof RejectValue) {
125
+ if (result instanceof SkipCaching) {
123
126
  const cacheValue = cache.get(cacheKey);
124
127
  if (cacheValue?.value === promise) {
125
128
  cache.delete(cacheKey);
126
129
  }
127
130
  return result.value;
128
131
  }
132
+ if (options?.skipCachingWhen?.(result)) {
133
+ const cacheValue = cache.get(cacheKey);
134
+ if (cacheValue?.value === promise) {
135
+ cache.delete(cacheKey);
136
+ }
137
+ return result;
138
+ }
129
139
  const unwrappedValue = unwrapValue(result, Date.now());
130
140
  cache.set(cacheKey, unwrappedValue);
131
141
  return unwrappedValue.value;
@@ -169,7 +179,7 @@ function createCache({
169
179
  },
170
180
  async setAsync(cacheKey, value) {
171
181
  const promise = value(utils).then((result) => {
172
- if (result instanceof RejectValue) {
182
+ if (result instanceof SkipCaching) {
173
183
  const cacheValue = cache.get(cacheKey);
174
184
  if (cacheValue?.value === promise) {
175
185
  cache.delete(cacheKey);
@@ -198,7 +208,7 @@ function createCache({
198
208
  };
199
209
  }
200
210
  export {
201
- RejectValue,
211
+ SkipCaching,
202
212
  WithExpiration,
203
213
  cachedGetter,
204
214
  createCache
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "2.13.0",
4
+ "version": "2.14.1",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"
@@ -322,13 +322,13 @@
322
322
  }
323
323
  },
324
324
  "scripts": {
325
- "test": "vitest --ui",
326
- "test:run": "vitest run",
325
+ "test:ui": "vitest --ui",
326
+ "test": "vitest run",
327
327
  "lint": "pnpm tsc && pnpm eslint",
328
328
  "tsc": "tsc -p tsconfig.prod.json",
329
329
  "tsc:watch": "tsc -p tsconfig.prod.json --watch",
330
330
  "eslint": "CI=true eslint src/ scripts/ --color --max-warnings=0",
331
- "build": "pnpm test:run && pnpm lint && pnpm build:no-test && pnpm build:update-exports",
331
+ "build": "pnpm test && pnpm lint && pnpm build:no-test && pnpm build:update-exports",
332
332
  "build:no-test": "tsup",
333
333
  "build:update-exports": "tsm --no-warnings scripts/updatePackageExports.ts",
334
334
  "build-test": "tsup --config tsup.test.config.ts",