@ix-xs/node-comfort 1.2.0 → 2.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.
Files changed (78) hide show
  1. package/CHANGELOG.md +340 -0
  2. package/README.md +137 -548
  3. package/index.js +80 -35
  4. package/package.json +161 -33
  5. package/src/Arr.js +1050 -418
  6. package/src/Async.js +817 -0
  7. package/src/Cache.js +433 -0
  8. package/src/Checker.js +1202 -539
  9. package/src/Cli.js +1293 -0
  10. package/src/Color.js +556 -0
  11. package/src/Crypto.js +1082 -0
  12. package/src/Emitter.js +356 -0
  13. package/src/Env.js +689 -0
  14. package/src/FS.js +1626 -894
  15. package/src/Func.js +823 -356
  16. package/src/Http.js +700 -0
  17. package/src/Id.js +568 -200
  18. package/src/Logger.js +963 -378
  19. package/src/Num.js +965 -360
  20. package/src/Obj.js +1077 -379
  21. package/src/SQLite.js +1629 -1005
  22. package/src/Schema.js +2101 -0
  23. package/src/Str.js +1209 -432
  24. package/src/Sys.js +654 -0
  25. package/src/Time.js +1533 -440
  26. package/src/Utils.js +342 -325
  27. package/src/errors.js +335 -0
  28. package/src/internal/shared.js +415 -0
  29. package/types/index.d.ts +735 -526
  30. package/types/src/Arr.d.ts +533 -145
  31. package/types/src/Arr.d.ts.map +1 -1
  32. package/types/src/Async.d.ts +372 -0
  33. package/types/src/Async.d.ts.map +1 -0
  34. package/types/src/Cache.d.ts +221 -0
  35. package/types/src/Cache.d.ts.map +1 -0
  36. package/types/src/Checker.d.ts +651 -232
  37. package/types/src/Checker.d.ts.map +1 -1
  38. package/types/src/Cli.d.ts +664 -0
  39. package/types/src/Cli.d.ts.map +1 -0
  40. package/types/src/Color.d.ts +300 -0
  41. package/types/src/Color.d.ts.map +1 -0
  42. package/types/src/Crypto.d.ts +620 -0
  43. package/types/src/Crypto.d.ts.map +1 -0
  44. package/types/src/Emitter.d.ts +189 -0
  45. package/types/src/Emitter.d.ts.map +1 -0
  46. package/types/src/Env.d.ts +412 -0
  47. package/types/src/Env.d.ts.map +1 -0
  48. package/types/src/FS.d.ts +717 -238
  49. package/types/src/FS.d.ts.map +1 -1
  50. package/types/src/Func.d.ts +508 -135
  51. package/types/src/Func.d.ts.map +1 -1
  52. package/types/src/Http.d.ts +444 -0
  53. package/types/src/Http.d.ts.map +1 -0
  54. package/types/src/Id.d.ts +199 -66
  55. package/types/src/Id.d.ts.map +1 -1
  56. package/types/src/Logger.d.ts +414 -46
  57. package/types/src/Logger.d.ts.map +1 -1
  58. package/types/src/Num.d.ts +514 -137
  59. package/types/src/Num.d.ts.map +1 -1
  60. package/types/src/Obj.d.ts +453 -104
  61. package/types/src/Obj.d.ts.map +1 -1
  62. package/types/src/SQLite.d.ts +754 -308
  63. package/types/src/SQLite.d.ts.map +1 -1
  64. package/types/src/Schema.d.ts +1189 -0
  65. package/types/src/Schema.d.ts.map +1 -0
  66. package/types/src/Str.d.ts +617 -166
  67. package/types/src/Str.d.ts.map +1 -1
  68. package/types/src/Sys.d.ts +314 -0
  69. package/types/src/Sys.d.ts.map +1 -0
  70. package/types/src/Time.d.ts +759 -204
  71. package/types/src/Time.d.ts.map +1 -1
  72. package/types/src/Utils.d.ts +158 -102
  73. package/types/src/Utils.d.ts.map +1 -1
  74. package/types/src/errors.d.ts +316 -0
  75. package/types/src/errors.d.ts.map +1 -0
  76. package/types/src/internal/shared.d.ts +123 -0
  77. package/types/src/internal/shared.d.ts.map +1 -0
  78. package/types/index.d.ts.map +0 -1
