@catbee/utils 2.0.0-next.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.
- package/LICENSE +1 -1
- package/README.md +102 -52
- package/array/index.cjs +215 -74
- package/array/index.d.ts +345 -2
- package/array/index.mjs +201 -74
- package/async/index.cjs +116 -39
- package/async/index.d.ts +292 -2
- package/async/index.mjs +116 -40
- package/cache/index.cjs +2 -2
- package/cache/index.d.ts +156 -2
- package/cache/index.mjs +3 -3
- package/config/index.cjs +80 -66
- package/config/index.d.ts +65 -3
- package/config/index.mjs +77 -65
- package/context-store/index.cjs +2 -3
- package/context-store/index.d.ts +193 -2
- package/context-store/index.mjs +2 -3
- package/crypto/index.cjs +55 -5
- package/crypto/index.d.ts +225 -2
- package/crypto/index.mjs +52 -7
- package/date/index.cjs +676 -2
- package/date/index.d.ts +676 -2
- package/date/index.mjs +665 -3
- package/decorator/index.cjs +2172 -0
- package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
- package/decorator/index.mjs +2131 -0
- package/{dir → directory}/index.cjs +5 -4
- package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
- package/{dir → directory}/index.mjs +5 -4
- package/env/index.cjs +100 -68
- package/env/index.d.ts +391 -2
- package/env/index.mjs +100 -68
- package/exception/index.cjs +1 -1
- package/exception/index.d.ts +233 -2
- package/exception/index.mjs +1 -1
- package/fs/index.cjs +71 -37
- package/fs/index.d.ts +206 -2
- package/fs/index.mjs +65 -35
- package/http-status-codes/index.cjs +1 -1
- package/http-status-codes/index.d.ts +268 -2
- package/http-status-codes/index.mjs +1 -1
- package/id/index.cjs +1 -1
- package/id/index.d.ts +38 -2
- package/id/index.mjs +1 -1
- package/index.cjs +13 -13
- package/index.d.ts +5 -5
- package/index.mjs +5 -5
- package/logger/index.cjs +13 -15
- package/logger/index.d.ts +190 -2
- package/logger/index.mjs +14 -16
- package/middleware/index.cjs +1 -1
- package/middleware/index.d.ts +104 -2
- package/middleware/index.mjs +1 -1
- package/object/index.cjs +379 -0
- package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
- package/object/index.mjs +360 -0
- package/package.json +41 -23
- package/performance/index.cjs +4 -4
- package/performance/index.d.ts +139 -2
- package/performance/index.mjs +4 -4
- package/request/index.cjs +37 -25
- package/request/index.d.ts +242 -3
- package/request/index.mjs +37 -25
- package/response/index.cjs +1 -1
- package/response/index.d.ts +319 -3
- package/response/index.mjs +1 -1
- package/server/index.cjs +249 -146
- package/server/index.d.ts +866 -5
- package/server/index.mjs +248 -144
- package/stream/index.cjs +1 -1
- package/stream/index.d.ts +91 -2
- package/stream/index.mjs +1 -1
- package/string/index.cjs +34 -1
- package/string/index.d.ts +146 -2
- package/string/index.mjs +31 -2
- package/type/index.cjs +19 -2
- package/type/index.d.ts +144 -2
- package/type/index.mjs +17 -3
- package/types/index.cjs +1 -1
- package/types/index.d.ts +775 -5
- package/types/index.mjs +1 -1
- package/url/index.cjs +63 -5
- package/url/index.d.ts +200 -2
- package/url/index.mjs +59 -6
- package/{validate → validation}/index.cjs +91 -44
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
- package/{validate → validation}/index.mjs +87 -44
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/index.cjs +0 -913
- package/decorators/index.d.ts +0 -25
- package/decorators/index.mjs +0 -872
- package/dir/index.d.ts +0 -25
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/index.cjs +0 -317
- package/obj/index.d.ts +0 -25
- package/obj/index.mjs +0 -301
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/date/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -88,7 +88,7 @@ function parseDate(input, fallback) {
|
|
|
88
88
|
}
|
|
89
89
|
try {
|
|
90
90
|
const date = new Date(input);
|
|
91
|
-
if (isNaN(date.getTime())) {
|
|
91
|
+
if (Number.isNaN(date.getTime())) {
|
|
92
92
|
return fallback || null;
|
|
93
93
|
}
|
|
94
94
|
return date;
|
|
@@ -281,15 +281,689 @@ function formatDuration(ms) {
|
|
|
281
281
|
return parts.join(" ");
|
|
282
282
|
}
|
|
283
283
|
__name(formatDuration, "formatDuration");
|
|
284
|
+
function parseDuration(value) {
|
|
285
|
+
const MAX_SAFE = Number.MAX_SAFE_INTEGER;
|
|
286
|
+
if (typeof value === "number" && Number.isFinite(value)) return Math.min(value, MAX_SAFE);
|
|
287
|
+
if (typeof value !== "string" || !value.trim()) return 0;
|
|
288
|
+
if (/^\d+$/.test(value)) return Math.min(Number(value), MAX_SAFE);
|
|
289
|
+
const val = value.replace(/\s+/g, "").toLowerCase();
|
|
290
|
+
const unitMap = {
|
|
291
|
+
y: 31536e6,
|
|
292
|
+
w: 6048e5,
|
|
293
|
+
d: 864e5,
|
|
294
|
+
h: 36e5,
|
|
295
|
+
m: 6e4,
|
|
296
|
+
s: 1e3,
|
|
297
|
+
ms: 1
|
|
298
|
+
};
|
|
299
|
+
const unitKeys = Object.keys(unitMap).sort((a, b) => b.length - a.length);
|
|
300
|
+
let total = 0;
|
|
301
|
+
let i = 0;
|
|
302
|
+
while (i < val.length) {
|
|
303
|
+
const start = i;
|
|
304
|
+
while (i < val.length && /\d/.test(val[i])) i++;
|
|
305
|
+
if (start === i) {
|
|
306
|
+
throw new Error(`Invalid duration at offset ${i} in "${value}"`);
|
|
307
|
+
}
|
|
308
|
+
const num = Number(val.slice(start, i));
|
|
309
|
+
if (!Number.isSafeInteger(num)) throw new Error(`Invalid number at offset ${start} in "${value}"`);
|
|
310
|
+
const unit = unitKeys.find((u) => val.startsWith(u, i));
|
|
311
|
+
if (!unit) throw new Error(`Unknown unit at offset ${i} in "${value}"`);
|
|
312
|
+
const add = num * unitMap[unit];
|
|
313
|
+
total = Math.min(total + add, MAX_SAFE);
|
|
314
|
+
i += unit.length;
|
|
315
|
+
}
|
|
316
|
+
return total;
|
|
317
|
+
}
|
|
318
|
+
__name(parseDuration, "parseDuration");
|
|
319
|
+
function getDateFromDuration(input) {
|
|
320
|
+
const ms = parseDuration(input);
|
|
321
|
+
if (ms <= 0) {
|
|
322
|
+
throw new Error(`Invalid duration: "${input}". Duration must be a positive value.`);
|
|
323
|
+
}
|
|
324
|
+
return new Date(Date.now() + ms);
|
|
325
|
+
}
|
|
326
|
+
__name(getDateFromDuration, "getDateFromDuration");
|
|
327
|
+
function isWeekend(date) {
|
|
328
|
+
const d = date instanceof Date ? date : new Date(date);
|
|
329
|
+
const day = d.getDay();
|
|
330
|
+
return day === 0 || day === 6;
|
|
331
|
+
}
|
|
332
|
+
__name(isWeekend, "isWeekend");
|
|
333
|
+
function isToday(date) {
|
|
334
|
+
const d = date instanceof Date ? date : new Date(date);
|
|
335
|
+
const today = /* @__PURE__ */ new Date();
|
|
336
|
+
return d.getFullYear() === today.getFullYear() && d.getMonth() === today.getMonth() && d.getDate() === today.getDate();
|
|
337
|
+
}
|
|
338
|
+
__name(isToday, "isToday");
|
|
339
|
+
function isFuture(date) {
|
|
340
|
+
const d = date instanceof Date ? date : new Date(date);
|
|
341
|
+
return d.getTime() > Date.now();
|
|
342
|
+
}
|
|
343
|
+
__name(isFuture, "isFuture");
|
|
344
|
+
function isPast(date) {
|
|
345
|
+
const d = date instanceof Date ? date : new Date(date);
|
|
346
|
+
return d.getTime() < Date.now();
|
|
347
|
+
}
|
|
348
|
+
__name(isPast, "isPast");
|
|
349
|
+
function addDays(date, days) {
|
|
350
|
+
const d = new Date(date instanceof Date ? date : new Date(date));
|
|
351
|
+
d.setDate(d.getDate() + days);
|
|
352
|
+
return d;
|
|
353
|
+
}
|
|
354
|
+
__name(addDays, "addDays");
|
|
355
|
+
function addMonths(date, months) {
|
|
356
|
+
const d = new Date(date instanceof Date ? date : new Date(date));
|
|
357
|
+
d.setMonth(d.getMonth() + months);
|
|
358
|
+
return d;
|
|
359
|
+
}
|
|
360
|
+
__name(addMonths, "addMonths");
|
|
361
|
+
function addYears(date, years) {
|
|
362
|
+
const d = new Date(date instanceof Date ? date : new Date(date));
|
|
363
|
+
d.setFullYear(d.getFullYear() + years);
|
|
364
|
+
return d;
|
|
365
|
+
}
|
|
366
|
+
__name(addYears, "addYears");
|
|
367
|
+
function quarterOf(date) {
|
|
368
|
+
const d = date instanceof Date ? date : new Date(date);
|
|
369
|
+
return Math.floor(d.getMonth() / 3) + 1;
|
|
370
|
+
}
|
|
371
|
+
__name(quarterOf, "quarterOf");
|
|
372
|
+
function weekOfYear(date) {
|
|
373
|
+
const d = new Date(date instanceof Date ? date : new Date(date));
|
|
374
|
+
d.setHours(0, 0, 0, 0);
|
|
375
|
+
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
|
|
376
|
+
const yearStart = new Date(d.getFullYear(), 0, 1);
|
|
377
|
+
return Math.ceil(((d.getTime() - yearStart.getTime()) / 864e5 + 1) / 7);
|
|
378
|
+
}
|
|
379
|
+
__name(weekOfYear, "weekOfYear");
|
|
380
|
+
|
|
381
|
+
// src/date/date.builder.ts
|
|
382
|
+
var DateBuilder = class _DateBuilder {
|
|
383
|
+
static {
|
|
384
|
+
__name(this, "DateBuilder");
|
|
385
|
+
}
|
|
386
|
+
date;
|
|
387
|
+
/**
|
|
388
|
+
* Creates a new DateBuilder instance.
|
|
389
|
+
* @param date - Initial date (default: current date/time)
|
|
390
|
+
*/
|
|
391
|
+
constructor(date) {
|
|
392
|
+
if (date === void 0) {
|
|
393
|
+
this.date = /* @__PURE__ */ new Date();
|
|
394
|
+
} else if (date instanceof Date) {
|
|
395
|
+
this.date = new Date(date);
|
|
396
|
+
} else if (typeof date === "number") {
|
|
397
|
+
this.date = new Date(date);
|
|
398
|
+
} else {
|
|
399
|
+
this.date = new Date(date);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Create a DateBuilder from an existing date.
|
|
404
|
+
* @param date - Date to build from
|
|
405
|
+
*/
|
|
406
|
+
static from(date) {
|
|
407
|
+
return new _DateBuilder(date);
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Create a DateBuilder for the current moment.
|
|
411
|
+
*/
|
|
412
|
+
static now() {
|
|
413
|
+
return new _DateBuilder();
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Create a DateBuilder for a specific date.
|
|
417
|
+
* @param year - Year
|
|
418
|
+
* @param month - Month (1-12, not 0-11)
|
|
419
|
+
* @param day - Day of month
|
|
420
|
+
* @param hour - Hour (default: 0)
|
|
421
|
+
* @param minute - Minute (default: 0)
|
|
422
|
+
* @param second - Second (default: 0)
|
|
423
|
+
* @param millisecond - Millisecond (default: 0)
|
|
424
|
+
*/
|
|
425
|
+
static of(year, month, day, hour = 0, minute = 0, second = 0, millisecond = 0) {
|
|
426
|
+
const d = new Date(year, month - 1, day, hour, minute, second, millisecond);
|
|
427
|
+
return new _DateBuilder(d);
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Parse a date string into a DateBuilder.
|
|
431
|
+
* @param dateString - Date string to parse
|
|
432
|
+
*/
|
|
433
|
+
static parse(dateString) {
|
|
434
|
+
return new _DateBuilder(new Date(dateString));
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Create a DateBuilder from a duration string (e.g., "5m", "2h", "1d" from now).
|
|
438
|
+
* @param duration - Duration string
|
|
439
|
+
*/
|
|
440
|
+
static fromDuration(duration) {
|
|
441
|
+
return new _DateBuilder(getDateFromDuration(duration));
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Clone this DateBuilder instance.
|
|
445
|
+
*/
|
|
446
|
+
clone() {
|
|
447
|
+
return new _DateBuilder(this.date);
|
|
448
|
+
}
|
|
449
|
+
// ========== Setters (return new instance) ==========
|
|
450
|
+
/**
|
|
451
|
+
* Set the year.
|
|
452
|
+
* @param year - Year to set
|
|
453
|
+
*/
|
|
454
|
+
year(year) {
|
|
455
|
+
const newDate = new Date(this.date);
|
|
456
|
+
newDate.setFullYear(year);
|
|
457
|
+
return new _DateBuilder(newDate);
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Set the month (1-12, not 0-11).
|
|
461
|
+
* @param month - Month to set (1-12)
|
|
462
|
+
*/
|
|
463
|
+
month(month) {
|
|
464
|
+
const newDate = new Date(this.date);
|
|
465
|
+
newDate.setMonth(month - 1);
|
|
466
|
+
return new _DateBuilder(newDate);
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Set the day of the month.
|
|
470
|
+
* @param day - Day to set
|
|
471
|
+
*/
|
|
472
|
+
day(day) {
|
|
473
|
+
const newDate = new Date(this.date);
|
|
474
|
+
newDate.setDate(day);
|
|
475
|
+
return new _DateBuilder(newDate);
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Set the hour.
|
|
479
|
+
* @param hour - Hour to set
|
|
480
|
+
*/
|
|
481
|
+
hour(hour) {
|
|
482
|
+
const newDate = new Date(this.date);
|
|
483
|
+
newDate.setHours(hour);
|
|
484
|
+
return new _DateBuilder(newDate);
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Set the minute.
|
|
488
|
+
* @param minute - Minute to set
|
|
489
|
+
*/
|
|
490
|
+
minute(minute) {
|
|
491
|
+
const newDate = new Date(this.date);
|
|
492
|
+
newDate.setMinutes(minute);
|
|
493
|
+
return new _DateBuilder(newDate);
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Set the second.
|
|
497
|
+
* @param second - Second to set
|
|
498
|
+
*/
|
|
499
|
+
second(second) {
|
|
500
|
+
const newDate = new Date(this.date);
|
|
501
|
+
newDate.setSeconds(second);
|
|
502
|
+
return new _DateBuilder(newDate);
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Set the millisecond.
|
|
506
|
+
* @param millisecond - Millisecond to set
|
|
507
|
+
*/
|
|
508
|
+
millisecond(millisecond) {
|
|
509
|
+
const newDate = new Date(this.date);
|
|
510
|
+
newDate.setMilliseconds(millisecond);
|
|
511
|
+
return new _DateBuilder(newDate);
|
|
512
|
+
}
|
|
513
|
+
// ========== Addition/Subtraction Methods ==========
|
|
514
|
+
/**
|
|
515
|
+
* Add years to the date.
|
|
516
|
+
* @param years - Number of years to add (can be negative)
|
|
517
|
+
*/
|
|
518
|
+
addYears(years) {
|
|
519
|
+
return new _DateBuilder(addToDate(this.date, years, "years"));
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Add months to the date.
|
|
523
|
+
* @param months - Number of months to add (can be negative)
|
|
524
|
+
*/
|
|
525
|
+
addMonths(months) {
|
|
526
|
+
return new _DateBuilder(addToDate(this.date, months, "months"));
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Add days to the date.
|
|
530
|
+
* @param days - Number of days to add (can be negative)
|
|
531
|
+
*/
|
|
532
|
+
addDays(days) {
|
|
533
|
+
return new _DateBuilder(addToDate(this.date, days, "days"));
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Add hours to the date.
|
|
537
|
+
* @param hours - Number of hours to add (can be negative)
|
|
538
|
+
*/
|
|
539
|
+
addHours(hours) {
|
|
540
|
+
return new _DateBuilder(addToDate(this.date, hours, "hours"));
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Add minutes to the date.
|
|
544
|
+
* @param minutes - Number of minutes to add (can be negative)
|
|
545
|
+
*/
|
|
546
|
+
addMinutes(minutes) {
|
|
547
|
+
return new _DateBuilder(addToDate(this.date, minutes, "minutes"));
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Add seconds to the date.
|
|
551
|
+
* @param seconds - Number of seconds to add (can be negative)
|
|
552
|
+
*/
|
|
553
|
+
addSeconds(seconds) {
|
|
554
|
+
return new _DateBuilder(addToDate(this.date, seconds, "seconds"));
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Add milliseconds to the date.
|
|
558
|
+
* @param milliseconds - Number of milliseconds to add (can be negative)
|
|
559
|
+
*/
|
|
560
|
+
addMilliseconds(milliseconds) {
|
|
561
|
+
return new _DateBuilder(addToDate(this.date, milliseconds, "milliseconds"));
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Subtract years from the date.
|
|
565
|
+
* @param years - Number of years to subtract
|
|
566
|
+
*/
|
|
567
|
+
subtractYears(years) {
|
|
568
|
+
return this.addYears(-years);
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Subtract months from the date.
|
|
572
|
+
* @param months - Number of months to subtract
|
|
573
|
+
*/
|
|
574
|
+
subtractMonths(months) {
|
|
575
|
+
return this.addMonths(-months);
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Subtract days from the date.
|
|
579
|
+
* @param days - Number of days to subtract
|
|
580
|
+
*/
|
|
581
|
+
subtractDays(days) {
|
|
582
|
+
return this.addDays(-days);
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Subtract hours from the date.
|
|
586
|
+
* @param hours - Number of hours to subtract
|
|
587
|
+
*/
|
|
588
|
+
subtractHours(hours) {
|
|
589
|
+
return this.addHours(-hours);
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Subtract minutes from the date.
|
|
593
|
+
* @param minutes - Number of minutes to subtract
|
|
594
|
+
*/
|
|
595
|
+
subtractMinutes(minutes) {
|
|
596
|
+
return this.addMinutes(-minutes);
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Subtract seconds from the date.
|
|
600
|
+
* @param seconds - Number of seconds to subtract
|
|
601
|
+
*/
|
|
602
|
+
subtractSeconds(seconds) {
|
|
603
|
+
return this.addSeconds(-seconds);
|
|
604
|
+
}
|
|
605
|
+
// ========== Start/End of Period Methods ==========
|
|
606
|
+
/**
|
|
607
|
+
* Set to the start of the second.
|
|
608
|
+
*/
|
|
609
|
+
startOfSecond() {
|
|
610
|
+
return new _DateBuilder(startOf(this.date, "second"));
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Set to the start of the minute.
|
|
614
|
+
*/
|
|
615
|
+
startOfMinute() {
|
|
616
|
+
return new _DateBuilder(startOf(this.date, "minute"));
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Set to the start of the hour.
|
|
620
|
+
*/
|
|
621
|
+
startOfHour() {
|
|
622
|
+
return new _DateBuilder(startOf(this.date, "hour"));
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Set to the start of the day (midnight).
|
|
626
|
+
*/
|
|
627
|
+
startOfDay() {
|
|
628
|
+
return new _DateBuilder(startOf(this.date, "day"));
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Set to the start of the week (Sunday).
|
|
632
|
+
*/
|
|
633
|
+
startOfWeek() {
|
|
634
|
+
return new _DateBuilder(startOf(this.date, "week"));
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Set to the start of the month.
|
|
638
|
+
*/
|
|
639
|
+
startOfMonth() {
|
|
640
|
+
return new _DateBuilder(startOf(this.date, "month"));
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Set to the start of the quarter.
|
|
644
|
+
*/
|
|
645
|
+
startOfQuarter() {
|
|
646
|
+
return new _DateBuilder(startOf(this.date, "quarter"));
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Set to the start of the year.
|
|
650
|
+
*/
|
|
651
|
+
startOfYear() {
|
|
652
|
+
return new _DateBuilder(startOf(this.date, "year"));
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* Set to the end of the second.
|
|
656
|
+
*/
|
|
657
|
+
endOfSecond() {
|
|
658
|
+
return new _DateBuilder(endOf(this.date, "second"));
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Set to the end of the minute.
|
|
662
|
+
*/
|
|
663
|
+
endOfMinute() {
|
|
664
|
+
return new _DateBuilder(endOf(this.date, "minute"));
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Set to the end of the hour.
|
|
668
|
+
*/
|
|
669
|
+
endOfHour() {
|
|
670
|
+
return new _DateBuilder(endOf(this.date, "hour"));
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Set to the end of the day (23:59:59.999).
|
|
674
|
+
*/
|
|
675
|
+
endOfDay() {
|
|
676
|
+
return new _DateBuilder(endOf(this.date, "day"));
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Set to the end of the week (Saturday).
|
|
680
|
+
*/
|
|
681
|
+
endOfWeek() {
|
|
682
|
+
return new _DateBuilder(endOf(this.date, "week"));
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Set to the end of the month.
|
|
686
|
+
*/
|
|
687
|
+
endOfMonth() {
|
|
688
|
+
return new _DateBuilder(endOf(this.date, "month"));
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* Set to the end of the quarter.
|
|
692
|
+
*/
|
|
693
|
+
endOfQuarter() {
|
|
694
|
+
return new _DateBuilder(endOf(this.date, "quarter"));
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Set to the end of the year.
|
|
698
|
+
*/
|
|
699
|
+
endOfYear() {
|
|
700
|
+
return new _DateBuilder(endOf(this.date, "year"));
|
|
701
|
+
}
|
|
702
|
+
// ========== Comparison Methods ==========
|
|
703
|
+
/**
|
|
704
|
+
* Check if the date is before another date.
|
|
705
|
+
* @param other - Date to compare with
|
|
706
|
+
*/
|
|
707
|
+
isBefore(other) {
|
|
708
|
+
const otherDate = other instanceof _DateBuilder ? other.toDate() : other;
|
|
709
|
+
return this.date.getTime() < otherDate.getTime();
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Check if the date is after another date.
|
|
713
|
+
* @param other - Date to compare with
|
|
714
|
+
*/
|
|
715
|
+
isAfter(other) {
|
|
716
|
+
const otherDate = other instanceof _DateBuilder ? other.toDate() : other;
|
|
717
|
+
return this.date.getTime() > otherDate.getTime();
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Check if the date is the same as another date.
|
|
721
|
+
* @param other - Date to compare with
|
|
722
|
+
*/
|
|
723
|
+
isSame(other) {
|
|
724
|
+
const otherDate = other instanceof _DateBuilder ? other.toDate() : other;
|
|
725
|
+
return this.date.getTime() === otherDate.getTime();
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Check if the date is between two dates.
|
|
729
|
+
* @param start - Start date
|
|
730
|
+
* @param end - End date
|
|
731
|
+
* @param inclusive - Whether to include boundaries
|
|
732
|
+
*/
|
|
733
|
+
isBetween(start, end, inclusive = true) {
|
|
734
|
+
const startDate = start instanceof _DateBuilder ? start.toDate() : start;
|
|
735
|
+
const endDate = end instanceof _DateBuilder ? end.toDate() : end;
|
|
736
|
+
return isBetween(this.date, startDate, endDate, inclusive);
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* Check if the date is today.
|
|
740
|
+
*/
|
|
741
|
+
isToday() {
|
|
742
|
+
return isToday(this.date);
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Check if the date is in the future.
|
|
746
|
+
*/
|
|
747
|
+
isFuture() {
|
|
748
|
+
return isFuture(this.date);
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Check if the date is in the past.
|
|
752
|
+
*/
|
|
753
|
+
isPast() {
|
|
754
|
+
return isPast(this.date);
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* Check if the date is on a weekend.
|
|
758
|
+
*/
|
|
759
|
+
isWeekend() {
|
|
760
|
+
return isWeekend(this.date);
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* Check if the year is a leap year.
|
|
764
|
+
*/
|
|
765
|
+
isLeapYear() {
|
|
766
|
+
return isLeapYear(this.date);
|
|
767
|
+
}
|
|
768
|
+
// ========== Getters ==========
|
|
769
|
+
/**
|
|
770
|
+
* Get the year.
|
|
771
|
+
*/
|
|
772
|
+
getYear() {
|
|
773
|
+
return this.date.getFullYear();
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* Get the month (1-12, not 0-11).
|
|
777
|
+
*/
|
|
778
|
+
getMonth() {
|
|
779
|
+
return this.date.getMonth() + 1;
|
|
780
|
+
}
|
|
781
|
+
/**
|
|
782
|
+
* Get the day of the month.
|
|
783
|
+
*/
|
|
784
|
+
getDay() {
|
|
785
|
+
return this.date.getDate();
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* Get the day of the week (0-6, Sunday is 0).
|
|
789
|
+
*/
|
|
790
|
+
getDayOfWeek() {
|
|
791
|
+
return this.date.getDay();
|
|
792
|
+
}
|
|
793
|
+
/**
|
|
794
|
+
* Get the hour.
|
|
795
|
+
*/
|
|
796
|
+
getHour() {
|
|
797
|
+
return this.date.getHours();
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Get the minute.
|
|
801
|
+
*/
|
|
802
|
+
getMinute() {
|
|
803
|
+
return this.date.getMinutes();
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* Get the second.
|
|
807
|
+
*/
|
|
808
|
+
getSecond() {
|
|
809
|
+
return this.date.getSeconds();
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Get the millisecond.
|
|
813
|
+
*/
|
|
814
|
+
getMillisecond() {
|
|
815
|
+
return this.date.getMilliseconds();
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* Get the quarter (1-4).
|
|
819
|
+
*/
|
|
820
|
+
getQuarter() {
|
|
821
|
+
return quarterOf(this.date);
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* Get the ISO week number.
|
|
825
|
+
*/
|
|
826
|
+
getWeekOfYear() {
|
|
827
|
+
return weekOfYear(this.date);
|
|
828
|
+
}
|
|
829
|
+
/**
|
|
830
|
+
* Get the number of days in the current month.
|
|
831
|
+
*/
|
|
832
|
+
getDaysInMonth() {
|
|
833
|
+
return daysInMonth(this.date);
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Get the Unix timestamp in milliseconds.
|
|
837
|
+
*/
|
|
838
|
+
getTime() {
|
|
839
|
+
return this.date.getTime();
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* Get the Unix timestamp in seconds.
|
|
843
|
+
*/
|
|
844
|
+
getUnixTimestamp() {
|
|
845
|
+
return Math.floor(this.date.getTime() / 1e3);
|
|
846
|
+
}
|
|
847
|
+
// ========== Difference Methods ==========
|
|
848
|
+
/**
|
|
849
|
+
* Calculate the difference from another date.
|
|
850
|
+
* @param other - Date to compare with
|
|
851
|
+
* @param unit - Unit of time
|
|
852
|
+
*/
|
|
853
|
+
diff(other, unit = "milliseconds") {
|
|
854
|
+
const otherDate = other instanceof _DateBuilder ? other.toDate() : other;
|
|
855
|
+
return dateDiff(this.date, otherDate, unit);
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* Get the difference from now.
|
|
859
|
+
* @param unit - Unit of time
|
|
860
|
+
*/
|
|
861
|
+
diffFromNow(unit = "milliseconds") {
|
|
862
|
+
return dateDiff(this.date, /* @__PURE__ */ new Date(), unit);
|
|
863
|
+
}
|
|
864
|
+
// ========== Formatting Methods ==========
|
|
865
|
+
/**
|
|
866
|
+
* Format the date.
|
|
867
|
+
* @param options - Formatting options
|
|
868
|
+
*/
|
|
869
|
+
format(options) {
|
|
870
|
+
if (typeof options === "string") {
|
|
871
|
+
return formatDate(this.date, {
|
|
872
|
+
format: options
|
|
873
|
+
});
|
|
874
|
+
}
|
|
875
|
+
return formatDate(this.date, options);
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* Format as relative time (e.g., "5 minutes ago").
|
|
879
|
+
* @param locale - Locale for formatting
|
|
880
|
+
*/
|
|
881
|
+
formatRelative(locale) {
|
|
882
|
+
return formatRelativeTime(this.date, /* @__PURE__ */ new Date(), locale);
|
|
883
|
+
}
|
|
884
|
+
/**
|
|
885
|
+
* Format as ISO 8601 string.
|
|
886
|
+
*/
|
|
887
|
+
toISOString() {
|
|
888
|
+
return this.date.toISOString();
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* Format as UTC string.
|
|
892
|
+
*/
|
|
893
|
+
toUTCString() {
|
|
894
|
+
return this.date.toUTCString();
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Format as date string.
|
|
898
|
+
*/
|
|
899
|
+
toDateString() {
|
|
900
|
+
return this.date.toDateString();
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Format as time string.
|
|
904
|
+
*/
|
|
905
|
+
toTimeString() {
|
|
906
|
+
return this.date.toTimeString();
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
* Format as locale string.
|
|
910
|
+
*/
|
|
911
|
+
toLocaleString(locales, options) {
|
|
912
|
+
return this.date.toLocaleString(locales, options);
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* Convert to JSON string.
|
|
916
|
+
*/
|
|
917
|
+
toJSON() {
|
|
918
|
+
return this.date.toJSON();
|
|
919
|
+
}
|
|
920
|
+
// ========== Conversion Methods ==========
|
|
921
|
+
/**
|
|
922
|
+
* Build and return the Date object.
|
|
923
|
+
*/
|
|
924
|
+
build() {
|
|
925
|
+
return new Date(this.date);
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* Get the Date object (alias for build).
|
|
929
|
+
*/
|
|
930
|
+
toDate() {
|
|
931
|
+
return new Date(this.date);
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* Get the internal Date object value.
|
|
935
|
+
*/
|
|
936
|
+
valueOf() {
|
|
937
|
+
return this.date.valueOf();
|
|
938
|
+
}
|
|
939
|
+
/**
|
|
940
|
+
* Convert to string.
|
|
941
|
+
*/
|
|
942
|
+
toString() {
|
|
943
|
+
return this.date.toString();
|
|
944
|
+
}
|
|
945
|
+
};
|
|
284
946
|
|
|
947
|
+
exports.DateBuilder = DateBuilder;
|
|
948
|
+
exports.addDays = addDays;
|
|
949
|
+
exports.addMonths = addMonths;
|
|
285
950
|
exports.addToDate = addToDate;
|
|
951
|
+
exports.addYears = addYears;
|
|
286
952
|
exports.dateDiff = dateDiff;
|
|
287
953
|
exports.daysInMonth = daysInMonth;
|
|
288
954
|
exports.endOf = endOf;
|
|
289
955
|
exports.formatDate = formatDate;
|
|
290
956
|
exports.formatDuration = formatDuration;
|
|
291
957
|
exports.formatRelativeTime = formatRelativeTime;
|
|
958
|
+
exports.getDateFromDuration = getDateFromDuration;
|
|
292
959
|
exports.isBetween = isBetween;
|
|
960
|
+
exports.isFuture = isFuture;
|
|
293
961
|
exports.isLeapYear = isLeapYear;
|
|
962
|
+
exports.isPast = isPast;
|
|
963
|
+
exports.isToday = isToday;
|
|
964
|
+
exports.isWeekend = isWeekend;
|
|
294
965
|
exports.parseDate = parseDate;
|
|
966
|
+
exports.parseDuration = parseDuration;
|
|
967
|
+
exports.quarterOf = quarterOf;
|
|
295
968
|
exports.startOf = startOf;
|
|
969
|
+
exports.weekOfYear = weekOfYear;
|