@ls-stack/utils 2.11.0 → 2.13.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 CHANGED
@@ -20,6 +20,8 @@ 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,
24
+ WithExpiration: () => WithExpiration,
23
25
  cachedGetter: () => cachedGetter,
24
26
  createCache: () => createCache
25
27
  });
@@ -33,6 +35,22 @@ function isPromise(value) {
33
35
  return isObject(value) && "then" in value;
34
36
  }
35
37
 
38
+ // src/time.ts
39
+ var MINUTE_AS_MS = 60 * 1e3;
40
+ var HOUR_AS_MS = 60 * MINUTE_AS_MS;
41
+ var DAY_AS_MS = 24 * HOUR_AS_MS;
42
+ var WEEK_AS_MS = 7 * DAY_AS_MS;
43
+ var MONTH_AS_MS = 30 * DAY_AS_MS;
44
+ var YEAR_AS_MS = 365 * DAY_AS_MS;
45
+ var HOUR_AS_SECS = 60 * 60;
46
+ var DAY_AS_SECS = 24 * HOUR_AS_SECS;
47
+ var WEEK_AS_SECS = 7 * DAY_AS_SECS;
48
+ var MONTH_AS_SECS = 30 * DAY_AS_SECS;
49
+ var YEAR_AS_SECS = 365 * DAY_AS_SECS;
50
+ function durationObjToMs(durationObj) {
51
+ return (durationObj.hours ?? 0) * HOUR_AS_MS + (durationObj.minutes ?? 0) * MINUTE_AS_MS + (durationObj.seconds ?? 0) * 1e3 + (durationObj.ms ?? 0) + (durationObj.days ?? 0) * DAY_AS_MS;
52
+ }
53
+
36
54
  // src/cache.ts
37
55
  function cachedGetter(getter) {
38
56
  return {
@@ -43,6 +61,24 @@ function cachedGetter(getter) {
43
61
  }
44
62
  };
45
63
  }