@@ -1,205 +1,760 @@
1
- declare namespace _exports {
2
- export { TimeModule };
3
- }
4
- declare namespace _exports {
5
- /**
6
- * Sets the global default locale used by `relative`, `formatDuration` and
7
- * `format` (named tokens). Chainable.
8
- *
9
- * @example
10
- * nodeComfort.time.setLocale("fr");
11
- *
12
- * @param {string} locale - A BCP-47 locale tag (e.g. `"fr"`, `"en-GB"`, `"ja"`).
13
- * @returns {TimeModule} The time module (for chaining).
14
- */
15
- function setLocale(locale: string): TimeModule;
16
- /**
17
- * Sets the global default time zone used by `format` and `isSameDay`.
18
- * Chainable. Pass `undefined` to fall back to the system local zone.
19
- *
20
- * @example
21
- * nodeComfort.time.setTimezone("Europe/Paris");
22
- *
23
- * @param {string|undefined} timeZone - An IANA time-zone name (e.g. `"America/New_York"`).
24
- * @returns {TimeModule} The time module (for chaining).
25
- */
26
- function setTimezone(timeZone: string | undefined): TimeModule;
27
- /**
28
- * Returns the current global `{ locale, timeZone }` defaults.
29
- *
30
- * @returns {{ locale: string, timeZone: string|undefined }} A copy of the config.
31
- */
32
- function getConfig(): {
33
- locale: string;
34
- timeZone: string | undefined;
35
- };
36
- /**
37
- * Parses a human duration string into milliseconds.
38
- *
39
- * Accepts things like `"1h"`, `"30m"`, `"1h30m"`, `"2 days"`, `"500ms"`.
40
- * A plain number is treated as milliseconds.
41
- *
42
- * @example
43
- * nodeComfort.time.parseDuration("1h30m"); // 5400000
44
- * nodeComfort.time.parseDuration("2 days"); // 172800000
45
- *
46
- * @param {string|number} input - Duration string or millisecond count.
47
- * @returns {number|null} Milliseconds, or null if it can't be parsed.
48
- */
49
- function parseDuration(input: string | number): number | null;
50
- /**
51
- * Formats a millisecond duration into a human string.
52
- *
53
- * The compact form (default) is language-neutral (`"1h 30m"`). The `long`
54
- * form is fully localized through `Intl` • pass `options.locale` or set a
55
- * global locale with {@link module:Time.setLocale}.
56
- *
57
- * @example
58
- * nodeComfort.time.formatDuration(5400000); // "1h 30m"
59
- * nodeComfort.time.formatDuration(90000, { long: true }); // "1 minute 30 seconds"
60
- * nodeComfort.time.formatDuration(90000, { long: true, locale: "fr" }); // "1 minute 30 secondes"
61
- *
62
- * @param {number} ms - Duration in milliseconds.
63
- * @param {{ long?: boolean, units?: number, locale?: string }} [options] - Formatting options (`units` caps how many parts show).
64
- * @returns {string} The formatted duration.
65
- */
66
- function formatDuration(ms: number, options?: {
67
- long?: boolean;
68
- units?: number;
69
- locale?: string;
70
- }): string;
71
- /**
72
- * Returns a human-friendly, localized relative time string.
73
- *
74
- * Built on `Intl.RelativeTimeFormat`, so it speaks every language. Set
75
- * `options.locale` (or a global locale) to translate; `options.numeric`
76
- * controls wording (`"always"` → "1 day ago", `"auto"` → "yesterday").
77
- *
78
- * @example
79
- * nodeComfort.time.relative(Date.now() - 3600e3); // "1 hour ago"
80
- * nodeComfort.time.relative(Date.now() - 3600e3, undefined, { locale: "fr" }); // "il y a 1 heure"
81
- * nodeComfort.time.relative(Date.now() - 86400e3, undefined, { numeric: "auto" }); // "yesterday"
82
- *
83
- * @param {Date|number|string} date - Target date.
84
- * @param {Date|number|string} [from] - Reference date (defaults to now).
85
- * @param {{ locale?: string, numeric?: "always"|"auto" }} [options] - Formatting options.
86
- * @returns {string} The relative time string.
87
- */
88
- function relative(date: Date | number | string, from?: Date | number | string, options?: {
89
- locale?: string;
90
- numeric?: "always" | "auto";
91
- }): string;
92
- /**
93
- * Formats a date using a small token syntax, with optional locale & time zone.
94
- *
95
- * **Numeric tokens:** `YYYY` `YY` `MM` `M` `DD` `D` `HH` `H` (24h) `hh` `h`
96
- * (12h) `mm` `m` `ss` `s` `SSS`.
97
- * **Localized tokens:** `MMMM` (month name) `MMM` (short month) `dddd` (weekday)
98
- * `ddd` (short weekday) `A` (AM/PM) `a` (am/pm).
99
- *
100
- * Wrap literal text in square brackets to keep it verbatim: `"[Today is] dddd"`.
101
- *
102
- * @example
103
- * nodeComfort.time.format(new Date(), "YYYY-MM-DD HH:mm:ss");
104
- * nodeComfort.time.format(Date.now(), "dddd D MMMM YYYY", { locale: "fr" });
105
- * nodeComfort.time.format(Date.now(), "HH:mm", { timeZone: "Asia/Tokyo" });
106
- *
107
- * @param {Date|number|string} date - Date to format.
108
- * @param {string} [pattern="YYYY-MM-DD HH:mm:ss"] - Format pattern.
109
- * @param {{ locale?: string, timeZone?: string }} [options] - Locale / time-zone overrides.
110
- * @returns {string} The formatted date.
111
- */
112
- function format(date: Date | number | string, pattern?: string, options?: {
113
- locale?: string;
114
- timeZone?: string;
115
- }): string;
116
- /**
117
- * Adds a duration to a date and returns a new Date.
118
- *
119
- * The duration may be a millisecond number or a string like `"1h30m"`.
120
- *
121
- * @example
122
- * nodeComfort.time.add(new Date(), "2h");
123
- * nodeComfort.time.add(new Date(), -1, "days");
124
- *
125
- * @param {Date|number|string} date - Base date.
126
- * @param {number|string} amount - Amount (ms/string) or a number paired with `unit`.
127
- * @param {"ms"|"s"|"m"|"h"|"d"|"w"} [unit] - Unit when `amount` is a plain number.
128
- * @returns {Date} The resulting date.
129
- */
130
- function add(date: Date | number | string, amount: number | string, unit?: "ms" | "s" | "m" | "h" | "d" | "w"): Date;
131
- /**
132
- * Subtracts a duration from a date and returns a new Date.
133
- *
134
- * @param {Date|number|string} date - Base date.
135
- * @param {number|string} amount - Amount (ms/string) or a number paired with `unit`.
136
- * @param {"ms"|"s"|"m"|"h"|"d"|"w"} [unit] - Unit when `amount` is a plain number.
137
- * @returns {Date} The resulting date.
138
- */
139
- function subtract(date: Date | number | string, amount: number | string, unit?: "ms" | "s" | "m" | "h" | "d" | "w"): Date;
140
- /**
141
- * Returns the difference between two dates in the requested unit.
142
- *
143
- * @example
144
- * nodeComfort.time.diff("2024-01-02", "2024-01-01", "d"); // 1
145
- *
146
- * @param {Date|number|string} a - First date.
147
- * @param {Date|number|string} b - Second date.
148
- * @param {"ms"|"s"|"m"|"h"|"d"|"w"} [unit="ms"] - Result unit.
149
- * @returns {number} The signed difference (`a - b`) in the given unit.
150
- */
151
- function diff(a: Date | number | string, b: Date | number | string, unit?: "ms" | "s" | "m" | "h" | "d" | "w"): number;
152
- /**
153
- * Checks whether two dates fall on the same calendar day.
154
- *
155
- * Uses local time by default; pass `options.timeZone` (or set a global zone)
156
- * to compare calendar days in a specific zone.
157
- *
158
- * @param {Date|number|string} a - First date.
159
- * @param {Date|number|string} b - Second date.
160
- * @param {{ timeZone?: string }} [options] - Time-zone override.
161
- * @returns {boolean} True if both are the same day.
162
- */
163
- function isSameDay(a: Date | number | string, b: Date | number | string, options?: {
164
- timeZone?: string;
165
- }): boolean;
166
- /**
167
- * Returns a new Date at the start of the given unit (day/hour/etc).
168
- *
169
- * @example
170
- * nodeComfort.time.startOf(new Date(), "day"); // midnight today
171
- *
172
- * @param {Date|number|string} date - Base date.
173
- * @param {"year"|"month"|"day"|"hour"|"minute"|"second"} unit - Unit to reset to.
174
- * @returns {Date} The start-of-unit date.
175
- */
176
- function startOf(date: Date | number | string, unit: "year" | "month" | "day" | "hour" | "minute" | "second"): Date;
177
- /**
178
- * Returns the current Unix timestamp in seconds.
179
- *
180
- * @returns {number} Seconds since the Unix epoch.
181
- */
182
- function unix(): number;
183
- /**
184
- * Returns a high-resolution stopwatch you can `.stop()` to get elapsed ms.
185
- *
186
- * @example
187
- * const timer = nodeComfort.time.stopwatch();
188
- * doWork();
189
- * console.log(timer.stop()); // e.g. 12.34 (ms, sub-ms precise)
190
- * console.log(timer.stop(true)); // "12.34ms"
191
- *
192
- * @returns {{ elapsed: () => number, stop: (format?: boolean) => number|string }} A stopwatch handle.
193
- */
194
- function stopwatch(): {
195
- elapsed: () => number;
196
- stop: (format?: boolean) => number | string;
197
- };
198
- }
199
- export = _exports;
200
- /**
201
- * The time module itself • used as the return type of the chainable setters so
202
- * `setLocale().setTimezone()...` stays fully typed without inlining the type.
203
- */
204
- type TimeModule = typeof import("./Time");
1
+ /**
2
+ * A date: a `Date`, a timestamp in ms, or a string `new Date()` understands
3
+ * (ISO 8601 is safest).
4
+ */
5
+ export type DateInput = Date | number | string;
6
+ /**
7
+ * A unit with a fixed length.
8
+ */
9
+ export type DurationUnit = "ms" | "millisecond" | "milliseconds" | "s" | "sec" | "second" | "seconds" | "m" | "min" | "minute" | "minutes" | "h" | "hour" | "hours" | "d" | "day" | "days" | "w" | "week" | "weeks";
10
+ /**
11
+ * A fixed unit, or months and years, whose length varies.
12
+ */
13
+ export type CalendarUnit = DurationUnit | "M" | "month" | "months" | "y" | "year" | "years";
14
+ /**
15
+ * A unit for `startOf()` and `endOf()`.
16
+ */
17
+ export type StartOfUnit = "year" | "quarter" | "month" | "week" | "isoWeek" | "day" | "hour" | "minute" | "second";
18
+ /**
19
+ * Options for `formatDuration()`.
20
+ */
21
+ export type FormatDurationOptions = {
22
+ /**
23
+ * Full, translated words: `"1 hour 30 minutes"` instead of `"1h 30m"`.
24
+ */
25
+ long?: boolean | undefined;
26
+ /**
27
+ * Show at most this many units, largest first: `units: 2` turns `"1d 3h 5m"` into `"1d 3h"`.
28
+ */
29
+ units?: number | undefined;
30
+ /**
31
+ * Clock style, like `"01:30:05"`.
32
+ */
33
+ clock?: boolean | undefined;
34
+ /**
35
+ * Show milliseconds. Defaults to `true`.
36
+ */
37
+ milliseconds?: boolean | undefined;
38
+ /**
39
+ * Language for `long`. Defaults to the global locale.
40
+ */
41
+ locale?: string | undefined;
42
+ };
43
+ /**
44
+ * Options for `relative()`.
45
+ */
46
+ export type RelativeOptions = {
47
+ /**
48
+ * Defaults to the global locale.
49
+ */
50
+ locale?: string | undefined;
51
+ /**
52
+ * `"auto"` says `"yesterday"` instead of `"1 day ago"`. Defaults to `"always"`.
53
+ */
54
+ numeric?: "auto" | "always" | undefined;
55
+ /**
56
+ * `"short"` gives `"in 3 mo."`. Defaults to `"long"`.
57
+ */
58
+ style?: "long" | "short" | "narrow" | undefined;
59
+ };
60
+ /**
61
+ * Options for `format()`.
62
+ */
63
+ export type FormatDateOptions = {
64
+ /**
65
+ * Language of month and day names. Defaults to the global locale.
66
+ */
67
+ locale?: string | undefined;
68
+ /**
69
+ * Show the date in this zone, like `"Asia/Tokyo"`. Defaults to the global zone, then the system's.
70
+ */
71
+ timeZone?: string | undefined;
72
+ };
73
+ /**
74
+ * Options for `calendar()`.
75
+ */
76
+ export type CalendarOptions = {
77
+ /**
78
+ * Defaults to the global locale.
79
+ */
80
+ locale?: string | undefined;
81
+ /**
82
+ * Defaults to the global zone, then the system's.
83
+ */
84
+ timeZone?: string | undefined;
85
+ /**
86
+ * What "now" is. Defaults to the current time.
87
+ */
88
+ now?: DateInput | undefined;
89
+ };
90
+ /**
91
+ * Week settings.
92
+ */
93
+ export type WeekOptions = {
94
+ /**
95
+ * First day of the week: `0` for Sunday, `1` for Monday. Defaults to `1`.
96
+ */
97
+ weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined;
98
+ /**
99
+ * Zone whose calendar is used. Defaults to the global zone, then the system's.
100
+ */
101
+ timeZone?: string | undefined;
102
+ };
103
+ /**
104
+ * Options for `parse()`.
105
+ */
106
+ export type ParseDateOptions = {
107
+ /**
108
+ * Language of month names in the text. Defaults to the global locale.
109
+ */
110
+ locale?: string | undefined;
111
+ /**
112
+ * Zone the text is written in, unless it carries its own offset. Defaults to the global zone, then the system's.
113
+ */
114
+ timeZone?: string | undefined;
115
+ };
116
+ /**
117
+ * Options for `range()`.
118
+ */
119
+ export type DateRangeOptions = {
120
+ /**
121
+ * How many units between two dates. Defaults to `1`.
122
+ */
123
+ step?: number | undefined;
124
+ /**
125
+ * Zone whose calendar is used for days, weeks, months and years.
126
+ */
127
+ timeZone?: string | undefined;
128
+ };
129
+ /**
130
+ * Time zone setting.
131
+ */
132
+ export type TimeZoneOptions = {
133
+ /**
134
+ * Zone used to decide which day it is. Defaults to the global zone, then the system's.
135
+ */
136
+ timeZone?: string | undefined;
137
+ };
138
+ /**
139
+ * Options for `every()`.
140
+ */
141
+ export type EveryOptions = {
142
+ /**
143
+ * Also run once right away.
144
+ */
145
+ immediate?: boolean | undefined;
146
+ /**
147
+ * Let the process exit even if the schedule is active.
148
+ */
149
+ unref?: boolean | undefined;
150
+ /**
151
+ * Called when the task fails. Without it, errors surface as unhandled errors.
152
+ */
153
+ onError?: ((error: unknown) => void) | undefined;
154
+ };
155
+ /**
156
+ * Options for `cron()` and `nextRun()`.
157
+ */
158
+ export type CronOptions = {
159
+ /**
160
+ * Zone in which the expression is read, like `"Europe/Paris"`. Defaults to the global zone, then the system's.
161
+ */
162
+ timeZone?: string | undefined;
163
+ /**
164
+ * `nextRun()` only: search after this date. Defaults to now.
165
+ */
166
+ from?: DateInput | undefined;
167
+ /**
168
+ * Let the process exit even if the schedule is active.
169
+ */
170
+ unref?: boolean | undefined;
171
+ /**
172
+ * Start a run even if the previous one hasn't finished. By default it's skipped.
173
+ */
174
+ overlap?: boolean | undefined;
175
+ /**
176
+ * `cron()` only: also run once right away.
177
+ */
178
+ immediate?: boolean | undefined;
179
+ /**
180
+ * Called when the task fails.
181
+ */
182
+ onError?: ((error: unknown) => void) | undefined;
183
+ };
184
+ /**
185
+ * A schedule returned by `every()` and `cron()`.
186
+ */
187
+ export type ScheduledTask = {
188
+ /**
189
+ * Stops the schedule. A run in progress finishes.
190
+ */
191
+ stop: () => void;
192
+ /**
193
+ * When the next run is planned.
194
+ */
195
+ next: () => Date | undefined;
196
+ /**
197
+ * Whether a run is in progress.
198
+ */
199
+ isRunning: () => boolean;
200
+ /**
201
+ * How many runs have started.
202
+ */
203
+ runs: () => number;
204
+ };
205
+ /**
206
+ * Returned by `stopwatch()`.
207
+ */
208
+ export type Stopwatch = {
209
+ /**
210
+ * Milliseconds since the start.
211
+ */
212
+ elapsed: () => number;
213
+ /**
214
+ * The elapsed time; `stop(true)` formats it, like `"1.5s"`.
215
+ */
216
+ stop: (format?: boolean) => number | string;
217
+ /**
218
+ * Records a lap and returns the ms since the previous one.
219
+ */
220
+ lap: (label?: string) => number;
221
+ /**
222
+ * Every lap so far.
223
+ */
224
+ laps: () => Array<{
225
+ label: string;
226
+ ms: number;
227
+ total: number;
228
+ }>;
229
+ /**
230
+ * Starts over.
231
+ */
232
+ reset: () => void;
233
+ };
234
+ /**
235
+ * Sets the default language for `relative`, `calendar`, `formatDuration`
236
+ * and `format`.
237
+ *
238
+ * @example
239
+ * time.setLocale("fr");
240
+ * time.relative(Date.now() - 60e3); // "il y a 1 minute"
241
+ *
242
+ * @param {string} locale Like `"fr"`, `"en-GB"`, `"pt-BR"` or `"ja"`.
243
+ * @returns {typeof import("./Time")} The module, so calls chain.
244
+ */
245
+ export function setLocale(locale: string): typeof import("./Time");
246
+ /**
247
+ * Sets the default time zone for `format`, `calendar`, `isSameDay`,
248
+ * `cron`... Pass `undefined` to go back to the system zone.
249
+ *
250
+ * @example
251
+ * time.setTimezone("Europe/Paris");
252
+ *
253
+ * @param {string | undefined} timeZone Like `"America/New_York"`.
254
+ * @returns {typeof import("./Time")} The module, so calls chain.
255
+ * @throws {RangeError} If the zone doesn't exist.
256
+ */
257
+ export function setTimezone(timeZone: string | undefined): typeof import("./Time");
258
+ /**
259
+ * The current defaults.
260
+ *
261
+ * @example
262
+ * time.getConfig(); // { locale: "en", timeZone: undefined }
263
+ *
264
+ * @returns {{ locale: string, timeZone: string | undefined }}
265
+ */
266
+ export function getConfig(): {
267
+ locale: string;
268
+ timeZone: string | undefined;
269
+ };
270
+ /**
271
+ * Every time zone Node knows, sorted.
272
+ *
273
+ * @example
274
+ * time.timezones().includes("Europe/Paris"); // true
275
+ *
276
+ * @returns {string[]}
277
+ */
278
+ export function timezones(): string[];
279
+ /**
280
+ * A zone's UTC offset at a given date, in minutes. Daylight saving included.
281
+ *
282
+ * @example
283
+ * time.offset("Europe/Paris", "2024-07-01"); // 120
284
+ * time.offset("Europe/Paris", "2024-01-01"); // 60
285
+ *
286
+ * @param {string} [timeZone] Defaults to the global zone, then the system's.
287
+ * @param {DateInput} [date] Defaults to now.
288
+ * @returns {number}
289
+ */
290
+ export function offset(timeZone?: string, date?: DateInput): number;
291
+ /**
292
+ * Turns a duration into milliseconds. Understands `"1h30m"`, `"2 days"`,
293
+ * `"500ms"`, clock times like `"01:30:00"`, ISO durations like `"PT1H30M"`,
294
+ * and plain numbers. Months and years are refused because their length
295
+ * varies; use `add()` for those.
296
+ *
297
+ * @example
298
+ * time.parseDuration("1h30m"); // 5400000
299
+ * time.parseDuration("01:30:00"); // 5400000
300
+ * time.parseDuration("banana"); // null
301
+ *
302
+ * @param {string | number} input
303
+ * @returns {number | null} `null` when it isn't a duration.
304
+ */
305
+ export function parseDuration(input: string | number): number | null;
306
+ /**
307
+ * Writes a duration for humans.
308
+ *
309
+ * @example
310
+ * time.formatDuration(5_400_000); // "1h 30m"
311
+ * time.formatDuration(90_000, { long: true }); // "1 minute 30 seconds"
312
+ * time.formatDuration(90_000, { long: true, locale: "fr" }); // "1 minute 30 secondes"
313
+ * time.formatDuration(93_784_000, { units: 2 }); // "1d 2h"
314
+ * time.formatDuration(5_405_000, { clock: true }); // "01:30:05"
315
+ * time.formatDuration("90m"); // "1h 30m"
316
+ *
317
+ * @param {number | string} duration Milliseconds, or text like `"90m"`.
318
+ * @param {FormatDurationOptions} [options]
319
+ * @returns {string}
320
+ */
321
+ export function formatDuration(duration: number | string, options?: FormatDurationOptions): string;
322
+ /**
323
+ * Describes a date relative to now: `"3 hours ago"`, `"in 2 days"`, in
324
+ * any language.
325
+ *
326
+ * @example
327
+ * time.relative(Date.now() - 3_600_000); // "1 hour ago"
328
+ * time.relative(Date.now() - 86_400_000, undefined, { numeric: "auto" }); // "yesterday"
329
+ * time.relative(Date.now() - 3_600_000, undefined, { locale: "fr" }); // "il y a 1 heure"
330
+ *
331
+ * @param {DateInput} date
332
+ * @param {DateInput} [from] Compare with this date instead of now.
333
+ * @param {RelativeOptions} [options]
334
+ * @returns {string}
335
+ */
336
+ export function relative(date: DateInput, from?: DateInput, options?: RelativeOptions): string;
337
+ /**
338
+ * Describes a date like a chat app does: `"Today at 2:30 PM"`,
339
+ * `"Yesterday at 9:05 AM"`, a weekday for the coming days, and the full date
340
+ * beyond that.
341
+ *
342
+ * @example
343
+ * time.calendar(new Date()); // "Today at 2:30 PM"
344
+ * time.calendar(yesterday, { locale: "fr" }); // "Hier à 09:05"
345
+ *
346
+ * @param {DateInput} date
347
+ * @param {CalendarOptions} [options]
348
+ * @returns {string}
349
+ */
350
+ export function calendar(date: DateInput, options?: CalendarOptions): string;
351
+ /**
352
+ * Formats a date with tokens, in any language and time zone.
353
+ *
354
+ * | Token | Output | | Token | Output |
355
+ * | --- | --- | --- | --- | --- |
356
+ * | `YYYY` / `YY` | 2024 / 24 | | `HH` / `H` | 09 / 9 (24h) |
357
+ * | `Q` | quarter, 1-4 | | `hh` / `h` | 09 / 9 (12h) |
358
+ * | `MMMM` / `MMM` | January / Jan | | `mm` / `m` | 05 / 5 |
359
+ * | `MM` / `M` | 01 / 1 | | `ss` / `s` | 07 / 7 |
360
+ * | `DD` / `D` | 05 / 5 | | `SSS` | 042 (ms) |
361
+ * | `dddd` / `ddd` | Monday / Mon | | `A` / `a` | PM / pm |
362
+ * | `d` | weekday, 0 is Sunday | | `Z` / `ZZ` | +02:00 / +0200 |
363
+ * | `W` / `WW` | ISO week | | `X` / `x` | Unix seconds / ms |
364
+ * | `Do` | 5th | | `z` | zone name, like EST or GMT+1 |
365
+ *
366
+ * Text in brackets stays as is: `"[Today is] dddd"`.
367
+ *
368
+ * @example
369
+ * time.format(new Date(), "YYYY-MM-DD HH:mm:ss"); // "2024-01-15 14:30:05"
370
+ * time.format(Date.now(), "dddd D MMMM YYYY", { locale: "fr" }); // "lundi 15 janvier 2024"
371
+ * time.format(Date.now(), "HH:mm Z", { timeZone: "Asia/Tokyo" }); // "22:30 +09:00"
372
+ *
373
+ * @param {DateInput} date
374
+ * @param {string} [pattern="YYYY-MM-DD HH:mm:ss"]
375
+ * @param {FormatDateOptions} [options]
376
+ * @returns {string} Or `"Invalid Date"`.
377
+ */
378
+ export function format(date: DateInput, pattern?: string, options?: FormatDateOptions): string;
379
+ /**
380
+ * Reads a date written in a known format: the opposite of `format()`, with
381
+ * the same tokens. Parts the pattern doesn't have default to today's date
382
+ * and to midnight.
383
+ *
384
+ * @example
385
+ * time.parse("15/01/2024 14:30", "DD/MM/YYYY HH:mm");
386
+ * time.parse("15 janvier 2024", "D MMMM YYYY", { locale: "fr" });
387
+ * time.parse("2024-01-15 09:00", "YYYY-MM-DD HH:mm", { timeZone: "Asia/Tokyo" });
388
+ * time.parse("31/02/2024", "DD/MM/YYYY"); // undefined, no such day
389
+ *
390
+ * @param {string} text
391
+ * @param {string} pattern Tokens like `YYYY`, `MM`, `DD`, `HH`, `mm`, `ss`, `A`, `Z`. Text in brackets must match as is.
392
+ * @param {ParseDateOptions} [options]
393
+ * @returns {Date | undefined} `undefined` when the text doesn't match or isn't a real date.
394
+ */
395
+ export function parse(text: string, pattern: string, options?: ParseDateOptions): Date | undefined;
396
+ /**
397
+ * The date as `YYYY-MM-DD`, the format `<input type="date">` and most APIs
398
+ * expect.
399
+ *
400
+ * @example
401
+ * time.toISODate(new Date(2024, 0, 5)); // "2024-01-05"
402
+ *
403
+ * @param {DateInput} [date] Defaults to now.
404
+ * @param {TimeZoneOptions} [options]
405
+ * @returns {string}
406
+ */
407
+ export function toISODate(date?: DateInput, options?: TimeZoneOptions): string;
408
+ /**
409
+ * Adds time to a date and returns a new `Date`. Whole days, weeks, months
410
+ * and years follow the calendar: one day later is the same time tomorrow,
411
+ * even when the clocks change in between, and January 31 plus one month is
412
+ * the end of February. Hours and smaller units are exact.
413
+ *
414
+ * @example
415
+ * time.add(new Date(), "2h30m");
416
+ * time.add(new Date(), 3, "days");
417
+ * time.add(new Date(), "1 month");
418
+ * time.add(new Date(2024, 0, 31), 1, "month"); // 2024-02-29
419
+ * time.add(date, "1 day", { timeZone: "Europe/Paris" });
420
+ *
421
+ * @param {DateInput} date
422
+ * @param {number | string} amount A number with `unit`, or a duration like `"1h30m"` or `"1 month"`.
423
+ * @param {CalendarUnit | TimeZoneOptions} [unit="ms"] The unit of a number. With a text amount there is none to give: the options can come here.
424
+ * @param {TimeZoneOptions} [options] The zone whose calendar is followed.
425
+ * @returns {Date}
426
+ * @throws {TypeError} If a text amount isn't a duration, or the unit isn't one.
427
+ */
428
+ export function add(date: DateInput, amount: number | string, unit?: CalendarUnit | TimeZoneOptions, options?: TimeZoneOptions): Date;
429
+ /**
430
+ * Subtracts time from a date and returns a new `Date`.
431
+ *
432
+ * @example
433
+ * time.subtract(new Date(), 7, "days");
434
+ * time.subtract(new Date(), "90m");
435
+ *
436
+ * @param {DateInput} date
437
+ * @param {number | string} amount
438
+ * @param {CalendarUnit | TimeZoneOptions} [unit="ms"] The unit of a number, or the options when the amount is text.
439
+ * @param {TimeZoneOptions} [options]
440
+ * @returns {Date}
441
+ * @throws {TypeError} If a text amount isn't a duration, or the unit isn't one.
442
+ */
443
+ export function subtract(date: DateInput, amount: number | string, unit?: CalendarUnit | TimeZoneOptions, options?: TimeZoneOptions): Date;
444
+ /**
445
+ * `a - b` in a unit. Fixed units can give fractions; months and years are
446
+ * whole calendar months and years.
447
+ *
448
+ * @example
449
+ * time.diff("2024-01-03", "2024-01-01", "days"); // 2
450
+ * time.diff("2024-03-15", "2024-01-20", "months"); // 1
451
+ *
452
+ * @param {DateInput} a
453
+ * @param {DateInput} b
454
+ * @param {CalendarUnit} [unit="ms"]
455
+ * @param {TimeZoneOptions} [options] The zone whose calendar months and years follow.
456
+ * @returns {number}
457
+ * @throws {TypeError} If the unit isn't one.
458
+ */
459
+ export function diff(a: DateInput, b: DateInput, unit?: CalendarUnit, options?: TimeZoneOptions): number;
460
+ /**
461
+ * A new `Date` at the start of the day, week, month... in the global time
462
+ * zone, or the one you name.
463
+ *
464
+ * @example
465
+ * time.startOf(new Date(), "day"); // today at 00:00
466
+ * time.startOf(new Date(), "week"); // Monday at 00:00
467
+ * time.startOf(new Date(), "week", { weekStartsOn: 0 }); // Sunday at 00:00
468
+ * time.startOf(new Date(), "day", { timeZone: "Asia/Tokyo" }); // midnight in Tokyo
469
+ *
470
+ * @param {DateInput} date
471
+ * @param {StartOfUnit} unit
472
+ * @param {WeekOptions} [options]
473
+ * @returns {Date}
474
+ */
475
+ export function startOf(date: DateInput, unit: StartOfUnit, options?: WeekOptions): Date;
476
+ /**
477
+ * A new `Date` at the last millisecond of the day, week, month...
478
+ *
479
+ * @example
480
+ * time.endOf(new Date(), "month"); // last day of the month, 23:59:59.999
481
+ *
482
+ * @param {DateInput} date
483
+ * @param {StartOfUnit} unit
484
+ * @param {WeekOptions} [options]
485
+ * @returns {Date}
486
+ */
487
+ export function endOf(date: DateInput, unit: StartOfUnit, options?: WeekOptions): Date;
488
+ /**
489
+ * Is `a` before `b`?
490
+ *
491
+ * @param {DateInput} a
492
+ * @param {DateInput} b
493
+ * @returns {boolean}
494
+ */
495
+ export function isBefore(a: DateInput, b: DateInput): boolean;
496
+ /**
497
+ * Is `a` after `b`?
498
+ *
499
+ * @example
500
+ * if (time.isAfter(expiresAt, Date.now())) grantAccess();
501
+ *
502
+ * @param {DateInput} a
503
+ * @param {DateInput} b
504
+ * @returns {boolean}
505
+ */
506
+ export function isAfter(a: DateInput, b: DateInput): boolean;
507
+ /**
508
+ * Is the date between `start` and `end`, both included?
509
+ *
510
+ * @param {DateInput} date
511
+ * @param {DateInput} start
512
+ * @param {DateInput} end
513
+ * @returns {boolean}
514
+ */
515
+ export function isBetween(date: DateInput, start: DateInput, end: DateInput): boolean;
516
+ /**
517
+ * Are both dates in the same year, month, week, day, hour...?
518
+ *
519
+ * @example
520
+ * time.isSame(a, b, "month");
521
+ * time.isSame(a, b, "day", { timeZone: "America/New_York" });
522
+ *
523
+ * @param {DateInput} a
524
+ * @param {DateInput} b
525
+ * @param {StartOfUnit} unit
526
+ * @param {WeekOptions} [options]
527
+ * @returns {boolean}
528
+ */
529
+ export function isSame(a: DateInput, b: DateInput, unit: StartOfUnit, options?: WeekOptions): boolean;
530
+ /**
531
+ * Are both dates on the same calendar day?
532
+ *
533
+ * @example
534
+ * time.isSameDay(a, b); // local time
535
+ * time.isSameDay(a, b, { timeZone: "Asia/Tokyo" }); // as seen in Tokyo
536
+ *
537
+ * @param {DateInput} a
538
+ * @param {DateInput} b
539
+ * @param {TimeZoneOptions} [options]
540
+ * @returns {boolean}
541
+ */
542
+ export function isSameDay(a: DateInput, b: DateInput, options?: TimeZoneOptions): boolean;
543
+ /**
544
+ * Is the date today?
545
+ *
546
+ * @param {DateInput} date
547
+ * @param {TimeZoneOptions} [options]
548
+ * @returns {boolean}
549
+ */
550
+ export function isToday(date: DateInput, options?: TimeZoneOptions): boolean;
551
+ /**
552
+ * Was the date yesterday?
553
+ *
554
+ * @param {DateInput} date
555
+ * @param {TimeZoneOptions} [options]
556
+ * @returns {boolean}
557
+ */
558
+ export function isYesterday(date: DateInput, options?: TimeZoneOptions): boolean;
559
+ /**
560
+ * Is the date tomorrow?
561
+ *
562
+ * @param {DateInput} date
563
+ * @param {TimeZoneOptions} [options]
564
+ * @returns {boolean}
565
+ */
566
+ export function isTomorrow(date: DateInput, options?: TimeZoneOptions): boolean;
567
+ /**
568
+ * Is it a Saturday or a Sunday?
569
+ *
570
+ * @param {DateInput} date
571
+ * @param {TimeZoneOptions} [options]
572
+ * @returns {boolean}
573
+ */
574
+ export function isWeekend(date: DateInput, options?: TimeZoneOptions): boolean;
575
+ /**
576
+ * Is it a leap year?
577
+ *
578
+ * @example
579
+ * time.isLeapYear(2024); // true
580
+ * time.isLeapYear(1900); // false
581
+ *
582
+ * @param {number | DateInput} yearOrDate A year (1000 to 9999) or a date.
583
+ * @returns {boolean}
584
+ */
585
+ export function isLeapYear(yearOrDate: number | DateInput): boolean;
586
+ /**
587
+ * Is it a real date, or something that parses to one?
588
+ *
589
+ * @example
590
+ * time.isValid("2024-02-29"); // true
591
+ * time.isValid("2024-02-30"); // false
592
+ *
593
+ * @param {unknown} value
594
+ * @returns {boolean}
595
+ */
596
+ export function isValid(value: unknown): boolean;
597
+ /**
598
+ * Number of days in a month.
599
+ *
600
+ * @example
601
+ * time.daysInMonth(new Date(2024, 1)); // 29
602
+ * time.daysInMonth(2023, 2); // 28
603
+ *
604
+ * @param {DateInput | number} dateOrYear A date, or a year when you pass `month`.
605
+ * @param {number} [month] 1 to 12.
606
+ * @returns {number}
607
+ */
608
+ export function daysInMonth(dateOrYear: DateInput | number, month?: number): number;
609
+ /**
610
+ * Day of the year, from 1 to 366.
611
+ *
612
+ * @param {DateInput} [date] Defaults to now.
613
+ * @param {TimeZoneOptions} [options]
614
+ * @returns {number}
615
+ */
616
+ export function dayOfYear(date?: DateInput, options?: TimeZoneOptions): number;
617
+ /**
618
+ * ISO week number, from 1 to 53. Weeks start on Monday, and week 1 holds
619
+ * the year's first Thursday.
620
+ *
621
+ * @example
622
+ * time.weekOfYear(new Date(2021, 0, 3)); // 53, the last week of 2020
623
+ *
624
+ * @param {DateInput} [date] Defaults to now.
625
+ * @param {{ utc?: boolean, timeZone?: string }} [options] `utc` reads the date in UTC, `timeZone` in another zone.
626
+ * @returns {number}
627
+ */
628
+ export function weekOfYear(date?: DateInput, options?: {
629
+ utc?: boolean;
630
+ timeZone?: string;
631
+ }): number;
632
+ /**
633
+ * The earliest date.
634
+ *
635
+ * @param {Iterable<DateInput>} dates
636
+ * @returns {Date | undefined}
637
+ */
638
+ export function min(dates: Iterable<DateInput>): Date | undefined;
639
+ /**
640
+ * The latest date.
641
+ *
642
+ * @param {Iterable<DateInput>} dates
643
+ * @returns {Date | undefined}
644
+ */
645
+ export function max(dates: Iterable<DateInput>): Date | undefined;
646
+ /**
647
+ * The current Unix timestamp, in seconds.
648
+ *
649
+ * @returns {number}
650
+ */
651
+ export function unix(): number;
652
+ /**
653
+ * A `Date` from a Unix timestamp in seconds, as found in JWTs and many APIs.
654
+ *
655
+ * @example
656
+ * time.fromUnix(1705329000); // 2024-01-15T14:30:00.000Z
657
+ *
658
+ * @param {number} seconds
659
+ * @returns {Date}
660
+ */
661
+ export function fromUnix(seconds: number): Date;
662
+ /**
663
+ * Every date from `start` to `end`, both included, one unit apart. Handy
664
+ * for charts, calendars and reports.
665
+ *
666
+ * @example
667
+ * time.range("2024-01-01", "2024-01-07", "day"); // 7 dates
668
+ * time.range(start, end, "month"); // the same day each month
669
+ * time.range(start, end, "minute", { step: 15 }); // every quarter of an hour
670
+ *
671
+ * @param {DateInput} start
672
+ * @param {DateInput} end
673
+ * @param {CalendarUnit} [unit="day"]
674
+ * @param {DateRangeOptions} [options]
675
+ * @returns {Date[]}
676
+ * @throws {RangeError} If the step isn't positive, or the range would hold more than a million dates.
677
+ */
678
+ export function range(start: DateInput, end: DateInput, unit?: CalendarUnit, options?: DateRangeOptions): Date[];
679
+ /**
680
+ * Starts a precise stopwatch.
681
+ *
682
+ * @example
683
+ * const sw = time.stopwatch();
684
+ * await loadConfig(); sw.lap("config");
685
+ * await connectDb(); sw.lap("db");
686
+ * console.log(sw.stop(true)); // "182.4ms"
687
+ *
688
+ * @returns {Stopwatch}
689
+ */
690
+ export function stopwatch(): Stopwatch;
691
+ /**
692
+ * Runs a function, sync or async, and tells you how long it took.
693
+ *
694
+ * @example
695
+ * const { result, duration } = await time.measure(() => db.query(sql));
696
+ * nc.info(`Query took ${duration.toFixed(1)}ms`);
697
+ *
698
+ * @template T
699
+ * @param {() => T} fn
700
+ * @returns {Promise<{ result: Awaited<T>, duration: number }>} `duration` is in milliseconds.
701
+ */
702
+ export function measure<T>(fn: () => T): Promise<{
703
+ result: Awaited<T>;
704
+ duration: number;
705
+ }>;
706
+ /**
707
+ * Runs a task at a regular interval. Unlike `setInterval`, a slow run
708
+ * delays the next one instead of piling up, timing doesn't drift, and
709
+ * errors are caught.
710
+ *
711
+ * @example
712
+ * const job = time.every("5m", syncInventory, { immediate: true, onError: nc.error });
713
+ * job.next(); // when it runs next
714
+ * job.stop();
715
+ *
716
+ * @param {number | string} interval In ms, or a duration like `"30s"`.
717
+ * @param {() => unknown} task Can be async.
718
+ * @param {EveryOptions} [options]
719
+ * @returns {ScheduledTask}
720
+ * @throws {RangeError} If the interval isn't a positive duration.
721
+ */
722
+ export function every(interval: number | string, task: () => unknown, options?: EveryOptions): ScheduledTask;
723
+ /**
724
+ * The next date matching a cron expression, without scheduling anything.
725
+ *
726
+ * Expressions have 5 fields (`minute hour day month weekday`), or 6 with
727
+ * seconds first. You can use `*`, lists (`1,15`), ranges (`1-5`), steps
728
+ * (`*\/15`), names (`jan`, `mon-fri`), `L` for the last day of the month,
729
+ * and `@daily`, `@hourly`, `@weekly`, `@monthly`, `@yearly`. As in classic
730
+ * cron, when both the day and the weekday are set, either one matching is
731
+ * enough.
732
+ *
733
+ * @example
734
+ * time.nextRun("0 9 * * mon-fri"); // next weekday at 09:00
735
+ * time.nextRun("0 0 1 * *", { timeZone: "America/New_York" }); // next 1st of the month
736
+ *
737
+ * @param {string} expression
738
+ * @param {CronOptions} [options]
739
+ * @returns {Date | undefined} `undefined` if nothing matches within 8 years.
740
+ * @throws {SyntaxError} If the expression is invalid.
741
+ */
742
+ export function nextRun(expression: string, options?: CronOptions): Date | undefined;
743
+ /**
744
+ * Runs a task on a cron schedule, in any time zone. Daylight-saving changes
745
+ * are handled: a skipped time is skipped, a repeated one runs once. See
746
+ * `nextRun()` for the syntax.
747
+ *
748
+ * @example
749
+ * const job = time.cron("0 9 * * 1-5", sendDailyReport, { timeZone: "Europe/Paris" });
750
+ * time.cron("*\/15 * * * *", refreshCache, { onError: nc.error });
751
+ * job.stop();
752
+ *
753
+ * @param {string} expression
754
+ * @param {() => unknown} task Can be async.
755
+ * @param {CronOptions} [options]
756
+ * @returns {ScheduledTask}
757
+ * @throws {SyntaxError} If the expression is invalid.
758
+ */
759
+ export function cron(expression: string, task: () => unknown, options?: CronOptions): ScheduledTask;
205
760
  //# sourceMappingURL=Time.d.ts.map