@flightlesslabs/time-utils 0.1.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/CHANGELOG.md +4 -0
- package/LICENSE +21 -0
- package/README.md +54 -0
- package/esm/date-creator/createDate/createDate.d.ts +19 -0
- package/esm/date-creator/createDate/createDate.d.ts.map +1 -0
- package/esm/date-creator/createDate/createDate.js +17 -0
- package/esm/date-creator/createDateFactory/createDateFactory.d.ts +39 -0
- package/esm/date-creator/createDateFactory/createDateFactory.d.ts.map +1 -0
- package/esm/date-creator/createDateFactory/createDateFactory.js +80 -0
- package/esm/date-creator/createDateFactory/dayjs.d.ts +4 -0
- package/esm/date-creator/createDateFactory/dayjs.d.ts.map +1 -0
- package/esm/date-creator/createDateFactory/dayjs.js +33 -0
- package/esm/date-creator/createDateFactory/types.d.ts +39 -0
- package/esm/date-creator/createDateFactory/types.d.ts.map +1 -0
- package/esm/date-creator/createDateFactory/types.js +1 -0
- package/esm/mod.d.ts +5 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +3 -0
- package/esm/package.json +3 -0
- package/esm/timeout/timeout.d.ts +22 -0
- package/esm/timeout/timeout.d.ts.map +1 -0
- package/esm/timeout/timeout.js +28 -0
- package/package.json +29 -0
- package/script/date-creator/createDate/createDate.d.ts +19 -0
- package/script/date-creator/createDate/createDate.d.ts.map +1 -0
- package/script/date-creator/createDate/createDate.js +10 -0
- package/script/date-creator/createDateFactory/createDateFactory.d.ts +39 -0
- package/script/date-creator/createDateFactory/createDateFactory.d.ts.map +1 -0
- package/script/date-creator/createDateFactory/createDateFactory.js +86 -0
- package/script/date-creator/createDateFactory/dayjs.d.ts +4 -0
- package/script/date-creator/createDateFactory/dayjs.d.ts.map +1 -0
- package/script/date-creator/createDateFactory/dayjs.js +38 -0
- package/script/date-creator/createDateFactory/types.d.ts +39 -0
- package/script/date-creator/createDateFactory/types.d.ts.map +1 -0
- package/script/date-creator/createDateFactory/types.js +2 -0
- package/script/mod.d.ts +5 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +9 -0
- package/script/package.json +3 -0
- package/script/timeout/timeout.d.ts +22 -0
- package/script/timeout/timeout.d.ts.map +1 -0
- package/script/timeout/timeout.js +31 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Siddhesh Mangela
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# @flightlesslabs/time-utils
|
|
2
|
+
|
|
3
|
+
Date and time utilities at your disposable
|
|
4
|
+
|
|
5
|
+
## 🔗 Links
|
|
6
|
+
|
|
7
|
+
- 📚 Docs: https://flightlesslabs.github.io/time-utils
|
|
8
|
+
- 💻 GitHub: https://github.com/flightlesslabs/time-utils
|
|
9
|
+
- 📦 npm: https://www.npmjs.com/package/@flightlesslabs/time-utils
|
|
10
|
+
- 🧩 JSR: https://jsr.io/@flightlesslabs/time-utils
|
|
11
|
+
|
|
12
|
+
## 🚀 Installation
|
|
13
|
+
|
|
14
|
+
JSR
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
deno add jsr:@flightlesslabs/time-utils
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
pnpm
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm add @flightlesslabs/time-utils
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
npm
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm i @flightlesslabs/time-utils
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Docs
|
|
35
|
+
|
|
36
|
+
Build docs
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
deno task docs
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Preview docs
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
deno task preview
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Develop
|
|
49
|
+
|
|
50
|
+
Publish to npm
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
deno task publish
|
|
54
|
+
```
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
declare const createDate: {
|
|
2
|
+
(): ReturnType<typeof import("dayjs")>;
|
|
3
|
+
(date: import("../createDateFactory/types.js").CreateDateInput, format?: import("../createDateFactory/types.js").CreateDateFormat, options?: import("../createDateFactory/types.js").CreateDateOptions): ReturnType<typeof import("dayjs")>;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Default preconfigured `createDate` function using a shared Dayjs instance.
|
|
7
|
+
*
|
|
8
|
+
* Provides a ready-to-use API with UTC, timezone, and custom parsing support
|
|
9
|
+
* without requiring users to configure Dayjs manually.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* createDate();
|
|
14
|
+
* createDate('2024-01-01');
|
|
15
|
+
* createDate('2024-01-01', undefined, { utc: true });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export { createDate };
|
|
19
|
+
//# sourceMappingURL=createDate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createDate.d.ts","sourceRoot":"","sources":["../../../src/date-creator/createDate/createDate.ts"],"names":[],"mappings":"AAGA,QAAA,MAAQ,UAAU;;;CAAoC,CAAC;AAEvD;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createDateFactory } from '../createDateFactory/createDateFactory.js';
|
|
2
|
+
import defaultDayjs from '../createDateFactory/dayjs.js';
|
|
3
|
+
const { createDate } = createDateFactory(defaultDayjs);
|
|
4
|
+
/**
|
|
5
|
+
* Default preconfigured `createDate` function using a shared Dayjs instance.
|
|
6
|
+
*
|
|
7
|
+
* Provides a ready-to-use API with UTC, timezone, and custom parsing support
|
|
8
|
+
* without requiring users to configure Dayjs manually.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* createDate();
|
|
13
|
+
* createDate('2024-01-01');
|
|
14
|
+
* createDate('2024-01-01', undefined, { utc: true });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export { createDate };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import defaultDayjs from './dayjs.js';
|
|
2
|
+
import type { CreateDateFormat, CreateDateInput, CreateDateOptions } from './types.js';
|
|
3
|
+
type Dayjs = typeof defaultDayjs;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a date factory bound to a specific Dayjs instance.
|
|
6
|
+
*
|
|
7
|
+
* This factory wraps a configured Dayjs instance and provides a unified
|
|
8
|
+
* `createDate` function that supports:
|
|
9
|
+
*
|
|
10
|
+
* - UTC parsing (highest priority)
|
|
11
|
+
* - Timezone-aware parsing
|
|
12
|
+
* - Local parsing
|
|
13
|
+
* - Optional strict parsing
|
|
14
|
+
* - Optional invalid-date throwing
|
|
15
|
+
*
|
|
16
|
+
* @param customDayjs - Optional custom Dayjs instance (useful for testing or plugin overrides)
|
|
17
|
+
*
|
|
18
|
+
* @returns An object containing:
|
|
19
|
+
* - `createDate`: Unified date creation function
|
|
20
|
+
* - `dayjs`: The underlying Dayjs instance used by the factory
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const { createDate } = createDateFactory();
|
|
25
|
+
*
|
|
26
|
+
* const date = createDate('2024-01-01');
|
|
27
|
+
* const utcDate = createDate('2024-01-01', undefined, { utc: true });
|
|
28
|
+
* const tzDate = createDate('2024-01-01', undefined, { timezone: 'Asia/Kolkata' });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function createDateFactory(customDayjs?: Dayjs): {
|
|
32
|
+
createDate: {
|
|
33
|
+
(): ReturnType<Dayjs>;
|
|
34
|
+
(date: CreateDateInput, format?: CreateDateFormat, options?: CreateDateOptions): ReturnType<Dayjs>;
|
|
35
|
+
};
|
|
36
|
+
dayjs: typeof import("dayjs");
|
|
37
|
+
};
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=createDateFactory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createDateFactory.d.ts","sourceRoot":"","sources":["../../../src/date-creator/createDateFactory/createDateFactory.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,YAAY,CAAC;AACtC,OAAO,KAAK,EACV,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAEpB,KAAK,KAAK,GAAG,OAAO,YAAY,CAAC;AAEjC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,CAAC,EAAE,KAAK;;YAG5B,UAAU,CAAC,KAAK,CAAC;eAEhC,eAAe,WACZ,gBAAgB,YACf,iBAAiB,GAC1B,UAAU,CAAC,KAAK,CAAC;;;EA6DrB"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import defaultDayjs from './dayjs.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a date factory bound to a specific Dayjs instance.
|
|
4
|
+
*
|
|
5
|
+
* This factory wraps a configured Dayjs instance and provides a unified
|
|
6
|
+
* `createDate` function that supports:
|
|
7
|
+
*
|
|
8
|
+
* - UTC parsing (highest priority)
|
|
9
|
+
* - Timezone-aware parsing
|
|
10
|
+
* - Local parsing
|
|
11
|
+
* - Optional strict parsing
|
|
12
|
+
* - Optional invalid-date throwing
|
|
13
|
+
*
|
|
14
|
+
* @param customDayjs - Optional custom Dayjs instance (useful for testing or plugin overrides)
|
|
15
|
+
*
|
|
16
|
+
* @returns An object containing:
|
|
17
|
+
* - `createDate`: Unified date creation function
|
|
18
|
+
* - `dayjs`: The underlying Dayjs instance used by the factory
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const { createDate } = createDateFactory();
|
|
23
|
+
*
|
|
24
|
+
* const date = createDate('2024-01-01');
|
|
25
|
+
* const utcDate = createDate('2024-01-01', undefined, { utc: true });
|
|
26
|
+
* const tzDate = createDate('2024-01-01', undefined, { timezone: 'Asia/Kolkata' });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function createDateFactory(customDayjs) {
|
|
30
|
+
const d = customDayjs ?? defaultDayjs;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a Dayjs date instance with optional parsing rules.
|
|
33
|
+
*
|
|
34
|
+
* Overloads:
|
|
35
|
+
* - `createDate()` → current date/time
|
|
36
|
+
* - `createDate(input, format?, options?)` → parsed date
|
|
37
|
+
*
|
|
38
|
+
* @param date - Date input (string, number, Date, Dayjs, etc.)
|
|
39
|
+
* @param format - Optional format string for parsing
|
|
40
|
+
* @param options - Parsing and behavior options
|
|
41
|
+
*
|
|
42
|
+
* @returns A Dayjs instance
|
|
43
|
+
*
|
|
44
|
+
* @throws Error if `throwOnInvalid` is true and the date is invalid
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* createDate(); // now
|
|
49
|
+
* createDate('2024-01-01');
|
|
50
|
+
* createDate('01-01-2024', 'DD-MM-YYYY');
|
|
51
|
+
*
|
|
52
|
+
* createDate('2024-01-01', undefined, { utc: true });
|
|
53
|
+
* createDate('2024-01-01', undefined, { timezone: 'UTC' });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
function createDate(date, format, options = {}) {
|
|
57
|
+
const { timezone, utc, strict = false, throwOnInvalid = false } = options;
|
|
58
|
+
const input = date ?? undefined;
|
|
59
|
+
let instance;
|
|
60
|
+
// 1. UTC takes highest priority
|
|
61
|
+
if (utc) {
|
|
62
|
+
instance = format ? d.utc(input, format, strict) : d.utc(input);
|
|
63
|
+
} // 2. Timezone
|
|
64
|
+
else if (timezone) {
|
|
65
|
+
instance = format ? d.tz(input, format, timezone) : d.tz(input, timezone);
|
|
66
|
+
} // 3. Local
|
|
67
|
+
else {
|
|
68
|
+
instance = format ? d(input, format, strict) : d(input);
|
|
69
|
+
}
|
|
70
|
+
// 4. Validation
|
|
71
|
+
if (throwOnInvalid && !instance.isValid()) {
|
|
72
|
+
throw new Error('Invalid date input');
|
|
73
|
+
}
|
|
74
|
+
return instance;
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
createDate,
|
|
78
|
+
dayjs: d,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dayjs.d.ts","sourceRoot":"","sources":["../../../src/date-creator/createDateFactory/dayjs.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,OAAO,CAAC;AAMjC,QAAA,MAAM,KAAK,EAAmB,OAAO,YAAY,CAAC;AA8BlD,eAAe,KAAK,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import dayjsFactory from 'dayjs';
|
|
2
|
+
import utcPlugin from 'dayjs/plugin/utc.js';
|
|
3
|
+
import timezonePlugin from 'dayjs/plugin/timezone.js';
|
|
4
|
+
import customParseFormatPlugin from 'dayjs/plugin/customParseFormat.js';
|
|
5
|
+
import isBetweenPlugin from 'dayjs/plugin/isBetween.js';
|
|
6
|
+
const dayjs = dayjsFactory;
|
|
7
|
+
// default plugin setup
|
|
8
|
+
/**
|
|
9
|
+
* Preconfigured Dayjs instance with commonly used plugins enabled.
|
|
10
|
+
*
|
|
11
|
+
* Enabled plugins:
|
|
12
|
+
* - UTC support
|
|
13
|
+
* - Timezone support
|
|
14
|
+
* - Custom parsing formats
|
|
15
|
+
* - Range checking (`isBetween`)
|
|
16
|
+
*
|
|
17
|
+
* This instance is intended to be shared across the application to ensure
|
|
18
|
+
* consistent date handling behavior.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import dayjs from './dayjs';
|
|
23
|
+
*
|
|
24
|
+
* dayjs().utc();
|
|
25
|
+
* dayjs().tz('Asia/Kolkata');
|
|
26
|
+
* dayjs('2024-01-01', 'YYYY-MM-DD', true);
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
dayjs.extend(utcPlugin);
|
|
30
|
+
dayjs.extend(timezonePlugin);
|
|
31
|
+
dayjs.extend(customParseFormatPlugin);
|
|
32
|
+
dayjs.extend(isBetweenPlugin);
|
|
33
|
+
export default dayjs;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ConfigType } from 'dayjs';
|
|
2
|
+
/**
|
|
3
|
+
* Supported input types for creating a date.
|
|
4
|
+
*
|
|
5
|
+
* Includes all native Dayjs-compatible input types such as:
|
|
6
|
+
* string, number, Date, Dayjs instance, etc.
|
|
7
|
+
*/
|
|
8
|
+
export type CreateDateInput = ConfigType;
|
|
9
|
+
/**
|
|
10
|
+
* Optional format string used when parsing a date.
|
|
11
|
+
*
|
|
12
|
+
* If provided, Dayjs will parse the input using this format.
|
|
13
|
+
*/
|
|
14
|
+
export type CreateDateFormat = string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Options controlling how a date is parsed and interpreted.
|
|
17
|
+
*/
|
|
18
|
+
export interface CreateDateOptions {
|
|
19
|
+
/**
|
|
20
|
+
* Timezone identifier (e.g., "Asia/Kolkata").
|
|
21
|
+
* Requires Dayjs timezone plugin.
|
|
22
|
+
*/
|
|
23
|
+
timezone?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Forces UTC parsing.
|
|
26
|
+
* Takes priority over `timezone`.
|
|
27
|
+
*/
|
|
28
|
+
utc?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Enables strict parsing mode.
|
|
31
|
+
* When true, input must exactly match the format.
|
|
32
|
+
*/
|
|
33
|
+
strict?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* If true, throws an error when the parsed date is invalid.
|
|
36
|
+
*/
|
|
37
|
+
throwOnInvalid?: boolean;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/date-creator/createDateFactory/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAExC;;;;;GAKG;AACH,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC;AAEzC;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,SAAS,CAAC;AAElD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,GAAG,CAAC,EAAE,OAAO,CAAC;IAEd;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/mod.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { timeout } from './timeout/timeout.js';
|
|
2
|
+
export { createDate } from './date-creator/createDate/createDate.js';
|
|
3
|
+
export { createDateFactory } from './date-creator/createDateFactory/createDateFactory.js';
|
|
4
|
+
export type { CreateDateFormat, CreateDateInput, CreateDateOptions, } from './date-creator/createDateFactory/types.js';
|
|
5
|
+
//# sourceMappingURL=mod.d.ts.map
|
package/esm/mod.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,OAAO,EAAE,UAAU,EAAE,MAAM,yCAAyC,CAAC;AAErE,OAAO,EAAE,iBAAiB,EAAE,MAAM,uDAAuD,CAAC;AAC1F,YAAY,EACV,gBAAgB,EAChB,eAAe,EACf,iBAAiB,GAClB,MAAM,2CAA2C,CAAC"}
|
package/esm/mod.js
ADDED
package/esm/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a promise that resolves after a specified delay.
|
|
3
|
+
*
|
|
4
|
+
* Useful for pausing execution in async workflows,
|
|
5
|
+
* throttling operations, or simulating delays in tests.
|
|
6
|
+
*
|
|
7
|
+
* @group Time
|
|
8
|
+
*
|
|
9
|
+
* @param milliseconds - Time to wait in milliseconds.
|
|
10
|
+
* @param value - Optional value to resolve with.
|
|
11
|
+
*
|
|
12
|
+
* @returns A promise that resolves after the delay.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* await timeout(1000);
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const result = await timeout(1000, "done");
|
|
19
|
+
* console.log(result); // "done"
|
|
20
|
+
*/
|
|
21
|
+
export declare function timeout<T = void>(milliseconds: number, value?: T): Promise<T>;
|
|
22
|
+
//# sourceMappingURL=timeout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../../src/timeout/timeout.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,OAAO,CAAC,CAAC,GAAG,IAAI,EAAE,YAAY,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAQ7E"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a promise that resolves after a specified delay.
|
|
3
|
+
*
|
|
4
|
+
* Useful for pausing execution in async workflows,
|
|
5
|
+
* throttling operations, or simulating delays in tests.
|
|
6
|
+
*
|
|
7
|
+
* @group Time
|
|
8
|
+
*
|
|
9
|
+
* @param milliseconds - Time to wait in milliseconds.
|
|
10
|
+
* @param value - Optional value to resolve with.
|
|
11
|
+
*
|
|
12
|
+
* @returns A promise that resolves after the delay.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* await timeout(1000);
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const result = await timeout(1000, "done");
|
|
19
|
+
* console.log(result); // "done"
|
|
20
|
+
*/
|
|
21
|
+
export function timeout(milliseconds, value) {
|
|
22
|
+
if (milliseconds < 0) {
|
|
23
|
+
return Promise.reject(new Error('Timeout duration must be >= 0'));
|
|
24
|
+
}
|
|
25
|
+
return new Promise((resolve) => {
|
|
26
|
+
setTimeout(() => resolve(value), milliseconds);
|
|
27
|
+
});
|
|
28
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flightlesslabs/time-utils",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Date and time utilities at your disposable",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/flightlesslabs/time-utils.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"main": "./script/mod.js",
|
|
11
|
+
"module": "./esm/mod.js",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./esm/mod.js",
|
|
15
|
+
"require": "./script/mod.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"scripts": {},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"dayjs": "^1.11.20"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^20.9.0"
|
|
27
|
+
},
|
|
28
|
+
"_generatedBy": "dnt@dev"
|
|
29
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
declare const createDate: {
|
|
2
|
+
(): ReturnType<typeof import("dayjs")>;
|
|
3
|
+
(date: import("../createDateFactory/types.js").CreateDateInput, format?: import("../createDateFactory/types.js").CreateDateFormat, options?: import("../createDateFactory/types.js").CreateDateOptions): ReturnType<typeof import("dayjs")>;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Default preconfigured `createDate` function using a shared Dayjs instance.
|
|
7
|
+
*
|
|
8
|
+
* Provides a ready-to-use API with UTC, timezone, and custom parsing support
|
|
9
|
+
* without requiring users to configure Dayjs manually.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* createDate();
|
|
14
|
+
* createDate('2024-01-01');
|
|
15
|
+
* createDate('2024-01-01', undefined, { utc: true });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export { createDate };
|
|
19
|
+
//# sourceMappingURL=createDate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createDate.d.ts","sourceRoot":"","sources":["../../../src/date-creator/createDate/createDate.ts"],"names":[],"mappings":"AAGA,QAAA,MAAQ,UAAU;;;CAAoC,CAAC;AAEvD;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createDate = void 0;
|
|
7
|
+
const createDateFactory_js_1 = require("../createDateFactory/createDateFactory.js");
|
|
8
|
+
const dayjs_js_1 = __importDefault(require("../createDateFactory/dayjs.js"));
|
|
9
|
+
const { createDate } = (0, createDateFactory_js_1.createDateFactory)(dayjs_js_1.default);
|
|
10
|
+
exports.createDate = createDate;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import defaultDayjs from './dayjs.js';
|
|
2
|
+
import type { CreateDateFormat, CreateDateInput, CreateDateOptions } from './types.js';
|
|
3
|
+
type Dayjs = typeof defaultDayjs;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a date factory bound to a specific Dayjs instance.
|
|
6
|
+
*
|
|
7
|
+
* This factory wraps a configured Dayjs instance and provides a unified
|
|
8
|
+
* `createDate` function that supports:
|
|
9
|
+
*
|
|
10
|
+
* - UTC parsing (highest priority)
|
|
11
|
+
* - Timezone-aware parsing
|
|
12
|
+
* - Local parsing
|
|
13
|
+
* - Optional strict parsing
|
|
14
|
+
* - Optional invalid-date throwing
|
|
15
|
+
*
|
|
16
|
+
* @param customDayjs - Optional custom Dayjs instance (useful for testing or plugin overrides)
|
|
17
|
+
*
|
|
18
|
+
* @returns An object containing:
|
|
19
|
+
* - `createDate`: Unified date creation function
|
|
20
|
+
* - `dayjs`: The underlying Dayjs instance used by the factory
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const { createDate } = createDateFactory();
|
|
25
|
+
*
|
|
26
|
+
* const date = createDate('2024-01-01');
|
|
27
|
+
* const utcDate = createDate('2024-01-01', undefined, { utc: true });
|
|
28
|
+
* const tzDate = createDate('2024-01-01', undefined, { timezone: 'Asia/Kolkata' });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function createDateFactory(customDayjs?: Dayjs): {
|
|
32
|
+
createDate: {
|
|
33
|
+
(): ReturnType<Dayjs>;
|
|
34
|
+
(date: CreateDateInput, format?: CreateDateFormat, options?: CreateDateOptions): ReturnType<Dayjs>;
|
|
35
|
+
};
|
|
36
|
+
dayjs: typeof import("dayjs");
|
|
37
|
+
};
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=createDateFactory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createDateFactory.d.ts","sourceRoot":"","sources":["../../../src/date-creator/createDateFactory/createDateFactory.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,YAAY,CAAC;AACtC,OAAO,KAAK,EACV,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAEpB,KAAK,KAAK,GAAG,OAAO,YAAY,CAAC;AAEjC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,CAAC,EAAE,KAAK;;YAG5B,UAAU,CAAC,KAAK,CAAC;eAEhC,eAAe,WACZ,gBAAgB,YACf,iBAAiB,GAC1B,UAAU,CAAC,KAAK,CAAC;;;EA6DrB"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createDateFactory = createDateFactory;
|
|
7
|
+
const dayjs_js_1 = __importDefault(require("./dayjs.js"));
|
|
8
|
+
/**
|
|
9
|
+
* Creates a date factory bound to a specific Dayjs instance.
|
|
10
|
+
*
|
|
11
|
+
* This factory wraps a configured Dayjs instance and provides a unified
|
|
12
|
+
* `createDate` function that supports:
|
|
13
|
+
*
|
|
14
|
+
* - UTC parsing (highest priority)
|
|
15
|
+
* - Timezone-aware parsing
|
|
16
|
+
* - Local parsing
|
|
17
|
+
* - Optional strict parsing
|
|
18
|
+
* - Optional invalid-date throwing
|
|
19
|
+
*
|
|
20
|
+
* @param customDayjs - Optional custom Dayjs instance (useful for testing or plugin overrides)
|
|
21
|
+
*
|
|
22
|
+
* @returns An object containing:
|
|
23
|
+
* - `createDate`: Unified date creation function
|
|
24
|
+
* - `dayjs`: The underlying Dayjs instance used by the factory
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const { createDate } = createDateFactory();
|
|
29
|
+
*
|
|
30
|
+
* const date = createDate('2024-01-01');
|
|
31
|
+
* const utcDate = createDate('2024-01-01', undefined, { utc: true });
|
|
32
|
+
* const tzDate = createDate('2024-01-01', undefined, { timezone: 'Asia/Kolkata' });
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
function createDateFactory(customDayjs) {
|
|
36
|
+
const d = customDayjs ?? dayjs_js_1.default;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a Dayjs date instance with optional parsing rules.
|
|
39
|
+
*
|
|
40
|
+
* Overloads:
|
|
41
|
+
* - `createDate()` → current date/time
|
|
42
|
+
* - `createDate(input, format?, options?)` → parsed date
|
|
43
|
+
*
|
|
44
|
+
* @param date - Date input (string, number, Date, Dayjs, etc.)
|
|
45
|
+
* @param format - Optional format string for parsing
|
|
46
|
+
* @param options - Parsing and behavior options
|
|
47
|
+
*
|
|
48
|
+
* @returns A Dayjs instance
|
|
49
|
+
*
|
|
50
|
+
* @throws Error if `throwOnInvalid` is true and the date is invalid
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* createDate(); // now
|
|
55
|
+
* createDate('2024-01-01');
|
|
56
|
+
* createDate('01-01-2024', 'DD-MM-YYYY');
|
|
57
|
+
*
|
|
58
|
+
* createDate('2024-01-01', undefined, { utc: true });
|
|
59
|
+
* createDate('2024-01-01', undefined, { timezone: 'UTC' });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
function createDate(date, format, options = {}) {
|
|
63
|
+
const { timezone, utc, strict = false, throwOnInvalid = false } = options;
|
|
64
|
+
const input = date ?? undefined;
|
|
65
|
+
let instance;
|
|
66
|
+
// 1. UTC takes highest priority
|
|
67
|
+
if (utc) {
|
|
68
|
+
instance = format ? d.utc(input, format, strict) : d.utc(input);
|
|
69
|
+
} // 2. Timezone
|
|
70
|
+
else if (timezone) {
|
|
71
|
+
instance = format ? d.tz(input, format, timezone) : d.tz(input, timezone);
|
|
72
|
+
} // 3. Local
|
|
73
|
+
else {
|
|
74
|
+
instance = format ? d(input, format, strict) : d(input);
|
|
75
|
+
}
|
|
76
|
+
// 4. Validation
|
|
77
|
+
if (throwOnInvalid && !instance.isValid()) {
|
|
78
|
+
throw new Error('Invalid date input');
|
|
79
|
+
}
|
|
80
|
+
return instance;
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
createDate,
|
|
84
|
+
dayjs: d,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dayjs.d.ts","sourceRoot":"","sources":["../../../src/date-creator/createDateFactory/dayjs.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,OAAO,CAAC;AAMjC,QAAA,MAAM,KAAK,EAAmB,OAAO,YAAY,CAAC;AA8BlD,eAAe,KAAK,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const dayjs_1 = __importDefault(require("dayjs"));
|
|
7
|
+
const utc_js_1 = __importDefault(require("dayjs/plugin/utc.js"));
|
|
8
|
+
const timezone_js_1 = __importDefault(require("dayjs/plugin/timezone.js"));
|
|
9
|
+
const customParseFormat_js_1 = __importDefault(require("dayjs/plugin/customParseFormat.js"));
|
|
10
|
+
const isBetween_js_1 = __importDefault(require("dayjs/plugin/isBetween.js"));
|
|
11
|
+
const dayjs = dayjs_1.default;
|
|
12
|
+
// default plugin setup
|
|
13
|
+
/**
|
|
14
|
+
* Preconfigured Dayjs instance with commonly used plugins enabled.
|
|
15
|
+
*
|
|
16
|
+
* Enabled plugins:
|
|
17
|
+
* - UTC support
|
|
18
|
+
* - Timezone support
|
|
19
|
+
* - Custom parsing formats
|
|
20
|
+
* - Range checking (`isBetween`)
|
|
21
|
+
*
|
|
22
|
+
* This instance is intended to be shared across the application to ensure
|
|
23
|
+
* consistent date handling behavior.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import dayjs from './dayjs';
|
|
28
|
+
*
|
|
29
|
+
* dayjs().utc();
|
|
30
|
+
* dayjs().tz('Asia/Kolkata');
|
|
31
|
+
* dayjs('2024-01-01', 'YYYY-MM-DD', true);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
dayjs.extend(utc_js_1.default);
|
|
35
|
+
dayjs.extend(timezone_js_1.default);
|
|
36
|
+
dayjs.extend(customParseFormat_js_1.default);
|
|
37
|
+
dayjs.extend(isBetween_js_1.default);
|
|
38
|
+
exports.default = dayjs;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ConfigType } from 'dayjs';
|
|
2
|
+
/**
|
|
3
|
+
* Supported input types for creating a date.
|
|
4
|
+
*
|
|
5
|
+
* Includes all native Dayjs-compatible input types such as:
|
|
6
|
+
* string, number, Date, Dayjs instance, etc.
|
|
7
|
+
*/
|
|
8
|
+
export type CreateDateInput = ConfigType;
|
|
9
|
+
/**
|
|
10
|
+
* Optional format string used when parsing a date.
|
|
11
|
+
*
|
|
12
|
+
* If provided, Dayjs will parse the input using this format.
|
|
13
|
+
*/
|
|
14
|
+
export type CreateDateFormat = string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Options controlling how a date is parsed and interpreted.
|
|
17
|
+
*/
|
|
18
|
+
export interface CreateDateOptions {
|
|
19
|
+
/**
|
|
20
|
+
* Timezone identifier (e.g., "Asia/Kolkata").
|
|
21
|
+
* Requires Dayjs timezone plugin.
|
|
22
|
+
*/
|
|
23
|
+
timezone?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Forces UTC parsing.
|
|
26
|
+
* Takes priority over `timezone`.
|
|
27
|
+
*/
|
|
28
|
+
utc?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Enables strict parsing mode.
|
|
31
|
+
* When true, input must exactly match the format.
|
|
32
|
+
*/
|
|
33
|
+
strict?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* If true, throws an error when the parsed date is invalid.
|
|
36
|
+
*/
|
|
37
|
+
throwOnInvalid?: boolean;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/date-creator/createDateFactory/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAExC;;;;;GAKG;AACH,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC;AAEzC;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,SAAS,CAAC;AAElD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,GAAG,CAAC,EAAE,OAAO,CAAC;IAEd;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B"}
|
package/script/mod.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { timeout } from './timeout/timeout.js';
|
|
2
|
+
export { createDate } from './date-creator/createDate/createDate.js';
|
|
3
|
+
export { createDateFactory } from './date-creator/createDateFactory/createDateFactory.js';
|
|
4
|
+
export type { CreateDateFormat, CreateDateInput, CreateDateOptions, } from './date-creator/createDateFactory/types.js';
|
|
5
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,OAAO,EAAE,UAAU,EAAE,MAAM,yCAAyC,CAAC;AAErE,OAAO,EAAE,iBAAiB,EAAE,MAAM,uDAAuD,CAAC;AAC1F,YAAY,EACV,gBAAgB,EAChB,eAAe,EACf,iBAAiB,GAClB,MAAM,2CAA2C,CAAC"}
|
package/script/mod.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDateFactory = exports.createDate = exports.timeout = void 0;
|
|
4
|
+
var timeout_js_1 = require("./timeout/timeout.js");
|
|
5
|
+
Object.defineProperty(exports, "timeout", { enumerable: true, get: function () { return timeout_js_1.timeout; } });
|
|
6
|
+
var createDate_js_1 = require("./date-creator/createDate/createDate.js");
|
|
7
|
+
Object.defineProperty(exports, "createDate", { enumerable: true, get: function () { return createDate_js_1.createDate; } });
|
|
8
|
+
var createDateFactory_js_1 = require("./date-creator/createDateFactory/createDateFactory.js");
|
|
9
|
+
Object.defineProperty(exports, "createDateFactory", { enumerable: true, get: function () { return createDateFactory_js_1.createDateFactory; } });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a promise that resolves after a specified delay.
|
|
3
|
+
*
|
|
4
|
+
* Useful for pausing execution in async workflows,
|
|
5
|
+
* throttling operations, or simulating delays in tests.
|
|
6
|
+
*
|
|
7
|
+
* @group Time
|
|
8
|
+
*
|
|
9
|
+
* @param milliseconds - Time to wait in milliseconds.
|
|
10
|
+
* @param value - Optional value to resolve with.
|
|
11
|
+
*
|
|
12
|
+
* @returns A promise that resolves after the delay.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* await timeout(1000);
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const result = await timeout(1000, "done");
|
|
19
|
+
* console.log(result); // "done"
|
|
20
|
+
*/
|
|
21
|
+
export declare function timeout<T = void>(milliseconds: number, value?: T): Promise<T>;
|
|
22
|
+
//# sourceMappingURL=timeout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../../src/timeout/timeout.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,OAAO,CAAC,CAAC,GAAG,IAAI,EAAE,YAAY,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAQ7E"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.timeout = timeout;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a promise that resolves after a specified delay.
|
|
6
|
+
*
|
|
7
|
+
* Useful for pausing execution in async workflows,
|
|
8
|
+
* throttling operations, or simulating delays in tests.
|
|
9
|
+
*
|
|
10
|
+
* @group Time
|
|
11
|
+
*
|
|
12
|
+
* @param milliseconds - Time to wait in milliseconds.
|
|
13
|
+
* @param value - Optional value to resolve with.
|
|
14
|
+
*
|
|
15
|
+
* @returns A promise that resolves after the delay.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* await timeout(1000);
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const result = await timeout(1000, "done");
|
|
22
|
+
* console.log(result); // "done"
|
|
23
|
+
*/
|
|
24
|
+
function timeout(milliseconds, value) {
|
|
25
|
+
if (milliseconds < 0) {
|
|
26
|
+
return Promise.reject(new Error('Timeout duration must be >= 0'));
|
|
27
|
+
}
|
|
28
|
+
return new Promise((resolve) => {
|
|
29
|
+
setTimeout(() => resolve(value), milliseconds);
|
|
30
|
+
});
|
|
31
|
+
}
|