64
+ var RejectValue = class {
65
+ value;
66
+ constructor(value) {
67
+ this.value = value;
68
+ }
69
+ };
70
+ var WithExpiration = class {
71
+ value;
72
+ expiration;
73
+ /**
74
+ * @param value - The value to store in the cache.
75
+ * @param expiration - The expiration time of the value in seconds or a duration object.
76
+ */
77
+ constructor(value, expiration) {
78
+ this.value = value;
79
+ this.expiration = durationObjToMs(expiration);
80
+ }
81
+ };
46
82
  function createCache({
47
83
  maxCacheSize = 1e3,
48
84
  maxItemAge,
@@ -50,13 +86,14 @@ function createCache({
50
86
  } = {}) {
51
87
  const cache = /* @__PURE__ */ new Map();
52
88
  let lastExpirationCheck = 0;
89
+ const defaultMaxItemAgeMs = maxItemAge && durationObjToMs(maxItemAge);
53
90
  function cleanExpiredItems() {
54
91
  const now = Date.now();
55
- if (!maxItemAge || now - lastExpirationCheck < expirationThrottle) return;
92
+ if (!defaultMaxItemAgeMs || now - lastExpirationCheck < expirationThrottle)
93
+ return;
56
94
  lastExpirationCheck = now;
57
- const maxAgeMs = maxItemAge * 1e3;
58
95
  for (const [key, item] of cache.entries()) {
59
- if (now - item.timestamp > maxAgeMs) {
96
+ if (isExpired(item, now)) {
60
97
  cache.delete(key);
61
98
  }
62
99
  }
@@ -74,19 +111,40 @@ function createCache({
74
111
  }
75
112
  }
76
113
  }
77
- function isExpired(timestamp, now) {
78
- return maxItemAge !== void 0 && now - timestamp > maxItemAge * 1e3;
114
+ function isExpired(entry, now) {
115
+ const maxItemAgeMs = entry.expiration ?? defaultMaxItemAgeMs;
116
+ return !!maxItemAgeMs && now - entry.timestamp > maxItemAgeMs;
117
+ }
118
+ function unwrapValue(value, now) {
119
+ if (value instanceof WithExpiration) {
120
+ return {
121
+ value: value.value,
122
+ timestamp: now,
123
+ expiration: value.expiration ? typeof value.expiration === "number" ? value.expiration : now + durationObjToMs(value.expiration) : void 0
124
+ };
125
+ }
126
+ return { value, timestamp: now, expiration: void 0 };
79
127
  }
128
+ const utils = {
129
+ reject: (value) => new RejectValue(value),
130
+ withExpiration: (value, expiration) => {
131
+ return new WithExpiration(value, expiration);
132
+ }
133
+ };
80
134
  return {
81
135
  getOrInsert(cacheKey, val) {
82
136
  const now = Date.now();
83
137
  const entry = cache.get(cacheKey);
84
- if (!entry || isExpired(entry.timestamp, now)) {
85
- const value = val();
86
- cache.set(cacheKey, { value, timestamp: now });
138
+ if (!entry || isExpired(entry, now)) {
139
+ const value = val(utils);
140
+ if (value instanceof RejectValue) {
141
+ return value.value;
142
+ }
143
+ const unwrappedValue = unwrapValue(value, now);
144
+ cache.set(cacheKey, unwrappedValue);
87
145
  trimToSize();
88
146
  cleanExpiredItems();
89
- return value;
147
+ return unwrappedValue.value;
90
148
  }
91
149
  if (isPromise(entry.value)) {
92
150
  throw new Error(
@@ -101,17 +159,29 @@ function createCache({
101
159
  return entry.value;
102
160
  }
103
161
  const now = Date.now();
104
- if (entry && !isExpired(entry.timestamp, now)) {
162
+ if (entry && !isExpired(entry, now)) {
105
163
  return entry.value;
106
164
  }
107
- const promise = val().then((result) => {
108
- cache.set(cacheKey, { value: result, timestamp: Date.now() });
109
- return result;
165
+ const promise = val(utils).then((result) => {
166
+ if (result instanceof RejectValue) {
167
+ const cacheValue = cache.get(cacheKey);
168
+ if (cacheValue?.value === promise) {
169
+ cache.delete(cacheKey);
170
+ }
171
+ return result.value;
172
+ }
173
+ const unwrappedValue = unwrapValue(result, Date.now());
174
+ cache.set(cacheKey, unwrappedValue);
175
+ return unwrappedValue.value;
110
176
  }).catch((error) => {
111
177
  cache.delete(cacheKey);
112
178
  throw error;
113
179
  });
114
- cache.set(cacheKey, { value: promise, timestamp: now });
180
+ cache.set(cacheKey, {
181
+ value: promise,
182
+ timestamp: now,
183
+ expiration: void 0
184
+ });
115
185
  trimToSize();
116
186
  cleanExpiredItems();
117
187
  return promise;
@@ -121,7 +191,7 @@ function createCache({
121
191
  },
122
192
  get(cacheKey) {
123
193
  const entry = cache.get(cacheKey);
124
- if (!entry || isExpired(entry.timestamp, Date.now())) {
194
+ if (!entry || isExpired(entry, Date.now())) {
125
195
  return void 0;
126
196
  }
127
197
  if (isPromise(entry.value)) {
@@ -130,28 +200,41 @@ function createCache({
130
200
  return entry.value;
131
201
  },
132
202
  set(cacheKey, value) {
133
- cache.set(cacheKey, { value, timestamp: Date.now() });
203
+ cache.set(cacheKey, unwrapValue(value, Date.now()));
134
204
  trimToSize();
135
205
  cleanExpiredItems();
136
206
  },
137
207
  async getAsync(cacheKey) {
138
208
  const entry = cache.get(cacheKey);
139
- if (!entry || isExpired(entry.timestamp, Date.now())) {
209
+ if (!entry || isExpired(entry, Date.now())) {
140
210
  return void 0;
141
211
  }
142
- return await entry.value;
212
+ return entry.value;
143
213
  },
144
- setAsync(cacheKey, value) {
145
- const promise = value().then((result) => {
146
- cache.set(cacheKey, { value: result, timestamp: Date.now() });
147
- return result;
214
+ async setAsync(cacheKey, value) {
215
+ const promise = value(utils).then((result) => {
216
+ if (result instanceof RejectValue) {
217
+ const cacheValue = cache.get(cacheKey);
218
+ if (cacheValue?.value === promise) {
219
+ cache.delete(cacheKey);
220
+ }
221
+ return result.value;
222
+ }
223
+ const unwrappedValue = unwrapValue(result, Date.now());
224
+ cache.set(cacheKey, unwrappedValue);
225
+ return unwrappedValue.value;
148
226
  }).catch((error) => {
149
227
  cache.delete(cacheKey);
150
228
  throw error;
151
229
  });
152
- cache.set(cacheKey, { value: promise, timestamp: Date.now() });
230
+ cache.set(cacheKey, {
231
+ value: promise,
232
+ timestamp: Date.now(),
233
+ expiration: void 0
234
+ });
153
235
  trimToSize();
154
236
  cleanExpiredItems();
237
+ return promise;
155
238
  },
156
239
  cleanExpiredItems,
157
240
  /** @internal */
@@ -160,6 +243,8 @@ function createCache({
160
243
  }
161
244
  // Annotate the CommonJS export names for ESM import in node:
162
245
  0 && (module.exports = {
246
+ RejectValue,
247
+ WithExpiration,
163
248
  cachedGetter,
164
249
  createCache
165
250
  });
package/dist/cache.d.cts CHANGED
@@ -1,3 +1,5 @@
1
+ import { DurationObj } from './time.cjs';
2
+
1
3
  declare function cachedGetter<T>(getter: () => T): {
2
4
  value: T;
3
5
  };
@@ -8,9 +10,9 @@ type Options = {
8
10
  */
9
11
  maxCacheSize?: number;
10
12
  /**
11
- * The maximum age of items in the cache in seconds.
13
+ * The maximum age of items in the cache.
12
14
  */
13
- maxItemAge?: number;
15
+ maxItemAge?: DurationObj;
14
16
  /**
15
17
  * The throttle for checking expired items in milliseconds.
16
18
  * @default
@@ -18,15 +20,37 @@ type Options = {
18
20
  */
19
21
  expirationThrottle?: number;
20
22
  };
23
+ declare class RejectValue<T> {
24
+ value: T;
25
+ constructor(value: T);
26
+ }
27
+ declare class WithExpiration<T> {
28
+ value: T;
29
+ expiration: number;
30
+ /**
31
+ * @param value - The value to store in the cache.
32
+ * @param expiration - The expiration time of the value in seconds or a duration object.
33
+ */
34
+ constructor(value: T, expiration: DurationObj);
35
+ }
36
+ type Utils<T> = {
37
+ reject: (value: T) => RejectValue<T>;
38
+ /**
39
+ * Create a new WithExpiration object with the given value and expiration time.
40
+ * @param value - The value to store in the cache.
41
+ * @param expiration - The expiration time of the value in seconds or a duration object.
42
+ */
43
+ withExpiration: (value: T, expiration: DurationObj) => WithExpiration<T>;
44
+ };
21
45
  type Cache<T> = {
22
- getOrInsert: (cacheKey: string, val: () => T) => T;
23
- getOrInsertAsync: (cacheKey: string, val: () => Promise<T>) => Promise<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>;
24
48
  clear: () => void;
25
49
  get: (cacheKey: string) => T | undefined;
26
- set: (cacheKey: string, value: T) => void;
50
+ set: (cacheKey: string, value: T | WithExpiration<T>) => void;
27
51
  cleanExpiredItems: () => void;
28
52
  getAsync: (cacheKey: string) => Promise<T | undefined>;
29
- setAsync: (cacheKey: string, value: () => Promise<T>) => void;
53
+ setAsync: (cacheKey: string, value: (utils: Utils<T>) => Promise<T | WithExpiration<T>>) => Promise<T>;
30
54
  [' cache']: {
31
55
  map: Map<string, {
32
56
  value: T | Promise<T>;
@@ -36,4 +60,4 @@ type Cache<T> = {
36
60
  };
37
61
  declare function createCache<T>({ maxCacheSize, maxItemAge, expirationThrottle, }?: Options): Cache<T>;
38
62
 
39
- export { type Cache, cachedGetter, createCache };
63
+ export { type Cache, RejectValue, WithExpiration, cachedGetter, createCache };
package/dist/cache.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { DurationObj } from './time.js';
2
+
1
3
  declare function cachedGetter<T>(getter: () => T): {
2
4
  value: T;
3
5
  };
@@ -8,9 +10,9 @@ type Options = {
8
10
  */
9
11
  maxCacheSize?: number;
10
12
  /**
11
- * The maximum age of items in the cache in seconds.
13
+ * The maximum age of items in the cache.
12
14
  */
13
- maxItemAge?: number;
15
+ maxItemAge?: DurationObj;
14
16
  /**
15
17
  * The throttle for checking expired items in milliseconds.
16
18
  * @default
@@ -18,15 +20,37 @@ type Options = {
18
20
  */
19
21
  expirationThrottle?: number;
20
22
  };
23
+ declare class RejectValue<T> {
24
+ value: T;
25
+ constructor(value: T);
26
+ }
27
+ declare class WithExpiration<T> {
28
+ value: T;
29
+ expiration: number;
30
+ /**
31
+ * @param value - The value to store in the cache.
32
+ * @param expiration - The expiration time of the value in seconds or a duration object.
33
+ */
34
+ constructor(value: T, expiration: DurationObj);
35
+ }
36
+ type Utils<T> = {
37
+ reject: (value: T) => RejectValue<T>;
38
+ /**
39
+ * Create a new WithExpiration object with the given value and expiration time.
40
+ * @param value - The value to store in the cache.
41
+ * @param expiration - The expiration time of the value in seconds or a duration object.
42
+ */
43
+ withExpiration: (value: T, expiration: DurationObj) => WithExpiration<T>;
44
+ };
21
45
  type Cache<T> = {
22
- getOrInsert: (cacheKey: string, val: () => T) => T;
23
- getOrInsertAsync: (cacheKey: string, val: () => Promise<T>) => Promise<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>;
24
48
  clear: () => void;
25
49
  get: (cacheKey: string) => T | undefined;
26
- set: (cacheKey: string, value: T) => void;
50
+ set: (cacheKey: string, value: T | WithExpiration<T>) => void;
27
51
  cleanExpiredItems: () => void;
28
52
  getAsync: (cacheKey: string) => Promise<T | undefined>;
29
- setAsync: (cacheKey: string, value: () => Promise<T>) => void;
53
+ setAsync: (cacheKey: string, value: (utils: Utils<T>) => Promise<T | WithExpiration<T>>) => Promise<T>;
30
54
  [' cache']: {
31
55
  map: Map<string, {
32
56
  value: T | Promise<T>;
@@ -36,4 +60,4 @@ type Cache<T> = {
36
60
  };
37
61
  declare function createCache<T>({ maxCacheSize, maxItemAge, expirationThrottle, }?: Options): Cache<T>;
38
62
 
39
- export { type Cache, cachedGetter, createCache };
63
+ export { type Cache, RejectValue, WithExpiration, cachedGetter, createCache };
package/dist/cache.js CHANGED
@@ -1,6 +1,11 @@
1
+ import {
2
+ durationObjToMs
3
+ } from "./chunk-RK6PT7JY.js";
4
+ import "./chunk-HTCYUMDR.js";
1
5
  import {
2
6
  isPromise
3
7
  } from "./chunk-4UGSP3L3.js";
8
+ import "./chunk-GBFS2I67.js";
4
9
 
5
10
  // src/cache.ts
6
11
  function cachedGetter(getter) {
@@ -12,6 +17,24 @@ function cachedGetter(getter) {
12
17
  }
13
18
  };
14
19
  }
20
+ var RejectValue = class {
21
+ value;
22
+ constructor(value) {
23
+ this.value = value;
24
+ }
25
+ };
26
+ var WithExpiration = class {
27
+ value;
28
+ expiration;
29
+ /**
30
+ * @param value - The value to store in the cache.
31
+ * @param expiration - The expiration time of the value in seconds or a duration object.
32
+ */
33
+ constructor(value, expiration) {
34
+ this.value = value;
35
+ this.expiration = durationObjToMs(expiration);
36
+ }
37
+ };
15
38
  function createCache({
16
39
  maxCacheSize = 1e3,
17
40
  maxItemAge,
@@ -19,13 +42,14 @@ function createCache({
19
42
  } = {}) {
20
43
  const cache = /* @__PURE__ */ new Map();
21
44
  let lastExpirationCheck = 0;
45
+ const defaultMaxItemAgeMs = maxItemAge && durationObjToMs(maxItemAge);
22
46
  function cleanExpiredItems() {
23
47
  const now = Date.now();
24
- if (!maxItemAge || now - lastExpirationCheck < expirationThrottle) return;
48
+ if (!defaultMaxItemAgeMs || now - lastExpirationCheck < expirationThrottle)
49
+ return;
25
50
  lastExpirationCheck = now;
26
- const maxAgeMs = maxItemAge * 1e3;
27
51
  for (const [key, item] of cache.entries()) {
28
- if (now - item.timestamp > maxAgeMs) {
52
+ if (isExpired(item, now)) {
29
53
  cache.delete(key);
30
54
  }
31
55
  }
@@ -43,19 +67,40 @@ function createCache({
43
67
  }
44
68
  }
45
69
  }
46
- function isExpired(timestamp, now) {
47
- return maxItemAge !== void 0 && now - timestamp > maxItemAge * 1e3;
70
+ function isExpired(entry, now) {
71
+ const maxItemAgeMs = entry.expiration ?? defaultMaxItemAgeMs;
72
+ return !!maxItemAgeMs && now - entry.timestamp > maxItemAgeMs;
73
+ }
74
+ function unwrapValue(value, now) {
75
+ if (value instanceof WithExpiration) {
76
+ return {
77
+ value: value.value,
78
+ timestamp: now,
79
+ expiration: value.expiration ? typeof value.expiration === "number" ? value.expiration : now + durationObjToMs(value.expiration) : void 0
80
+ };
81
+ }
82
+ return { value, timestamp: now, expiration: void 0 };
48
83
  }
84
+ const utils = {
85
+ reject: (value) => new RejectValue(value),
86
+ withExpiration: (value, expiration) => {
87
+ return new WithExpiration(value, expiration);
88
+ }
89
+ };
49
90
  return {
50
91
  getOrInsert(cacheKey, val) {
51
92
  const now = Date.now();
52
93
  const entry = cache.get(cacheKey);
53
- if (!entry || isExpired(entry.timestamp, now)) {
54
- const value = val();
55
- cache.set(cacheKey, { value, timestamp: now });
94
+ if (!entry || isExpired(entry, now)) {
95
+ const value = val(utils);
96
+ if (value instanceof RejectValue) {
97
+ return value.value;
98
+ }
99
+ const unwrappedValue = unwrapValue(value, now);
100
+ cache.set(cacheKey, unwrappedValue);
56
101
  trimToSize();
57
102
  cleanExpiredItems();
58
- return value;
103
+ return unwrappedValue.value;
59
104
  }
60
105
  if (isPromise(entry.value)) {
61
106
  throw new Error(
@@ -70,17 +115,29 @@ function createCache({
70
115
  return entry.value;
71
116
  }
72
117
  const now = Date.now();
73
- if (entry && !isExpired(entry.timestamp, now)) {
118
+ if (entry && !isExpired(entry, now)) {
74
119
  return entry.value;
75
120
  }
76
- const promise = val().then((result) => {
77
- cache.set(cacheKey, { value: result, timestamp: Date.now() });
78
- return result;
121
+ const promise = val(utils).then((result) => {
122
+ if (result instanceof RejectValue) {
123
+ const cacheValue = cache.get(cacheKey);
124
+ if (cacheValue?.value === promise) {
125
+ cache.delete(cacheKey);
126
+ }
127
+ return result.value;
128
+ }
129
+ const unwrappedValue = unwrapValue(result, Date.now());
130
+ cache.set(cacheKey, unwrappedValue);
131
+ return unwrappedValue.value;
79
132
  }).catch((error) => {
80
133
  cache.delete(cacheKey);
81
134
  throw error;
82
135
  });
83
- cache.set(cacheKey, { value: promise, timestamp: now });
136
+ cache.set(cacheKey, {
137
+ value: promise,
138
+ timestamp: now,
139
+ expiration: void 0
140
+ });
84
141
  trimToSize();
85
142
  cleanExpiredItems();
86
143
  return promise;
@@ -90,7 +147,7 @@ function createCache({
90
147
  },
91
148
  get(cacheKey) {
92
149
  const entry = cache.get(cacheKey);
93
- if (!entry || isExpired(entry.timestamp, Date.now())) {
150
+ if (!entry || isExpired(entry, Date.now())) {
94
151
  return void 0;
95
152
  }
96
153
  if (isPromise(entry.value)) {
@@ -99,28 +156,41 @@ function createCache({
99
156
  return entry.value;
100
157
  },
101
158
  set(cacheKey, value) {
102
- cache.set(cacheKey, { value, timestamp: Date.now() });
159
+ cache.set(cacheKey, unwrapValue(value, Date.now()));
103
160
  trimToSize();
104
161
  cleanExpiredItems();
105
162
  },
106
163
  async getAsync(cacheKey) {
107
164
  const entry = cache.get(cacheKey);
108
- if (!entry || isExpired(entry.timestamp, Date.now())) {
165
+ if (!entry || isExpired(entry, Date.now())) {
109
166
  return void 0;
110
167
  }
111
- return await entry.value;
168
+ return entry.value;
112
169
  },
113
- setAsync(cacheKey, value) {
114
- const promise = value().then((result) => {
115
- cache.set(cacheKey, { value: result, timestamp: Date.now() });
116
- return result;
170
+ async setAsync(cacheKey, value) {
171
+ const promise = value(utils).then((result) => {
172
+ if (result instanceof RejectValue) {
173
+ const cacheValue = cache.get(cacheKey);
174
+ if (cacheValue?.value === promise) {
175
+ cache.delete(cacheKey);
176
+ }
177
+ return result.value;
178
+ }
179
+ const unwrappedValue = unwrapValue(result, Date.now());
180
+ cache.set(cacheKey, unwrappedValue);
181
+ return unwrappedValue.value;
117
182
  }).catch((error) => {
118
183
  cache.delete(cacheKey);
119
184
  throw error;
120
185
  });
121
- cache.set(cacheKey, { value: promise, timestamp: Date.now() });
186
+ cache.set(cacheKey, {
187
+ value: promise,
188
+ timestamp: Date.now(),
189
+ expiration: void 0
190
+ });
122
191
  trimToSize();
123
192
  cleanExpiredItems();
193
+ return promise;
124
194
  },
125
195
  cleanExpiredItems,
126
196
  /** @internal */
@@ -128,6 +198,8 @@ function createCache({
128
198
  };
129
199
  }
130
200
  export {
201
+ RejectValue,
202
+ WithExpiration,
131
203
  cachedGetter,
132
204
  createCache
133
205
  };
@@ -0,0 +1,96 @@
1
+ import {
2
+ clampMax
3
+ } from "./chunk-HTCYUMDR.js";
4
+ import {
5
+ castToNumber
6
+ } from "./chunk-GBFS2I67.js";
7
+
8
+ // src/time.ts
9
+ var MINUTE_AS_MS = 60 * 1e3;
10
+ var HOUR_AS_MS = 60 * MINUTE_AS_MS;
11
+ var DAY_AS_MS = 24 * HOUR_AS_MS;
12
+ var WEEK_AS_MS = 7 * DAY_AS_MS;
13
+ var MONTH_AS_MS = 30 * DAY_AS_MS;
14
+ var YEAR_AS_MS = 365 * DAY_AS_MS;
15
+ var HOUR_AS_SECS = 60 * 60;
16
+ var DAY_AS_SECS = 24 * HOUR_AS_SECS;
17
+ var WEEK_AS_SECS = 7 * DAY_AS_SECS;
18
+ var MONTH_AS_SECS = 30 * DAY_AS_SECS;
19
+ var YEAR_AS_SECS = 365 * DAY_AS_SECS;
20
+ function dateStringOrNullToUnixMs(isoString) {
21
+ if (!isoString) return null;
22
+ const unixMs = new Date(isoString).getTime();
23
+ if (isNaN(unixMs)) return null;
24
+ return unixMs;
25
+ }
26
+ function msToTimeString(ms, format, hoursMinLength = 2) {
27
+ const { hours, minutes, seconds, milliseconds } = msToDurationObj(ms);
28
+ const hoursString = padTimeVal(hours, hoursMinLength);
29
+ const minutesString = padTimeVal(minutes);
30
+ if (format === "minutes") {
31
+ return `${hoursString}:${minutesString}`;
32
+ }
33
+ const secondsString = padTimeVal(seconds);
34
+ if (format === "seconds") {
35
+ return `${hoursString}:${minutesString}:${secondsString}`;
36
+ }
37
+ return `${hoursString}:${minutesString}:${secondsString}:${padTimeVal(
38
+ milliseconds,
39
+ 3
40
+ )}`;
41
+ }
42
+ function padTimeVal(val, maxLength = 2) {
43
+ return val.toString().padStart(maxLength, "0");
44
+ }
45
+ function parseTimeStringToMs(timeString) {
46
+ if (!timeString.trim()) return 0;
47
+ const [hours, minutes, seconds, ms] = timeString.split(":");
48
+ return getTimeStringPartToInt(hours) * HOUR_AS_MS + clampMax(getTimeStringPartToInt(minutes), 59) * MINUTE_AS_MS + clampMax(getTimeStringPartToInt(seconds), 59) * 1e3 + getTimeStringPartToInt(ms, 3);
49
+ }
50
+ function getTimeStringPartToInt(timeStringPart, length) {
51
+ if (!timeStringPart?.trim()) return 0;
52
+ let string = timeStringPart.replaceAll("_", "0");
53
+ string = string.replaceAll("-", "");
54
+ if (length) {
55
+ string = string.padEnd(length, "0");
56
+ if (string.length > length) {
57
+ string = string.slice(0, length);
58
+ }
59
+ }
60
+ const num = castToNumber(string);
61
+ if (!num) return 0;
62
+ return Math.floor(num);
63
+ }
64
+ function msToDurationObj(ms) {
65
+ return {
66
+ milliseconds: ms % 1e3,
67
+ seconds: Math.floor(ms / 1e3) % 60,
68
+ minutes: Math.floor(ms / 1e3 / 60) % 60,
69
+ hours: Math.floor(ms / 1e3 / 60 / 60)
70
+ };
71
+ }
72
+ function getUnixSeconds() {
73
+ return Math.floor(Date.now() / 1e3);
74
+ }
75
+ function durationObjToMs(durationObj) {
76
+ return (durationObj.hours ?? 0) * HOUR_AS_MS + (durationObj.minutes ?? 0) * MINUTE_AS_MS + (durationObj.seconds ?? 0) * 1e3 + (durationObj.ms ?? 0) + (durationObj.days ?? 0) * DAY_AS_MS;
77
+ }
78
+
79
+ export {
80
+ MINUTE_AS_MS,
81
+ HOUR_AS_MS,
82
+ DAY_AS_MS,
83
+ WEEK_AS_MS,
84
+ MONTH_AS_MS,
85
+ YEAR_AS_MS,
86
+ HOUR_AS_SECS,
87
+ DAY_AS_SECS,
88
+ WEEK_AS_SECS,
89
+ MONTH_AS_SECS,
90
+ YEAR_AS_SECS,
91
+ dateStringOrNullToUnixMs,
92
+ msToTimeString,
93
+ parseTimeStringToMs,
94
+ getUnixSeconds,
95
+ durationObjToMs
96
+ };
@@ -121,22 +121,29 @@ var EnhancedMap = class _EnhancedMap extends Map {
121
121
  }
122
122
  };
123
123
 
124
+ // src/time.ts
125
+ var MINUTE_AS_MS = 60 * 1e3;
126
+ var HOUR_AS_MS = 60 * MINUTE_AS_MS;
127
+ var DAY_AS_MS = 24 * HOUR_AS_MS;
128
+ var WEEK_AS_MS = 7 * DAY_AS_MS;
129
+ var MONTH_AS_MS = 30 * DAY_AS_MS;
130
+ var YEAR_AS_MS = 365 * DAY_AS_MS;
131
+ var HOUR_AS_SECS = 60 * 60;
132
+ var DAY_AS_SECS = 24 * HOUR_AS_SECS;
133
+ var WEEK_AS_SECS = 7 * DAY_AS_SECS;
134
+ var MONTH_AS_SECS = 30 * DAY_AS_SECS;
135
+ var YEAR_AS_SECS = 365 * DAY_AS_SECS;
136
+ function durationObjToMs(durationObj) {
137
+ return (durationObj.hours ?? 0) * HOUR_AS_MS + (durationObj.minutes ?? 0) * MINUTE_AS_MS + (durationObj.seconds ?? 0) * 1e3 + (durationObj.ms ?? 0) + (durationObj.days ?? 0) * DAY_AS_MS;
138
+ }
139
+
124
140
  // src/createThrottleController.ts
125
141
  function createThrottleController({
126
142
  maxCalls,
127
143
  per,
128
144
  cleanupCheckSecsInterval = 60 * 30
129
145
  }) {
130
- let msInterval = 0;
131
- if (per.ms) {
132
- msInterval = per.ms;
133
- } else if (per.seconds) {
134
- msInterval = per.seconds * 1e3;
135
- } else if (per.minutes) {
136
- msInterval = per.minutes * 1e3 * 60;
137
- } else if (per.hours) {
138
- msInterval = per.hours * 1e3 * 60 * 60;
139
- }
146
+ const msInterval = durationObjToMs(per);
140
147
  if (msInterval === 0) {
141
148
  throw new Error("Invalid interval");
142
149
  }
@@ -1,11 +1,8 @@
1
+ import { DurationObj } from './time.cjs';
2
+
1
3
  type Options = {
2
4
  maxCalls: number;
3
- per: {
4
- ms?: number;
5
- seconds?: number;
6
- minutes?: number;
7
- hours?: number;
8
- };
5
+ per: DurationObj;
9
6
  cleanupCheckSecsInterval?: number;
10
7
  };
11
8
  type ThrottleController = {
@@ -1,11 +1,8 @@
1
+ import { DurationObj } from './time.js';
2
+
1
3
  type Options = {
2
4
  maxCalls: number;
3
- per: {
4
- ms?: number;
5
- seconds?: number;
6
- minutes?: number;
7
- hours?: number;
8
- };
5
+ per: DurationObj;
9
6
  cleanupCheckSecsInterval?: number;
10
7
  };
11
8
  type ThrottleController = {
@@ -1,7 +1,12 @@
1
1
  import {
2
2
  EnhancedMap
3
3
  } from "./chunk-T5WDDPFI.js";
4
+ import {
5
+ durationObjToMs
6
+ } from "./chunk-RK6PT7JY.js";
7
+ import "./chunk-HTCYUMDR.js";
4
8
  import "./chunk-4UGSP3L3.js";
9
+ import "./chunk-GBFS2I67.js";
5
10
 
6
11
  // src/createThrottleController.ts
7
12
  function createThrottleController({
@@ -9,16 +14,7 @@ function createThrottleController({
9
14
  per,
10
15
  cleanupCheckSecsInterval = 60 * 30
11
16
  }) {
12
- let msInterval = 0;
13
- if (per.ms) {
14
- msInterval = per.ms;
15
- } else if (per.seconds) {
16
- msInterval = per.seconds * 1e3;
17
- } else if (per.minutes) {
18
- msInterval = per.minutes * 1e3 * 60;
19
- } else if (per.hours) {
20
- msInterval = per.hours * 1e3 * 60 * 60;
21
- }
17
+ const msInterval = durationObjToMs(per);
22
18
  if (msInterval === 0) {
23
19
  throw new Error("Invalid interval");
24
20
  }
package/dist/testUtils.js CHANGED
@@ -5,13 +5,13 @@ import {
5
5
  import {
6
6
  deepEqual
7
7
  } from "./chunk-JQFUKJU5.js";
8
- import {
9
- clampMin
10
- } from "./chunk-HTCYUMDR.js";
11
8
  import {
12
9
  arrayWithPrevAndIndex,
13
10
  filterAndMap
14
11
  } from "./chunk-QMFZE2VO.js";
12
+ import {
13
+ clampMin
14
+ } from "./chunk-HTCYUMDR.js";
15
15
  import {
16
16
  isObject
17
17
  } from "./chunk-4UGSP3L3.js";
package/dist/time.cjs CHANGED
@@ -32,6 +32,7 @@ __export(time_exports, {
32
32
  YEAR_AS_MS: () => YEAR_AS_MS,
33
33
  YEAR_AS_SECS: () => YEAR_AS_SECS,
34
34
  dateStringOrNullToUnixMs: () => dateStringOrNullToUnixMs,
35
+ durationObjToMs: () => durationObjToMs,
35
36
  getUnixSeconds: () => getUnixSeconds,
36
37
  msToTimeString: () => msToTimeString,
37
38
  parseTimeStringToMs: () => parseTimeStringToMs
@@ -119,6 +120,9 @@ function msToDurationObj(ms) {
119
120
  function getUnixSeconds() {
120
121
  return Math.floor(Date.now() / 1e3);
121
122
  }
123
+ function durationObjToMs(durationObj) {
124
+ return (durationObj.hours ?? 0) * HOUR_AS_MS + (durationObj.minutes ?? 0) * MINUTE_AS_MS + (durationObj.seconds ?? 0) * 1e3 + (durationObj.ms ?? 0) + (durationObj.days ?? 0) * DAY_AS_MS;
125
+ }
122
126
  // Annotate the CommonJS export names for ESM import in node:
123
127
  0 && (module.exports = {
124
128
  DAY_AS_MS,
@@ -133,6 +137,7 @@ function getUnixSeconds() {
133
137
  YEAR_AS_MS,
134
138
  YEAR_AS_SECS,
135
139
  dateStringOrNullToUnixMs,
140
+ durationObjToMs,
136
141
  getUnixSeconds,
137
142
  msToTimeString,
138
143
  parseTimeStringToMs
package/dist/time.d.cts CHANGED
@@ -13,5 +13,13 @@ declare function dateStringOrNullToUnixMs(isoString: string | null | undefined):
13
13
  declare function msToTimeString(ms: number, format: 'minutes' | 'seconds' | 'milliseconds', hoursMinLength?: number): string;
14
14
  declare function parseTimeStringToMs(timeString: string): number;
15
15
  declare function getUnixSeconds(): number;
16
+ type DurationObj = {
17
+ ms?: number;
18
+ seconds?: number;
19
+ minutes?: number;
20
+ hours?: number;
21
+ days?: number;
22
+ };
23
+ declare function durationObjToMs(durationObj: DurationObj): number;
16
24
 
17
- export { DAY_AS_MS, DAY_AS_SECS, HOUR_AS_MS, HOUR_AS_SECS, MINUTE_AS_MS, MONTH_AS_MS, MONTH_AS_SECS, WEEK_AS_MS, WEEK_AS_SECS, YEAR_AS_MS, YEAR_AS_SECS, dateStringOrNullToUnixMs, getUnixSeconds, msToTimeString, parseTimeStringToMs };
25
+ export { DAY_AS_MS, DAY_AS_SECS, type DurationObj, HOUR_AS_MS, HOUR_AS_SECS, MINUTE_AS_MS, MONTH_AS_MS, MONTH_AS_SECS, WEEK_AS_MS, WEEK_AS_SECS, YEAR_AS_MS, YEAR_AS_SECS, dateStringOrNullToUnixMs, durationObjToMs, getUnixSeconds, msToTimeString, parseTimeStringToMs };
package/dist/time.d.ts CHANGED
@@ -13,5 +13,13 @@ declare function dateStringOrNullToUnixMs(isoString: string | null | undefined):
13
13
  declare function msToTimeString(ms: number, format: 'minutes' | 'seconds' | 'milliseconds', hoursMinLength?: number): string;
14
14
  declare function parseTimeStringToMs(timeString: string): number;
15
15
  declare function getUnixSeconds(): number;
16
+ type DurationObj = {
17
+ ms?: number;
18
+ seconds?: number;
19
+ minutes?: number;
20
+ hours?: number;
21
+ days?: number;
22
+ };
23
+ declare function durationObjToMs(durationObj: DurationObj): number;
16
24
 
17
- export { DAY_AS_MS, DAY_AS_SECS, HOUR_AS_MS, HOUR_AS_SECS, MINUTE_AS_MS, MONTH_AS_MS, MONTH_AS_SECS, WEEK_AS_MS, WEEK_AS_SECS, YEAR_AS_MS, YEAR_AS_SECS, dateStringOrNullToUnixMs, getUnixSeconds, msToTimeString, parseTimeStringToMs };
25
+ export { DAY_AS_MS, DAY_AS_SECS, type DurationObj, HOUR_AS_MS, HOUR_AS_SECS, MINUTE_AS_MS, MONTH_AS_MS, MONTH_AS_SECS, WEEK_AS_MS, WEEK_AS_SECS, YEAR_AS_MS, YEAR_AS_SECS, dateStringOrNullToUnixMs, durationObjToMs, getUnixSeconds, msToTimeString, parseTimeStringToMs };
package/dist/time.js CHANGED
@@ -1,77 +1,23 @@
1
1
  import {
2
- clampMax
3
- } from "./chunk-HTCYUMDR.js";
4
- import {
5
- castToNumber
6
- } from "./chunk-GBFS2I67.js";
7
-
8
- // src/time.ts
9
- var MINUTE_AS_MS = 60 * 1e3;
10
- var HOUR_AS_MS = 60 * MINUTE_AS_MS;
11
- var DAY_AS_MS = 24 * HOUR_AS_MS;
12
- var WEEK_AS_MS = 7 * DAY_AS_MS;
13
- var MONTH_AS_MS = 30 * DAY_AS_MS;
14
- var YEAR_AS_MS = 365 * DAY_AS_MS;
15
- var HOUR_AS_SECS = 60 * 60;
16
- var DAY_AS_SECS = 24 * HOUR_AS_SECS;
17
- var WEEK_AS_SECS = 7 * DAY_AS_SECS;
18
- var MONTH_AS_SECS = 30 * DAY_AS_SECS;
19
- var YEAR_AS_SECS = 365 * DAY_AS_SECS;
20
- function dateStringOrNullToUnixMs(isoString) {
21
- if (!isoString) return null;
22
- const unixMs = new Date(isoString).getTime();
23
- if (isNaN(unixMs)) return null;
24
- return unixMs;
25
- }
26
- function msToTimeString(ms, format, hoursMinLength = 2) {
27
- const { hours, minutes, seconds, milliseconds } = msToDurationObj(ms);
28
- const hoursString = padTimeVal(hours, hoursMinLength);
29
- const minutesString = padTimeVal(minutes);
30
- if (format === "minutes") {
31
- return `${hoursString}:${minutesString}`;
32
- }
33
- const secondsString = padTimeVal(seconds);
34
- if (format === "seconds") {
35
- return `${hoursString}:${minutesString}:${secondsString}`;
36
- }
37
- return `${hoursString}:${minutesString}:${secondsString}:${padTimeVal(
38
- milliseconds,
39
- 3
40
- )}`;
41
- }
42
- function padTimeVal(val, maxLength = 2) {
43
- return val.toString().padStart(maxLength, "0");
44
- }
45
- function parseTimeStringToMs(timeString) {
46
- if (!timeString.trim()) return 0;
47
- const [hours, minutes, seconds, ms] = timeString.split(":");
48
- return getTimeStringPartToInt(hours) * HOUR_AS_MS + clampMax(getTimeStringPartToInt(minutes), 59) * MINUTE_AS_MS + clampMax(getTimeStringPartToInt(seconds), 59) * 1e3 + getTimeStringPartToInt(ms, 3);
49
- }
50
- function getTimeStringPartToInt(timeStringPart, length) {
51
- if (!timeStringPart?.trim()) return 0;
52
- let string = timeStringPart.replaceAll("_", "0");
53
- string = string.replaceAll("-", "");
54
- if (length) {
55
- string = string.padEnd(length, "0");
56
- if (string.length > length) {
57
- string = string.slice(0, length);
58
- }
59
- }
60
- const num = castToNumber(string);
61
- if (!num) return 0;
62
- return Math.floor(num);
63
- }
64
- function msToDurationObj(ms) {
65
- return {
66
- milliseconds: ms % 1e3,
67
- seconds: Math.floor(ms / 1e3) % 60,
68
- minutes: Math.floor(ms / 1e3 / 60) % 60,
69
- hours: Math.floor(ms / 1e3 / 60 / 60)
70
- };
71
- }
72
- function getUnixSeconds() {
73
- return Math.floor(Date.now() / 1e3);
74
- }
2
+ DAY_AS_MS,
3
+ DAY_AS_SECS,
4
+ HOUR_AS_MS,
5
+ HOUR_AS_SECS,
6
+ MINUTE_AS_MS,
7
+ MONTH_AS_MS,
8
+ MONTH_AS_SECS,
9
+ WEEK_AS_MS,
10
+ WEEK_AS_SECS,
11
+ YEAR_AS_MS,
12
+ YEAR_AS_SECS,
13
+ dateStringOrNullToUnixMs,
14
+ durationObjToMs,
15
+ getUnixSeconds,
16
+ msToTimeString,
17
+ parseTimeStringToMs
18
+ } from "./chunk-RK6PT7JY.js";
19
+ import "./chunk-HTCYUMDR.js";
20
+ import "./chunk-GBFS2I67.js";
75
21
  export {
76
22
  DAY_AS_MS,
77
23
  DAY_AS_SECS,
@@ -85,6 +31,7 @@ export {
85
31
  YEAR_AS_MS,
86
32
  YEAR_AS_SECS,
87
33
  dateStringOrNullToUnixMs,
34
+ durationObjToMs,
88
35
  getUnixSeconds,
89
36
  msToTimeString,
90
37
  parseTimeStringToMs
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "2.11.0",
4
+ "version": "2.13.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"