@ix-xs/node-comfort 1.2.0 → 2.0.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 (75) hide show
  1. package/CHANGELOG.md +190 -0
  2. package/README.md +136 -549
  3. package/index.js +80 -35
  4. package/package.json +160 -33
  5. package/src/Arr.js +887 -419
  6. package/src/Async.js +585 -0
  7. package/src/Cache.js +329 -0
  8. package/src/Checker.js +971 -539
  9. package/src/Cli.js +1034 -0
  10. package/src/Color.js +415 -0
  11. package/src/Crypto.js +707 -0
  12. package/src/Emitter.js +289 -0
  13. package/src/Env.js +555 -0
  14. package/src/FS.js +1483 -894
  15. package/src/Func.js +670 -353
  16. package/src/Http.js +538 -0
  17. package/src/Id.js +384 -201
  18. package/src/Logger.js +739 -378
  19. package/src/Num.js +844 -361
  20. package/src/Obj.js +855 -372
  21. package/src/SQLite.js +1308 -1005
  22. package/src/Schema.js +1586 -0
  23. package/src/Str.js +983 -436
  24. package/src/Sys.js +511 -0
  25. package/src/Time.js +1260 -422
  26. package/src/Utils.js +321 -325
  27. package/src/errors.js +292 -0
  28. package/types/index.d.ts +693 -526
  29. package/types/src/Arr.d.ts +455 -145
  30. package/types/src/Arr.d.ts.map +1 -1
  31. package/types/src/Async.d.ts +321 -0
  32. package/types/src/Async.d.ts.map +1 -0
  33. package/types/src/Cache.d.ts +195 -0
  34. package/types/src/Cache.d.ts.map +1 -0
  35. package/types/src/Checker.d.ts +501 -235
  36. package/types/src/Checker.d.ts.map +1 -1
  37. package/types/src/Cli.d.ts +582 -0
  38. package/types/src/Cli.d.ts.map +1 -0
  39. package/types/src/Color.d.ts +294 -0
  40. package/types/src/Color.d.ts.map +1 -0
  41. package/types/src/Crypto.d.ts +534 -0
  42. package/types/src/Crypto.d.ts.map +1 -0
  43. package/types/src/Emitter.d.ts +170 -0
  44. package/types/src/Emitter.d.ts.map +1 -0
  45. package/types/src/Env.d.ts +382 -0
  46. package/types/src/Env.d.ts.map +1 -0
  47. package/types/src/FS.d.ts +686 -238
  48. package/types/src/FS.d.ts.map +1 -1
  49. package/types/src/Func.d.ts +380 -135
  50. package/types/src/Func.d.ts.map +1 -1
  51. package/types/src/Http.d.ts +374 -0
  52. package/types/src/Http.d.ts.map +1 -0
  53. package/types/src/Id.d.ts +149 -68
  54. package/types/src/Id.d.ts.map +1 -1
  55. package/types/src/Logger.d.ts +363 -46
  56. package/types/src/Logger.d.ts.map +1 -1
  57. package/types/src/Num.d.ts +473 -137
  58. package/types/src/Num.d.ts.map +1 -1
  59. package/types/src/Obj.d.ts +390 -104
  60. package/types/src/Obj.d.ts.map +1 -1
  61. package/types/src/SQLite.d.ts +646 -309
  62. package/types/src/SQLite.d.ts.map +1 -1
  63. package/types/src/Schema.d.ts +940 -0
  64. package/types/src/Schema.d.ts.map +1 -0
  65. package/types/src/Str.d.ts +569 -166
  66. package/types/src/Str.d.ts.map +1 -1
  67. package/types/src/Sys.d.ts +269 -0
  68. package/types/src/Sys.d.ts.map +1 -0
  69. package/types/src/Time.d.ts +663 -204
  70. package/types/src/Time.d.ts.map +1 -1
  71. package/types/src/Utils.d.ts +154 -102
  72. package/types/src/Utils.d.ts.map +1 -1
  73. package/types/src/errors.d.ts +292 -0
  74. package/types/src/errors.d.ts.map +1 -0
  75. package/types/index.d.ts.map +0 -1
