@hyperfrontend/time-utils 0.0.1 → 0.0.3
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 +9 -0
- package/README.md +3 -4
- package/bundle/index.iife.js +207 -11
- package/bundle/index.iife.js.map +1 -1
- package/bundle/index.iife.min.js +1 -1
- package/bundle/index.iife.min.js.map +1 -1
- package/bundle/index.umd.js +207 -11
- package/bundle/index.umd.js.map +1 -1
- package/bundle/index.umd.min.js +1 -1
- package/bundle/index.umd.min.js.map +1 -1
- package/create-clock.d.ts.map +1 -1
- package/create-timer.d.ts.map +1 -1
- package/index.cjs.js +207 -11
- package/index.cjs.js.map +1 -1
- package/index.d.ts.map +1 -1
- package/index.esm.js +207 -11
- package/index.esm.js.map +1 -1
- package/normalize-to-base-time-window.d.ts.map +1 -1
- package/package.json +14 -14
- package/set-interval-callback.d.ts.map +1 -1
- package/sleep.d.ts.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,4 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
4
|
|
|
5
|
+
## [0.0.3](https://github.com/AndrewRedican/hyperfrontend/compare/lib-time-utils@0.0.2...lib-time-utils@0.0.3) (2026-03-02)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* **lib-time-utils:** correct package exports and dependencies ([3b9096b](https://github.com/AndrewRedican/hyperfrontend/commit/3b9096b54337c2c2f3e06d6b97865a772e79aad9))
|
|
11
|
+
|
|
12
|
+
## [0.0.2](https://github.com/AndrewRedican/hyperfrontend/compare/lib-time-utils@0.0.1...lib-time-utils@0.0.2) (2026-02-26)
|
|
13
|
+
|
|
5
14
|
## 0.0.1 (2026-02-15)
|
package/README.md
CHANGED
|
@@ -26,9 +26,6 @@
|
|
|
26
26
|
<a href="https://github.com/AndrewRedican/hyperfrontend">
|
|
27
27
|
<img src="https://img.shields.io/github/stars/AndrewRedican/hyperfrontend?style=flat-square" alt="GitHub stars">
|
|
28
28
|
</a>
|
|
29
|
-
<a href="https://bundlephobia.com/package/@hyperfrontend/time-utils">
|
|
30
|
-
<img src="https://img.shields.io/bundlephobia/minzip/@hyperfrontend/time-utils?style=flat-square" alt="Bundle Size">
|
|
31
|
-
</a>
|
|
32
29
|
<img src="https://img.shields.io/badge/node-%3E%3D18.0.0-brightgreen?style=flat-square&logo=node.js" alt="Node Version">
|
|
33
30
|
<img src="https://img.shields.io/badge/tree%20shakeable-%E2%9C%93-success?style=flat-square" alt="Tree Shakeable">
|
|
34
31
|
</p>
|
|
@@ -251,7 +248,9 @@ None — zero external dependencies.
|
|
|
251
248
|
|
|
252
249
|
## Part of hyperfrontend
|
|
253
250
|
|
|
254
|
-
This library is part of the [hyperfrontend](https://github.com/AndrewRedican/hyperfrontend) monorepo.
|
|
251
|
+
This library is part of the [hyperfrontend](https://github.com/AndrewRedican/hyperfrontend) monorepo.
|
|
252
|
+
|
|
253
|
+
**📖 [Full documentation](https://www.hyperfrontend.dev/docs/libraries/utils/time)**
|
|
255
254
|
|
|
256
255
|
- Used by [@hyperfrontend/cryptography](https://github.com/AndrewRedican/hyperfrontend/tree/main/libs/cryptography) for time-window password generation
|
|
257
256
|
|
package/bundle/index.iife.js
CHANGED
|
@@ -1,6 +1,92 @@
|
|
|
1
1
|
var HyperfrontendTimeUtils = (function (exports) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Safe copies of Date built-in via factory function and static methods.
|
|
6
|
+
*
|
|
7
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
8
|
+
* provides a factory function that uses Reflect.construct internally.
|
|
9
|
+
*
|
|
10
|
+
* These references are captured at module initialization time to protect against
|
|
11
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
12
|
+
*
|
|
13
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/date
|
|
14
|
+
*/
|
|
15
|
+
// Capture references at module initialization time
|
|
16
|
+
const _Date = globalThis.Date;
|
|
17
|
+
const _Reflect$2 = globalThis.Reflect;
|
|
18
|
+
function createDate(...args) {
|
|
19
|
+
return _Reflect$2.construct(_Date, args);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* (Safe copy) Returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
|
|
23
|
+
*/
|
|
24
|
+
const dateNow = _Date.now;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Safe copies of Object built-in methods.
|
|
28
|
+
*
|
|
29
|
+
* These references are captured at module initialization time to protect against
|
|
30
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
31
|
+
*
|
|
32
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/object
|
|
33
|
+
*/
|
|
34
|
+
// Capture references at module initialization time
|
|
35
|
+
const _Object = globalThis.Object;
|
|
36
|
+
/**
|
|
37
|
+
* (Safe copy) Prevents modification of existing property attributes and values,
|
|
38
|
+
* and prevents the addition of new properties.
|
|
39
|
+
*/
|
|
40
|
+
const freeze = _Object.freeze;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Safe copies of Timer/Scheduling built-in functions.
|
|
44
|
+
*
|
|
45
|
+
* These references are captured at module initialization time to protect against
|
|
46
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
47
|
+
*
|
|
48
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/timers
|
|
49
|
+
*/
|
|
50
|
+
// Capture references at module initialization time
|
|
51
|
+
const _setTimeout = globalThis.setTimeout;
|
|
52
|
+
const _setInterval = globalThis.setInterval;
|
|
53
|
+
const _clearTimeout = globalThis.clearTimeout;
|
|
54
|
+
const _clearInterval = globalThis.clearInterval;
|
|
55
|
+
/**
|
|
56
|
+
* (Safe copy) Sets a timer which executes a function once the timer expires.
|
|
57
|
+
*
|
|
58
|
+
* @param callback - Function to call when the timer elapses.
|
|
59
|
+
* @param delay - Time in milliseconds before executing.
|
|
60
|
+
* @param args - Additional arguments to pass to the callback.
|
|
61
|
+
* @returns A numeric ID for the timer.
|
|
62
|
+
*/
|
|
63
|
+
const setTimeout = (callback, delay, ...args) => _setTimeout(callback, delay, ...args);
|
|
64
|
+
/**
|
|
65
|
+
* (Safe copy) Repeatedly calls a function with a fixed time delay between each call.
|
|
66
|
+
*
|
|
67
|
+
* @param callback - Function to call at each interval.
|
|
68
|
+
* @param delay - Time in milliseconds between calls.
|
|
69
|
+
* @param args - Additional arguments to pass to the callback.
|
|
70
|
+
* @returns A numeric ID for the interval.
|
|
71
|
+
*/
|
|
72
|
+
const setInterval = (callback, delay, ...args) => _setInterval(callback, delay, ...args);
|
|
73
|
+
/**
|
|
74
|
+
* (Safe copy) Cancels a timeout previously established by setTimeout.
|
|
75
|
+
*
|
|
76
|
+
* @param id - The identifier of the timeout to cancel.
|
|
77
|
+
*/
|
|
78
|
+
const clearTimeout = (id) => {
|
|
79
|
+
_clearTimeout(id);
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* (Safe copy) Cancels a timed, repeating action previously established by setInterval.
|
|
83
|
+
*
|
|
84
|
+
* @param id - The identifier of the interval to cancel.
|
|
85
|
+
*/
|
|
86
|
+
const clearInterval = (id) => {
|
|
87
|
+
_clearInterval(id);
|
|
88
|
+
};
|
|
89
|
+
|
|
4
90
|
/**
|
|
5
91
|
* Creates an interval loop that invokes one or more subscribed callback functions
|
|
6
92
|
* at the specified internal (in milliseconds).
|
|
@@ -17,7 +103,7 @@ var HyperfrontendTimeUtils = (function (exports) {
|
|
|
17
103
|
const start = () => {
|
|
18
104
|
if (clockId === null) {
|
|
19
105
|
clockId = setInterval(() => {
|
|
20
|
-
const currentTime =
|
|
106
|
+
const currentTime = createDate();
|
|
21
107
|
subscribers.forEach((subscriber) => subscriber(currentTime));
|
|
22
108
|
}, interval);
|
|
23
109
|
}
|
|
@@ -34,7 +120,7 @@ var HyperfrontendTimeUtils = (function (exports) {
|
|
|
34
120
|
const unsubscribe = (callback) => {
|
|
35
121
|
subscribers = subscribers.filter((subscriber) => subscriber !== callback);
|
|
36
122
|
};
|
|
37
|
-
return
|
|
123
|
+
return freeze({ start, stop, subscribe, unsubscribe, interval });
|
|
38
124
|
}
|
|
39
125
|
|
|
40
126
|
/**
|
|
@@ -52,7 +138,7 @@ var HyperfrontendTimeUtils = (function (exports) {
|
|
|
52
138
|
const pause = () => {
|
|
53
139
|
if (timerId !== null) {
|
|
54
140
|
clearTimeout(timerId);
|
|
55
|
-
const now =
|
|
141
|
+
const now = dateNow();
|
|
56
142
|
/* istanbul ignore else - start is always set when timerId is not null */
|
|
57
143
|
if (start !== null) {
|
|
58
144
|
remaining -= now - start;
|
|
@@ -62,7 +148,7 @@ var HyperfrontendTimeUtils = (function (exports) {
|
|
|
62
148
|
};
|
|
63
149
|
const resume = () => {
|
|
64
150
|
if (timerId === null) {
|
|
65
|
-
start =
|
|
151
|
+
start = dateNow();
|
|
66
152
|
timerId = setTimeout(() => {
|
|
67
153
|
callback();
|
|
68
154
|
timerId = null;
|
|
@@ -74,9 +160,66 @@ var HyperfrontendTimeUtils = (function (exports) {
|
|
|
74
160
|
remaining = newDelay;
|
|
75
161
|
resume();
|
|
76
162
|
};
|
|
77
|
-
return
|
|
163
|
+
return freeze({ pause, resume, reset });
|
|
78
164
|
}
|
|
79
165
|
|
|
166
|
+
/**
|
|
167
|
+
* Safe copies of Error built-ins via factory functions.
|
|
168
|
+
*
|
|
169
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
170
|
+
* provides factory functions that use Reflect.construct internally.
|
|
171
|
+
*
|
|
172
|
+
* These references are captured at module initialization time to protect against
|
|
173
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
174
|
+
*
|
|
175
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/error
|
|
176
|
+
*/
|
|
177
|
+
// Capture references at module initialization time
|
|
178
|
+
const _Error = globalThis.Error;
|
|
179
|
+
const _Reflect$1 = globalThis.Reflect;
|
|
180
|
+
/**
|
|
181
|
+
* (Safe copy) Creates a new Error using the captured Error constructor.
|
|
182
|
+
* Use this instead of `new Error()`.
|
|
183
|
+
*
|
|
184
|
+
* @param message - Optional error message.
|
|
185
|
+
* @param options - Optional error options.
|
|
186
|
+
* @returns A new Error instance.
|
|
187
|
+
*/
|
|
188
|
+
const createError = (message, options) => _Reflect$1.construct(_Error, [message, options]);
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Safe copies of Math built-in methods.
|
|
192
|
+
*
|
|
193
|
+
* These references are captured at module initialization time to protect against
|
|
194
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
195
|
+
*
|
|
196
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/math
|
|
197
|
+
*/
|
|
198
|
+
// Capture references at module initialization time
|
|
199
|
+
const _Math = globalThis.Math;
|
|
200
|
+
/**
|
|
201
|
+
* (Safe copy) Returns the largest integer less than or equal to a number.
|
|
202
|
+
*/
|
|
203
|
+
const floor = _Math.floor;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Safe copies of Number built-in methods and constants.
|
|
207
|
+
*
|
|
208
|
+
* These references are captured at module initialization time to protect against
|
|
209
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
210
|
+
*
|
|
211
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/number
|
|
212
|
+
*/
|
|
213
|
+
// Capture references at module initialization time
|
|
214
|
+
const _isNaN = globalThis.isNaN;
|
|
215
|
+
// ============================================================================
|
|
216
|
+
// Global Type Checking (legacy, less strict)
|
|
217
|
+
// ============================================================================
|
|
218
|
+
/**
|
|
219
|
+
* (Safe copy) Global isNaN function (coerces to number first, less strict than Number.isNaN).
|
|
220
|
+
*/
|
|
221
|
+
const globalIsNaN = _isNaN;
|
|
222
|
+
|
|
80
223
|
/**
|
|
81
224
|
* Normalizes a given time to the nearest base time window.
|
|
82
225
|
*
|
|
@@ -85,16 +228,16 @@ var HyperfrontendTimeUtils = (function (exports) {
|
|
|
85
228
|
* @returns A new Date object normalized to the start of the time window
|
|
86
229
|
*/
|
|
87
230
|
function normalizeToBaseTimeWindow(time, baseTimeWindow) {
|
|
88
|
-
if (!time || !(time instanceof Date) ||
|
|
89
|
-
throw
|
|
231
|
+
if (!time || !(time instanceof Date) || globalIsNaN(time.getTime())) {
|
|
232
|
+
throw createError('Invalid time input');
|
|
90
233
|
}
|
|
91
234
|
if (baseTimeWindow <= 0) {
|
|
92
|
-
throw
|
|
235
|
+
throw createError('Base time window must be positive');
|
|
93
236
|
}
|
|
94
237
|
const timeInMs = time.getTime();
|
|
95
238
|
const windowInMs = baseTimeWindow * 60 * 1000;
|
|
96
|
-
const normalizedTimeInMs =
|
|
97
|
-
return
|
|
239
|
+
const normalizedTimeInMs = floor(timeInMs / windowInMs) * windowInMs;
|
|
240
|
+
return createDate(normalizedTimeInMs);
|
|
98
241
|
}
|
|
99
242
|
|
|
100
243
|
/**
|
|
@@ -109,6 +252,59 @@ var HyperfrontendTimeUtils = (function (exports) {
|
|
|
109
252
|
return () => clearInterval(timerId);
|
|
110
253
|
}
|
|
111
254
|
|
|
255
|
+
/**
|
|
256
|
+
* Safe copies of Promise built-in methods via factory functions.
|
|
257
|
+
*
|
|
258
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
259
|
+
* provides factory functions that use Reflect.construct internally.
|
|
260
|
+
*
|
|
261
|
+
* These references are captured at module initialization time to protect against
|
|
262
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
263
|
+
*
|
|
264
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/promise
|
|
265
|
+
*/
|
|
266
|
+
// Capture references at module initialization time
|
|
267
|
+
const _Promise = globalThis.Promise;
|
|
268
|
+
const _Reflect = globalThis.Reflect;
|
|
269
|
+
/**
|
|
270
|
+
* (Safe copy) Creates a new Promise using the captured Promise constructor.
|
|
271
|
+
* Use this instead of `new Promise()`.
|
|
272
|
+
*
|
|
273
|
+
* @param executor - The executor function.
|
|
274
|
+
* @returns A new Promise instance.
|
|
275
|
+
*/
|
|
276
|
+
const createPromise = (executor) => _Reflect.construct(_Promise, [executor]);
|
|
277
|
+
/**
|
|
278
|
+
* (Safe copy) Returns a Promise that resolves with the given value.
|
|
279
|
+
*/
|
|
280
|
+
_Promise.resolve.bind(_Promise);
|
|
281
|
+
/**
|
|
282
|
+
* (Safe copy) Returns a Promise that rejects with the given reason.
|
|
283
|
+
*/
|
|
284
|
+
_Promise.reject.bind(_Promise);
|
|
285
|
+
/**
|
|
286
|
+
* (Safe copy) Returns a Promise that resolves when all promises resolve.
|
|
287
|
+
*/
|
|
288
|
+
_Promise.all.bind(_Promise);
|
|
289
|
+
/**
|
|
290
|
+
* (Safe copy) Returns a Promise that resolves/rejects with the first settled promise.
|
|
291
|
+
*/
|
|
292
|
+
_Promise.race.bind(_Promise);
|
|
293
|
+
/**
|
|
294
|
+
* (Safe copy) Returns a Promise that resolves when all promises settle.
|
|
295
|
+
*/
|
|
296
|
+
_Promise.allSettled.bind(_Promise);
|
|
297
|
+
/**
|
|
298
|
+
* (Safe copy) Returns a Promise that resolves with the first fulfilled promise.
|
|
299
|
+
*/
|
|
300
|
+
_Promise.any.bind(_Promise);
|
|
301
|
+
/**
|
|
302
|
+
* (Safe copy) Creates a Promise along with its resolve and reject functions.
|
|
303
|
+
* Note: Available only in ES2024+ environments.
|
|
304
|
+
*/
|
|
305
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
306
|
+
_Promise.withResolvers?.bind(_Promise);
|
|
307
|
+
|
|
112
308
|
/**
|
|
113
309
|
* Pauses execution for a specified duration.
|
|
114
310
|
*
|
|
@@ -116,7 +312,7 @@ var HyperfrontendTimeUtils = (function (exports) {
|
|
|
116
312
|
* @returns A promise that resolves after the specified duration
|
|
117
313
|
*/
|
|
118
314
|
function sleep(milliseconds) {
|
|
119
|
-
return
|
|
315
|
+
return createPromise((resolve) => setTimeout(resolve, milliseconds));
|
|
120
316
|
}
|
|
121
317
|
|
|
122
318
|
exports.createClock = createClock;
|
package/bundle/index.iife.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.iife.js","sources":["../../../../../../../../../../libs/utils/time/src/create-clock.ts","../../../../../../../../../../libs/utils/time/src/create-timer.ts","../../../../../../../../../../libs/utils/time/src/normalize-to-base-time-window.ts","../../../../../../../../../../libs/utils/time/src/set-interval-callback.ts","../../../../../../../../../../libs/utils/time/src/sleep.ts"],"sourcesContent":[null,null,null,null,null],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.iife.js","sources":["../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/date/index.ts","../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/object/index.ts","../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/timers/index.ts","../../../../../../../../../../libs/utils/time/src/create-clock.ts","../../../../../../../../../../libs/utils/time/src/create-timer.ts","../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/error/index.ts","../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/math/index.ts","../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/number/index.ts","../../../../../../../../../../libs/utils/time/src/normalize-to-base-time-window.ts","../../../../../../../../../../libs/utils/time/src/set-interval-callback.ts","../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/promise/index.ts","../../../../../../../../../../libs/utils/time/src/sleep.ts"],"sourcesContent":[null,null,null,null,null,null,null,null,null,null,null,null],"names":["_Reflect"],"mappings":";;;IAAA;;;;;;;;;;IAUG;IAEH;IACA,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI;IAC7B,MAAMA,UAAQ,GAAG,UAAU,CAAC,OAAO;IAoB7B,SAAU,UAAU,CAAC,GAAG,IAAe,EAAA;QAC3C,OAAaA,UAAQ,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC;IAC9C;IAEA;;IA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;;"}
|
package/bundle/index.iife.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var HyperfrontendTimeUtils=function(e){"use strict";return e.createClock=function(e=1e3){let
|
|
1
|
+
var HyperfrontendTimeUtils=function(e){"use strict";const l=globalThis.Date,t=globalThis.Reflect;function n(...e){return t.construct(l,e)}const i=l.now,o=globalThis.Object.freeze,s=globalThis.setTimeout,r=globalThis.setInterval,a=globalThis.clearTimeout,c=globalThis.clearInterval,u=(e,l,...t)=>s(e,l,...t),b=(e,l,...t)=>r(e,l,...t),T=e=>{c(e)};const f=globalThis.Error,h=globalThis.Reflect,g=(e,l)=>h.construct(f,[e,l]),m=globalThis.Math.floor,d=globalThis.isNaN;const v=globalThis.Promise,p=globalThis.Reflect;return v.resolve.bind(v),v.reject.bind(v),v.all.bind(v),v.race.bind(v),v.allSettled.bind(v),v.any.bind(v),v.withResolvers?.bind(v),e.createClock=function(e=1e3){let l=null,t=[];return o({start:()=>{null===l&&(l=b(()=>{const e=n();t.forEach(l=>l(e))},e))},stop:()=>{null!==l&&(T(l),l=null)},subscribe:e=>{t.push(e)},unsubscribe:e=>{t=t.filter(l=>l!==e)},interval:e})},e.createTimer=function(e,l){let t=null,n=null,s=l;const r=()=>{if(null!==t){a(t);const e=i();null!==n&&(s-=e-n),t=null}},c=()=>{null===t&&(n=i(),t=u(()=>{e(),t=null},s))};return o({pause:r,resume:c,reset:(e=l)=>{r(),s=e,c()}})},e.normalizeToBaseTimeWindow=function(e,l){if(!e||!(e instanceof Date)||d(e.getTime()))throw g("Invalid time input");if(l<=0)throw g("Base time window must be positive");const t=e.getTime(),i=60*l*1e3;return n(m(t/i)*i)},e.setIntervalCallback=function(e,l){const t=b(e,l);return()=>T(t)},e.sleep=function(e){return l=l=>u(l,e),p.construct(v,[l]);var l},e}({});
|
|
2
2
|
//# sourceMappingURL=index.iife.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.iife.min.js","sources":["../../../../../../../../../../libs/utils/time/src/create-clock.ts","../../../../../../../../../../libs/utils/time/src/create-timer.ts","../../../../../../../../../../libs/utils/time/src/normalize-to-base-time-window.ts","../../../../../../../../../../libs/utils/time/src/set-interval-callback.ts","../../../../../../../../../../libs/utils/time/src/sleep.ts"],"sourcesContent":[null,null,null,null,null],"names":["
|
|
1
|
+
{"version":3,"file":"index.iife.min.js","sources":["../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/date/index.ts","../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/object/index.ts","../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/timers/index.ts","../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/error/index.ts","../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/math/index.ts","../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/number/index.ts","../../../../../../../../../../libs/utils/immutable-api/src/built-in-copy/promise/index.ts","../../../../../../../../../../libs/utils/time/src/create-clock.ts","../../../../../../../../../../libs/utils/time/src/create-timer.ts","../../../../../../../../../../libs/utils/time/src/normalize-to-base-time-window.ts","../../../../../../../../../../libs/utils/time/src/set-interval-callback.ts","../../../../../../../../../../libs/utils/time/src/sleep.ts"],"sourcesContent":[null,null,null,null,null,null,null,null,null,null,null,null],"names":["_Date","globalThis","Date","_Reflect","Reflect","createDate","args","construct","dateNow","now","freeze","Object","_setTimeout","setTimeout","_setInterval","setInterval","_clearTimeout","clearTimeout","_clearInterval","clearInterval","callback","delay","id","_Error","Error","createError","message","options","floor","Math","globalIsNaN","isNaN","_Promise","Promise","resolve","bind","reject","all","race","allSettled","any","withResolvers","interval","clockId","subscribers","start","currentTime","forEach","subscriber","stop","subscribe","push","unsubscribe","filter","timerId","remaining","pause","resume","reset","newDelay","time","baseTimeWindow","getTime","timeInMs","windowInMs","milliseconds","executor"],"mappings":"oDAaA,MAAMA,EAAQC,WAAWC,KACnBC,EAAWF,WAAWG,QAoBtB,SAAUC,KAAcC,GAC5B,OAAaH,EAASI,UA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AExB,MAAMmC,EAAQ,KACZ,GAAgB,OAAZF,EAAkB,CN0BxBtC,EMzBiBsC,GACb,MAAM7C,EAAMD,IAEE,OAAVqC,IACFU,GAAa9C,EAAMoC,GAErBS,EAAU,IACZ,GAGIG,EAAS,KACG,OAAZH,IACFT,EAAQrC,IACR8C,EAAUzC,EAAW,KACnBO,IACAkC,EAAU,MACTC,KAUP,OAAO7C,EAAO,CAAE8C,QAAOC,SAAQC,MANjB,CAACC,EAAmBtC,KAChCmC,IACAD,EAAYI,EACZF,MAIJ,8BC3CM,SAAoCG,EAAYC,GACpD,IAAKD,KAAUA,aAAgB1D,OAAS4B,EAAY8B,EAAKE,WACvD,MAAMrC,EAAY,sBAGpB,GAAIoC,GAAkB,EACpB,MAAMpC,EAAY,qCAGpB,MAAMsC,EAAWH,EAAKE,UAChBE,EAA8B,GAAjBH,EAAsB,IAEzC,OAAOxD,EADoBuB,EAAMmC,EAAWC,GAAcA,EAE5D,wBChBM,SAA8B5C,EAAsBsB,GACxD,MAAMY,EAAUvC,EAAYK,EAAUsB,GACtC,MAAO,IAAYvB,EAAcmC,EACnC,UCHM,SAAgBW,GACpB,OLeAC,EKfsBhC,GAAYrB,EAAWqB,EAAS+B,GLgB3B9D,EAASI,UAAUyB,EAAU,CAACkC,IAF9B,IAC3BA,CKdF"}
|
package/bundle/index.umd.js
CHANGED
|
@@ -4,6 +4,92 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.HyperfrontendTimeUtils = {}));
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Safe copies of Date built-in via factory function and static methods.
|
|
9
|
+
*
|
|
10
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
11
|
+
* provides a factory function that uses Reflect.construct internally.
|
|
12
|
+
*
|
|
13
|
+
* These references are captured at module initialization time to protect against
|
|
14
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
15
|
+
*
|
|
16
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/date
|
|
17
|
+
*/
|
|
18
|
+
// Capture references at module initialization time
|
|
19
|
+
const _Date = globalThis.Date;
|
|
20
|
+
const _Reflect$2 = globalThis.Reflect;
|
|
21
|
+
function createDate(...args) {
|
|
22
|
+
return _Reflect$2.construct(_Date, args);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* (Safe copy) Returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
|
|
26
|
+
*/
|
|
27
|
+
const dateNow = _Date.now;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Safe copies of Object built-in methods.
|
|
31
|
+
*
|
|
32
|
+
* These references are captured at module initialization time to protect against
|
|
33
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
34
|
+
*
|
|
35
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/object
|
|
36
|
+
*/
|
|
37
|
+
// Capture references at module initialization time
|
|
38
|
+
const _Object = globalThis.Object;
|
|
39
|
+
/**
|
|
40
|
+
* (Safe copy) Prevents modification of existing property attributes and values,
|
|
41
|
+
* and prevents the addition of new properties.
|
|
42
|
+
*/
|
|
43
|
+
const freeze = _Object.freeze;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Safe copies of Timer/Scheduling built-in functions.
|
|
47
|
+
*
|
|
48
|
+
* These references are captured at module initialization time to protect against
|
|
49
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
50
|
+
*
|
|
51
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/timers
|
|
52
|
+
*/
|
|
53
|
+
// Capture references at module initialization time
|
|
54
|
+
const _setTimeout = globalThis.setTimeout;
|
|
55
|
+
const _setInterval = globalThis.setInterval;
|
|
56
|
+
const _clearTimeout = globalThis.clearTimeout;
|
|
57
|
+
const _clearInterval = globalThis.clearInterval;
|
|
58
|
+
/**
|
|
59
|
+
* (Safe copy) Sets a timer which executes a function once the timer expires.
|
|
60
|
+
*
|
|
61
|
+
* @param callback - Function to call when the timer elapses.
|
|
62
|
+
* @param delay - Time in milliseconds before executing.
|
|
63
|
+
* @param args - Additional arguments to pass to the callback.
|
|
64
|
+
* @returns A numeric ID for the timer.
|
|
65
|
+
*/
|
|
66
|
+
const setTimeout = (callback, delay, ...args) => _setTimeout(callback, delay, ...args);
|
|
67
|
+
/**
|
|
68
|
+
* (Safe copy) Repeatedly calls a function with a fixed time delay between each call.
|
|
69
|
+
*
|
|
70
|
+
* @param callback - Function to call at each interval.
|
|
71
|
+
* @param delay - Time in milliseconds between calls.
|
|
72
|
+
* @param args - Additional arguments to pass to the callback.
|
|
73
|
+
* @returns A numeric ID for the interval.
|
|
74
|
+
*/
|
|
75
|
+
const setInterval = (callback, delay, ...args) => _setInterval(callback, delay, ...args);
|
|
76
|
+
/**
|
|
77
|
+
* (Safe copy) Cancels a timeout previously established by setTimeout.
|
|
78
|
+
*
|
|
79
|
+
* @param id - The identifier of the timeout to cancel.
|
|
80
|
+
*/
|
|
81
|
+
const clearTimeout = (id) => {
|
|
82
|
+
_clearTimeout(id);
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* (Safe copy) Cancels a timed, repeating action previously established by setInterval.
|
|
86
|
+
*
|
|
87
|
+
* @param id - The identifier of the interval to cancel.
|
|
88
|
+
*/
|
|
89
|
+
const clearInterval = (id) => {
|
|
90
|
+
_clearInterval(id);
|
|
91
|
+
};
|
|
92
|
+
|
|
7
93
|
/**
|
|
8
94
|
* Creates an interval loop that invokes one or more subscribed callback functions
|
|
9
95
|
* at the specified internal (in milliseconds).
|
|
@@ -20,7 +106,7 @@
|
|
|
20
106
|
const start = () => {
|
|
21
107
|
if (clockId === null) {
|
|
22
108
|
clockId = setInterval(() => {
|
|
23
|
-
const currentTime =
|
|
109
|
+
const currentTime = createDate();
|
|
24
110
|
subscribers.forEach((subscriber) => subscriber(currentTime));
|
|
25
111
|
}, interval);
|
|
26
112
|
}
|
|
@@ -37,7 +123,7 @@
|
|
|
37
123
|
const unsubscribe = (callback) => {
|
|
38
124
|
subscribers = subscribers.filter((subscriber) => subscriber !== callback);
|
|
39
125
|
};
|
|
40
|
-
return
|
|
126
|
+
return freeze({ start, stop, subscribe, unsubscribe, interval });
|
|
41
127
|
}
|
|
42
128
|
|
|
43
129
|
/**
|
|
@@ -55,7 +141,7 @@
|
|
|
55
141
|
const pause = () => {
|
|
56
142
|
if (timerId !== null) {
|
|
57
143
|
clearTimeout(timerId);
|
|
58
|
-
const now =
|
|
144
|
+
const now = dateNow();
|
|
59
145
|
/* istanbul ignore else - start is always set when timerId is not null */
|
|
60
146
|
if (start !== null) {
|
|
61
147
|
remaining -= now - start;
|
|
@@ -65,7 +151,7 @@
|
|
|
65
151
|
};
|
|
66
152
|
const resume = () => {
|
|
67
153
|
if (timerId === null) {
|
|
68
|
-
start =
|
|
154
|
+
start = dateNow();
|
|
69
155
|
timerId = setTimeout(() => {
|
|
70
156
|
callback();
|
|
71
157
|
timerId = null;
|
|
@@ -77,9 +163,66 @@
|
|
|
77
163
|
remaining = newDelay;
|
|
78
164
|
resume();
|
|
79
165
|
};
|
|
80
|
-
return
|
|
166
|
+
return freeze({ pause, resume, reset });
|
|
81
167
|
}
|
|
82
168
|
|
|
169
|
+
/**
|
|
170
|
+
* Safe copies of Error built-ins via factory functions.
|
|
171
|
+
*
|
|
172
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
173
|
+
* provides factory functions that use Reflect.construct internally.
|
|
174
|
+
*
|
|
175
|
+
* These references are captured at module initialization time to protect against
|
|
176
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
177
|
+
*
|
|
178
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/error
|
|
179
|
+
*/
|
|
180
|
+
// Capture references at module initialization time
|
|
181
|
+
const _Error = globalThis.Error;
|
|
182
|
+
const _Reflect$1 = globalThis.Reflect;
|
|
183
|
+
/**
|
|
184
|
+
* (Safe copy) Creates a new Error using the captured Error constructor.
|
|
185
|
+
* Use this instead of `new Error()`.
|
|
186
|
+
*
|
|
187
|
+
* @param message - Optional error message.
|
|
188
|
+
* @param options - Optional error options.
|
|
189
|
+
* @returns A new Error instance.
|
|
190
|
+
*/
|
|
191
|
+
const createError = (message, options) => _Reflect$1.construct(_Error, [message, options]);
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Safe copies of Math built-in methods.
|
|
195
|
+
*
|
|
196
|
+
* These references are captured at module initialization time to protect against
|
|
197
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
198
|
+
*
|
|
199
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/math
|
|
200
|
+
*/
|
|
201
|
+
// Capture references at module initialization time
|
|
202
|
+
const _Math = globalThis.Math;
|
|
203
|
+
/**
|
|
204
|
+
* (Safe copy) Returns the largest integer less than or equal to a number.
|
|
205
|
+
*/
|
|
206
|
+
const floor = _Math.floor;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Safe copies of Number built-in methods and constants.
|
|
210
|
+
*
|
|
211
|
+
* These references are captured at module initialization time to protect against
|
|
212
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
213
|
+
*
|
|
214
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/number
|
|
215
|
+
*/
|
|
216
|
+
// Capture references at module initialization time
|
|
217
|
+
const _isNaN = globalThis.isNaN;
|
|
218
|
+
// ============================================================================
|
|
219
|
+
// Global Type Checking (legacy, less strict)
|
|
220
|
+
// ============================================================================
|
|
221
|
+
/**
|
|
222
|
+
* (Safe copy) Global isNaN function (coerces to number first, less strict than Number.isNaN).
|
|
223
|
+
*/
|
|
224
|
+
const globalIsNaN = _isNaN;
|
|
225
|
+
|
|
83
226
|
/**
|
|
84
227
|
* Normalizes a given time to the nearest base time window.
|
|
85
228
|
*
|
|
@@ -88,16 +231,16 @@
|
|
|
88
231
|
* @returns A new Date object normalized to the start of the time window
|
|
89
232
|
*/
|
|
90
233
|
function normalizeToBaseTimeWindow(time, baseTimeWindow) {
|
|
91
|
-
if (!time || !(time instanceof Date) ||
|
|
92
|
-
throw
|
|
234
|
+
if (!time || !(time instanceof Date) || globalIsNaN(time.getTime())) {
|
|
235
|
+
throw createError('Invalid time input');
|
|
93
236
|
}
|
|
94
237
|
if (baseTimeWindow <= 0) {
|
|
95
|
-
throw
|
|
238
|
+
throw createError('Base time window must be positive');
|
|
96
239
|
}
|
|
97
240
|
const timeInMs = time.getTime();
|
|
98
241
|
const windowInMs = baseTimeWindow * 60 * 1000;
|
|
99
|
-
const normalizedTimeInMs =
|
|
100
|
-
return
|
|
242
|
+
const normalizedTimeInMs = floor(timeInMs / windowInMs) * windowInMs;
|
|
243
|
+
return createDate(normalizedTimeInMs);
|
|
101
244
|
}
|
|
102
245
|
|
|
103
246
|
/**
|
|
@@ -112,6 +255,59 @@
|
|
|
112
255
|
return () => clearInterval(timerId);
|
|
113
256
|
}
|
|
114
257
|
|
|
258
|
+
/**
|
|
259
|
+
* Safe copies of Promise built-in methods via factory functions.
|
|
260
|
+
*
|
|
261
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
262
|
+
* provides factory functions that use Reflect.construct internally.
|
|
263
|
+
*
|
|
264
|
+
* These references are captured at module initialization time to protect against
|
|
265
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
266
|
+
*
|
|
267
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/promise
|
|
268
|
+
*/
|
|
269
|
+
// Capture references at module initialization time
|
|
270
|
+
const _Promise = globalThis.Promise;
|
|
271
|
+
const _Reflect = globalThis.Reflect;
|
|
272
|
+
/**
|
|
273
|
+
* (Safe copy) Creates a new Promise using the captured Promise constructor.
|
|
274
|
+
* Use this instead of `new Promise()`.
|
|
275
|
+
*
|
|
276
|
+
* @param executor - The executor function.
|
|
277
|
+
* @returns A new Promise instance.
|
|
278
|
+
*/
|
|
279
|
+
const createPromise = (executor) => _Reflect.construct(_Promise, [executor]);
|
|
280
|
+
/**
|
|
281
|
+
* (Safe copy) Returns a Promise that resolves with the given value.
|
|
282
|
+
*/
|
|
283
|
+
_Promise.resolve.bind(_Promise);
|
|
284
|
+
/**
|
|
285
|
+
* (Safe copy) Returns a Promise that rejects with the given reason.
|
|
286
|
+
*/
|
|
287
|
+
_Promise.reject.bind(_Promise);
|
|
288
|
+
/**
|
|
289
|
+
* (Safe copy) Returns a Promise that resolves when all promises resolve.
|
|
290
|
+
*/
|
|
291
|
+
_Promise.all.bind(_Promise);
|
|
292
|
+
/**
|
|
293
|
+
* (Safe copy) Returns a Promise that resolves/rejects with the first settled promise.
|
|
294
|
+
*/
|
|
295
|
+
_Promise.race.bind(_Promise);
|
|
296
|
+
/**
|
|
297
|
+
* (Safe copy) Returns a Promise that resolves when all promises settle.
|
|
298
|
+
*/
|
|
299
|
+
_Promise.allSettled.bind(_Promise);
|
|
300
|
+
/**
|
|
301
|
+
* (Safe copy) Returns a Promise that resolves with the first fulfilled promise.
|
|
302
|
+
*/
|
|
303
|
+
_Promise.any.bind(_Promise);
|
|
304
|
+
/**
|
|
305
|
+
* (Safe copy) Creates a Promise along with its resolve and reject functions.
|
|
306
|
+
* Note: Available only in ES2024+ environments.
|
|
307
|
+
*/
|
|
308
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
309
|
+
_Promise.withResolvers?.bind(_Promise);
|
|
310
|
+
|
|
115
311
|
/**
|
|
116
312
|
* Pauses execution for a specified duration.
|
|
117
313
|
*
|
|
@@ -119,7 +315,7 @@
|
|
|
119
315
|
* @returns A promise that resolves after the specified duration
|
|
120
316
|
*/
|
|
121
317
|
function sleep(milliseconds) {
|
|
122
|
-
return
|
|
318
|
+
return createPromise((resolve) => setTimeout(resolve, milliseconds));
|
|
123
319
|
}
|
|
124
320
|
|
|
125
321
|
exports.createClock = createClock;
|