@@ -1,205 +1,664 @@
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?: "always" | "auto" | 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 | 2 | 1 | 3 | 4 | 5 | 6 | undefined;
98
+ };
99
+ /**
100
+ * Time zone setting.
101
+ */
102
+ export type TimeZoneOptions = {
103
+ /**
104
+ * Zone used to decide which day it is. Defaults to the global zone, then the system's.
105
+ */
106
+ timeZone?: string | undefined;
107
+ };
108
+ /**
109
+ * Options for `every()`.
110
+ */
111
+ export type EveryOptions = {
112
+ /**
113
+ * Also run once right away.
114
+ */
115
+ immediate?: boolean | undefined;
116
+ /**
117
+ * Let the process exit even if the schedule is active.
118
+ */
119
+ unref?: boolean | undefined;
120
+ /**
121
+ * Called when the task fails. Without it, errors surface as unhandled errors.
122
+ */
123
+ onError?: ((error: unknown) => void) | undefined;
124
+ };
125
+ /**
126
+ * Options for `cron()` and `nextRun()`.
127
+ */
128
+ export type CronOptions = {
129
+ /**
130
+ * Zone in which the expression is read, like `"Europe/Paris"`. Defaults to the global zone, then the system's.
131
+ */
132
+ timeZone?: string | undefined;
133
+ /**
134
+ * `nextRun()` only: search after this date. Defaults to now.
135
+ */
136
+ from?: DateInput | undefined;
137
+ /**
138
+ * Let the process exit even if the schedule is active.
139
+ */
140
+ unref?: boolean | undefined;
141
+ /**
142
+ * Start a run even if the previous one hasn't finished. By default it's skipped.
143
+ */
144
+ overlap?: boolean | undefined;
145
+ /**
146
+ * Called when the task fails.
147
+ */
148
+ onError?: ((error: unknown) => void) | undefined;
149
+ };
150
+ /**
151
+ * A schedule returned by `every()` and `cron()`.
152
+ */
153
+ export type ScheduledTask = {
154
+ /**
155
+ * Stops the schedule. A run in progress finishes.
156
+ */
157
+ stop: () => void;
158
+ /**
159
+ * When the next run is planned.
160
+ */
161
+ next: () => Date | undefined;
162
+ /**
163
+ * Whether a run is in progress.
164
+ */
165
+ isRunning: () => boolean;
166
+ /**
167
+ * How many runs have started.
168
+ */
169
+ runs: () => number;
170
+ };
171
+ /**
172
+ * Returned by `stopwatch()`.
173
+ */
174
+ export type Stopwatch = {
175
+ /**
176
+ * Milliseconds since the start.
177
+ */
178
+ elapsed: () => number;
179
+ /**
180
+ * The elapsed time; `stop(true)` formats it, like `"1.5s"`.
181
+ */
182
+ stop: (format?: boolean) => number | string;
183
+ /**
184
+ * Records a lap and returns the ms since the previous one.
185
+ */
186
+ lap: (label?: string) => number;
187
+ /**
188
+ * Every lap so far.
189
+ */
190
+ laps: () => Array<{
191
+ label: string;
192
+ ms: number;
193
+ total: number;
194
+ }>;
195
+ /**
196
+ * Starts over.
197
+ */
198
+ reset: () => void;
199
+ };
200
+ /**
201
+ * Parsed cron fields.
202
+ */
203
+ export type CronSpec = {
204
+ seconds: Set<number>;
205
+ minutes: Set<number>;
206
+ hours: Set<number>;
207
+ days: Set<number>;
208
+ months: Set<number>;
209
+ weekdays: Set<number>;
210
+ anyDay: boolean;
211
+ anyWeekday: boolean;
212
+ };
213
+ /**
214
+ * Sets the default language for `relative`, `calendar`, `formatDuration`
215
+ * and `format`.
216
+ *
217
+ * @example
218
+ * time.setLocale("fr");
219
+ * time.relative(Date.now() - 60e3); // "il y a 1 minute"
220
+ *
221
+ * @param {string} locale Like `"fr"`, `"en-GB"`, `"pt-BR"` or `"ja"`.
222
+ * @returns {typeof import("./Time")} The module, so calls chain.
223
+ */
224
+ export function setLocale(locale: string): typeof import("./Time");
225
+ /**
226
+ * Sets the default time zone for `format`, `calendar`, `isSameDay`,
227
+ * `cron`... Pass `undefined` to go back to the system zone.
228
+ *
229
+ * @example
230
+ * time.setTimezone("Europe/Paris");
231
+ *
232
+ * @param {string | undefined} timeZone Like `"America/New_York"`.
233
+ * @returns {typeof import("./Time")} The module, so calls chain.
234
+ * @throws {RangeError} If the zone doesn't exist.
235
+ */
236
+ export function setTimezone(timeZone: string | undefined): typeof import("./Time");
237
+ /**
238
+ * The current defaults.
239
+ *
240
+ * @example
241
+ * time.getConfig(); // { locale: "en", timeZone: undefined }
242
+ *
243
+ * @returns {{ locale: string, timeZone: string | undefined }}
244
+ */
245
+ export function getConfig(): {
246
+ locale: string;
247
+ timeZone: string | undefined;
248
+ };
249
+ /**
250
+ * Every time zone Node knows, sorted.
251
+ *
252
+ * @example
253
+ * time.timezones().includes("Europe/Paris"); // true
254
+ *
255
+ * @returns {string[]}
256
+ */
257
+ export function timezones(): string[];
258
+ /**
259
+ * A zone's UTC offset at a given date, in minutes. Daylight saving included.
260
+ *
261
+ * @example
262
+ * time.offset("Europe/Paris", "2024-07-01"); // 120
263
+ * time.offset("Europe/Paris", "2024-01-01"); // 60
264
+ *
265
+ * @param {string} [timeZone] Defaults to the global zone, then the system's.
266
+ * @param {DateInput} [date] Defaults to now.
267
+ * @returns {number}
268
+ */
269
+ export function offset(timeZone?: string, date?: DateInput): number;
270
+ /**
271
+ * Turns a duration into milliseconds. Understands `"1h30m"`, `"2 days"`,
272
+ * `"500ms"`, clock times like `"01:30:00"`, ISO durations like `"PT1H30M"`,
273
+ * and plain numbers. Months and years are refused because their length
274
+ * varies; use `add()` for those.
275
+ *
276
+ * @example
277
+ * time.parseDuration("1h30m"); // 5400000
278
+ * time.parseDuration("01:30:00"); // 5400000
279
+ * time.parseDuration("banana"); // null
280
+ *
281
+ * @param {string | number} input
282
+ * @returns {number | null} `null` when it isn't a duration.
283
+ */
284
+ export function parseDuration(input: string | number): number | null;
285
+ /**
286
+ * Writes a duration for humans.
287
+ *
288
+ * @example
289
+ * time.formatDuration(5_400_000); // "1h 30m"
290
+ * time.formatDuration(90_000, { long: true }); // "1 minute 30 seconds"
291
+ * time.formatDuration(90_000, { long: true, locale: "fr" }); // "1 minute 30 secondes"
292
+ * time.formatDuration(93_784_000, { units: 2 }); // "1d 2h"
293
+ * time.formatDuration(5_405_000, { clock: true }); // "01:30:05"
294
+ *
295
+ * @param {number} ms
296
+ * @param {FormatDurationOptions} [options]
297
+ * @returns {string}
298
+ */
299
+ export function formatDuration(ms: number, options?: FormatDurationOptions): string;
300
+ /**
301
+ * Describes a date relative to now: `"3 hours ago"`, `"in 2 days"`, in
302
+ * any language.
303
+ *
304
+ * @example
305
+ * time.relative(Date.now() - 3_600_000); // "1 hour ago"
306
+ * time.relative(Date.now() - 86_400_000, undefined, { numeric: "auto" }); // "yesterday"
307
+ * time.relative(Date.now() - 3_600_000, undefined, { locale: "fr" }); // "il y a 1 heure"
308
+ *
309
+ * @param {DateInput} date
310
+ * @param {DateInput} [from] Compare with this date instead of now.
311
+ * @param {RelativeOptions} [options]
312
+ * @returns {string}
313
+ */
314
+ export function relative(date: DateInput, from?: DateInput, options?: RelativeOptions): string;
315
+ /**
316
+ * Describes a date like a chat app does: `"Today at 2:30 PM"`,
317
+ * `"Yesterday at 9:05 AM"`, a weekday for the coming days, and the full date
318
+ * beyond that.
319
+ *
320
+ * @example
321
+ * time.calendar(new Date()); // "Today at 2:30 PM"
322
+ * time.calendar(yesterday, { locale: "fr" }); // "Hier à 09:05"
323
+ *
324
+ * @param {DateInput} date
325
+ * @param {CalendarOptions} [options]
326
+ * @returns {string}
327
+ */
328
+ export function calendar(date: DateInput, options?: CalendarOptions): string;
329
+ /**
330
+ * Formats a date with tokens, in any language and time zone.
331
+ *
332
+ * | Token | Output | | Token | Output |
333
+ * | --- | --- | --- | --- | --- |
334
+ * | `YYYY` / `YY` | 2024 / 24 | | `HH` / `H` | 09 / 9 (24h) |
335
+ * | `Q` | quarter, 1-4 | | `hh` / `h` | 09 / 9 (12h) |
336
+ * | `MMMM` / `MMM` | January / Jan | | `mm` / `m` | 05 / 5 |
337
+ * | `MM` / `M` | 01 / 1 | | `ss` / `s` | 07 / 7 |
338
+ * | `DD` / `D` | 05 / 5 | | `SSS` | 042 (ms) |
339
+ * | `dddd` / `ddd` | Monday / Mon | | `A` / `a` | PM / pm |
340
+ * | `d` | weekday, 0 is Sunday | | `Z` / `ZZ` | +02:00 / +0200 |
341
+ * | `W` / `WW` | ISO week | | `X` / `x` | Unix seconds / ms |
342
+ *
343
+ * Text in brackets stays as is: `"[Today is] dddd"`.
344
+ *
345
+ * @example
346
+ * time.format(new Date(), "YYYY-MM-DD HH:mm:ss"); // "2024-01-15 14:30:05"
347
+ * time.format(Date.now(), "dddd D MMMM YYYY", { locale: "fr" }); // "lundi 15 janvier 2024"
348
+ * time.format(Date.now(), "HH:mm Z", { timeZone: "Asia/Tokyo" }); // "22:30 +09:00"
349
+ *
350
+ * @param {DateInput} date
351
+ * @param {string} [pattern="YYYY-MM-DD HH:mm:ss"]
352
+ * @param {FormatDateOptions} [options]
353
+ * @returns {string} Or `"Invalid Date"`.
354
+ */
355
+ export function format(date: DateInput, pattern?: string, options?: FormatDateOptions): string;
356
+ /**
357
+ * The date as `YYYY-MM-DD`, the format `<input type="date">` and most APIs
358
+ * expect.
359
+ *
360
+ * @example
361
+ * time.toISODate(new Date(2024, 0, 5)); // "2024-01-05"
362
+ *
363
+ * @param {DateInput} [date] Defaults to now.
364
+ * @param {TimeZoneOptions} [options]
365
+ * @returns {string}
366
+ */
367
+ export function toISODate(date?: DateInput, options?: TimeZoneOptions): string;
368
+ /**
369
+ * Adds time to a date and returns a new `Date`. Months and years follow the
370
+ * calendar: January 31 plus one month is the end of February.
371
+ *
372
+ * @example
373
+ * time.add(new Date(), "2h30m");
374
+ * time.add(new Date(), 3, "days");
375
+ * time.add(new Date(2024, 0, 31), 1, "month"); // 2024-02-29
376
+ *
377
+ * @param {DateInput} date
378
+ * @param {number | string} amount A number with `unit`, or a duration like `"1h30m"`.
379
+ * @param {CalendarUnit} [unit="ms"]
380
+ * @returns {Date}
381
+ */
382
+ export function add(date: DateInput, amount: number | string, unit?: CalendarUnit): Date;
383
+ /**
384
+ * Subtracts time from a date and returns a new `Date`.
385
+ *
386
+ * @example
387
+ * time.subtract(new Date(), 7, "days");
388
+ * time.subtract(new Date(), "90m");
389
+ *
390
+ * @param {DateInput} date
391
+ * @param {number | string} amount
392
+ * @param {CalendarUnit} [unit="ms"]
393
+ * @returns {Date}
394
+ */
395
+ export function subtract(date: DateInput, amount: number | string, unit?: CalendarUnit): Date;
396
+ /**
397
+ * `a - b` in a unit. Fixed units can give fractions; months and years are
398
+ * whole calendar months and years.
399
+ *
400
+ * @example
401
+ * time.diff("2024-01-03", "2024-01-01", "days"); // 2
402
+ * time.diff("2024-03-15", "2024-01-20", "months"); // 1
403
+ *
404
+ * @param {DateInput} a
405
+ * @param {DateInput} b
406
+ * @param {CalendarUnit} [unit="ms"]
407
+ * @returns {number}
408
+ */
409
+ export function diff(a: DateInput, b: DateInput, unit?: CalendarUnit): number;
410
+ /**
411
+ * A new `Date` at the start of the day, week, month... in local time.
412
+ *
413
+ * @example
414
+ * time.startOf(new Date(), "day"); // today at 00:00
415
+ * time.startOf(new Date(), "week"); // Monday at 00:00
416
+ * time.startOf(new Date(), "week", { weekStartsOn: 0 }); // Sunday at 00:00
417
+ *
418
+ * @param {DateInput} date
419
+ * @param {StartOfUnit} unit
420
+ * @param {WeekOptions} [options]
421
+ * @returns {Date}
422
+ */
423
+ export function startOf(date: DateInput, unit: StartOfUnit, options?: WeekOptions): Date;
424
+ /**
425
+ * A new `Date` at the last millisecond of the day, week, month...
426
+ *
427
+ * @example
428
+ * time.endOf(new Date(), "month"); // last day of the month, 23:59:59.999
429
+ *
430
+ * @param {DateInput} date
431
+ * @param {StartOfUnit} unit
432
+ * @param {WeekOptions} [options]
433
+ * @returns {Date}
434
+ */
435
+ export function endOf(date: DateInput, unit: StartOfUnit, options?: WeekOptions): Date;
436
+ /**
437
+ * Is `a` before `b`?
438
+ *
439
+ * @param {DateInput} a
440
+ * @param {DateInput} b
441
+ * @returns {boolean}
442
+ */
443
+ export function isBefore(a: DateInput, b: DateInput): boolean;
444
+ /**
445
+ * Is `a` after `b`?
446
+ *
447
+ * @example
448
+ * if (time.isAfter(expiresAt, Date.now())) grantAccess();
449
+ *
450
+ * @param {DateInput} a
451
+ * @param {DateInput} b
452
+ * @returns {boolean}
453
+ */
454
+ export function isAfter(a: DateInput, b: DateInput): boolean;
455
+ /**
456
+ * Is the date between `start` and `end`, both included?
457
+ *
458
+ * @param {DateInput} date
459
+ * @param {DateInput} start
460
+ * @param {DateInput} end
461
+ * @returns {boolean}
462
+ */
463
+ export function isBetween(date: DateInput, start: DateInput, end: DateInput): boolean;
464
+ /**
465
+ * Are both dates on the same calendar day?
466
+ *
467
+ * @example
468
+ * time.isSameDay(a, b); // local time
469
+ * time.isSameDay(a, b, { timeZone: "Asia/Tokyo" }); // as seen in Tokyo
470
+ *
471
+ * @param {DateInput} a
472
+ * @param {DateInput} b
473
+ * @param {TimeZoneOptions} [options]
474
+ * @returns {boolean}
475
+ */
476
+ export function isSameDay(a: DateInput, b: DateInput, options?: TimeZoneOptions): boolean;
477
+ /**
478
+ * Is the date today?
479
+ *
480
+ * @param {DateInput} date
481
+ * @param {TimeZoneOptions} [options]
482
+ * @returns {boolean}
483
+ */
484
+ export function isToday(date: DateInput, options?: TimeZoneOptions): boolean;
485
+ /**
486
+ * Was the date yesterday?
487
+ *
488
+ * @param {DateInput} date
489
+ * @param {TimeZoneOptions} [options]
490
+ * @returns {boolean}
491
+ */
492
+ export function isYesterday(date: DateInput, options?: TimeZoneOptions): boolean;
493
+ /**
494
+ * Is the date tomorrow?
495
+ *
496
+ * @param {DateInput} date
497
+ * @param {TimeZoneOptions} [options]
498
+ * @returns {boolean}
499
+ */
500
+ export function isTomorrow(date: DateInput, options?: TimeZoneOptions): boolean;
501
+ /**
502
+ * Is it a Saturday or a Sunday?
503
+ *
504
+ * @param {DateInput} date
505
+ * @param {TimeZoneOptions} [options]
506
+ * @returns {boolean}
507
+ */
508
+ export function isWeekend(date: DateInput, options?: TimeZoneOptions): boolean;
509
+ /**
510
+ * Is it a leap year?
511
+ *
512
+ * @example
513
+ * time.isLeapYear(2024); // true
514
+ * time.isLeapYear(1900); // false
515
+ *
516
+ * @param {number | DateInput} yearOrDate A year (1000 to 9999) or a date.
517
+ * @returns {boolean}
518
+ */
519
+ export function isLeapYear(yearOrDate: number | DateInput): boolean;
520
+ /**
521
+ * Is it a real date, or something that parses to one?
522
+ *
523
+ * @example
524
+ * time.isValid("2024-02-29"); // true
525
+ * time.isValid("2024-02-30"); // false
526
+ *
527
+ * @param {unknown} value
528
+ * @returns {boolean}
529
+ */
530
+ export function isValid(value: unknown): boolean;
531
+ /**
532
+ * Number of days in a month.
533
+ *
534
+ * @example
535
+ * time.daysInMonth(new Date(2024, 1)); // 29
536
+ * time.daysInMonth(2023, 2); // 28
537
+ *
538
+ * @param {DateInput | number} dateOrYear A date, or a year when you pass `month`.
539
+ * @param {number} [month] 1 to 12.
540
+ * @returns {number}
541
+ */
542
+ export function daysInMonth(dateOrYear: DateInput | number, month?: number): number;
543
+ /**
544
+ * Day of the year, from 1 to 366.
545
+ *
546
+ * @param {DateInput} [date] Defaults to now.
547
+ * @returns {number}
548
+ */
549
+ export function dayOfYear(date?: DateInput): number;
550
+ /**
551
+ * ISO week number, from 1 to 53. Weeks start on Monday, and week 1 holds
552
+ * the year's first Thursday.
553
+ *
554
+ * @example
555
+ * time.weekOfYear(new Date(2021, 0, 3)); // 53, the last week of 2020
556
+ *
557
+ * @param {DateInput} [date] Defaults to now.
558
+ * @param {{ utc?: boolean }} [options] Read the date in UTC.
559
+ * @returns {number}
560
+ */
561
+ export function weekOfYear(date?: DateInput, options?: {
562
+ utc?: boolean;
563
+ }): number;
564
+ /**
565
+ * The earliest date.
566
+ *
567
+ * @param {Iterable<DateInput>} dates
568
+ * @returns {Date | undefined}
569
+ */
570
+ export function min(dates: Iterable<DateInput>): Date | undefined;
571
+ /**
572
+ * The latest date.
573
+ *
574
+ * @param {Iterable<DateInput>} dates
575
+ * @returns {Date | undefined}
576
+ */
577
+ export function max(dates: Iterable<DateInput>): Date | undefined;
578
+ /**
579
+ * The current Unix timestamp, in seconds.
580
+ *
581
+ * @returns {number}
582
+ */
583
+ export function unix(): number;
584
+ /**
585
+ * Starts a precise stopwatch.
586
+ *
587
+ * @example
588
+ * const sw = time.stopwatch();
589
+ * await loadConfig(); sw.lap("config");
590
+ * await connectDb(); sw.lap("db");
591
+ * console.log(sw.stop(true)); // "182.4ms"
592
+ *
593
+ * @returns {Stopwatch}
594
+ */
595
+ export function stopwatch(): Stopwatch;
596
+ /**
597
+ * Runs a function, sync or async, and tells you how long it took.
598
+ *
599
+ * @example
600
+ * const { result, duration } = await time.measure(() => db.query(sql));
601
+ * nc.info(`Query took ${duration.toFixed(1)}ms`);
602
+ *
603
+ * @template T
604
+ * @param {() => T} fn
605
+ * @returns {Promise<{ result: Awaited<T>, duration: number }>} `duration` is in milliseconds.
606
+ */
607
+ export function measure<T>(fn: () => T): Promise<{
608
+ result: Awaited<T>;
609
+ duration: number;
610
+ }>;
611
+ /**
612
+ * Runs a task at a regular interval. Unlike `setInterval`, a slow run
613
+ * delays the next one instead of piling up, timing doesn't drift, and
614
+ * errors are caught.
615
+ *
616
+ * @example
617
+ * const job = time.every("5m", syncInventory, { immediate: true, onError: nc.error });
618
+ * job.next(); // when it runs next
619
+ * job.stop();
620
+ *
621
+ * @param {number | string} interval In ms, or a duration like `"30s"`.
622
+ * @param {() => unknown} task Can be async.
623
+ * @param {EveryOptions} [options]
624
+ * @returns {ScheduledTask}
625
+ * @throws {RangeError} If the interval isn't a positive duration.
626
+ */
627
+ export function every(interval: number | string, task: () => unknown, options?: EveryOptions): ScheduledTask;
628
+ /**
629
+ * The next date matching a cron expression, without scheduling anything.
630
+ *
631
+ * Expressions have 5 fields (`minute hour day month weekday`), or 6 with
632
+ * seconds first. You can use `*`, lists (`1,15`), ranges (`1-5`), steps
633
+ * (`*\/15`), names (`jan`, `mon-fri`) and `@daily`, `@hourly`, `@weekly`,
634
+ * `@monthly`, `@yearly`. As in classic cron, when both the day and the
635
+ * weekday are set, either one matching is enough.
636
+ *
637
+ * @example
638
+ * time.nextRun("0 9 * * mon-fri"); // next weekday at 09:00
639
+ * time.nextRun("0 0 1 * *", { timeZone: "America/New_York" }); // next 1st of the month
640
+ *
641
+ * @param {string} expression
642
+ * @param {CronOptions} [options]
643
+ * @returns {Date | undefined} `undefined` if nothing matches within 8 years.
644
+ * @throws {SyntaxError} If the expression is invalid.
645
+ */
646
+ export function nextRun(expression: string, options?: CronOptions): Date | undefined;
647
+ /**
648
+ * Runs a task on a cron schedule, in any time zone. Daylight-saving changes
649
+ * are handled: a skipped time is skipped, a repeated one runs once. See
650
+ * `nextRun()` for the syntax.
651
+ *
652
+ * @example
653
+ * const job = time.cron("0 9 * * 1-5", sendDailyReport, { timeZone: "Europe/Paris" });
654
+ * time.cron("*\/15 * * * *", refreshCache, { onError: nc.error });
655
+ * job.stop();
656
+ *
657
+ * @param {string} expression
658
+ * @param {() => unknown} task Can be async.
659
+ * @param {CronOptions} [options]
660
+ * @returns {ScheduledTask}
661
+ * @throws {SyntaxError} If the expression is invalid.
662
+ */
663
+ export function cron(expression: string, task: () => unknown, options?: CronOptions): ScheduledTask;
205
664
  //# sourceMappingURL=Time.d.ts.map