chronal 0.0.1-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/esm/core/dtf.d.ts +2 -0
  2. package/esm/core/dtf.d.ts.map +1 -0
  3. package/esm/core/dtf.js +20 -0
  4. package/esm/lib/add.d.ts +24 -0
  5. package/esm/lib/add.d.ts.map +1 -0
  6. package/esm/lib/add.js +47 -0
  7. package/esm/lib/difference.d.ts +15 -0
  8. package/esm/lib/difference.d.ts.map +1 -0
  9. package/esm/lib/difference.js +47 -0
  10. package/esm/lib/end-of.d.ts +14 -0
  11. package/esm/lib/end-of.d.ts.map +1 -0
  12. package/esm/lib/end-of.js +53 -0
  13. package/esm/lib/format.d.ts +21 -0
  14. package/esm/lib/format.d.ts.map +1 -0
  15. package/esm/lib/format.js +184 -0
  16. package/esm/lib/is-after.d.ts +13 -0
  17. package/esm/lib/is-after.d.ts.map +1 -0
  18. package/esm/lib/is-after.js +15 -0
  19. package/esm/lib/is-before.d.ts +13 -0
  20. package/esm/lib/is-before.d.ts.map +1 -0
  21. package/esm/lib/is-before.js +14 -0
  22. package/esm/lib/months.d.ts +13 -0
  23. package/esm/lib/months.d.ts.map +1 -0
  24. package/esm/lib/months.js +25 -0
  25. package/esm/lib/start-of.d.ts +14 -0
  26. package/esm/lib/start-of.d.ts.map +1 -0
  27. package/esm/lib/start-of.js +50 -0
  28. package/esm/lib/sub.d.ts +24 -0
  29. package/esm/lib/sub.d.ts.map +1 -0
  30. package/esm/lib/sub.js +32 -0
  31. package/esm/lib/weekdays.d.ts +13 -0
  32. package/esm/lib/weekdays.d.ts.map +1 -0
  33. package/esm/lib/weekdays.js +25 -0
  34. package/esm/mod.d.ts +12 -0
  35. package/esm/mod.d.ts.map +1 -0
  36. package/esm/mod.js +11 -0
  37. package/esm/package.json +3 -0
  38. package/esm/types/unit.d.ts +3 -0
  39. package/esm/types/unit.d.ts.map +1 -0
  40. package/esm/types/unit.js +1 -0
  41. package/package.json +26 -0
  42. package/script/core/dtf.d.ts +2 -0
  43. package/script/core/dtf.d.ts.map +1 -0
  44. package/script/core/dtf.js +23 -0
  45. package/script/lib/add.d.ts +24 -0
  46. package/script/lib/add.d.ts.map +1 -0
  47. package/script/lib/add.js +50 -0
  48. package/script/lib/difference.d.ts +15 -0
  49. package/script/lib/difference.d.ts.map +1 -0
  50. package/script/lib/difference.js +50 -0
  51. package/script/lib/end-of.d.ts +14 -0
  52. package/script/lib/end-of.d.ts.map +1 -0
  53. package/script/lib/end-of.js +56 -0
  54. package/script/lib/format.d.ts +21 -0
  55. package/script/lib/format.d.ts.map +1 -0
  56. package/script/lib/format.js +187 -0
  57. package/script/lib/is-after.d.ts +13 -0
  58. package/script/lib/is-after.d.ts.map +1 -0
  59. package/script/lib/is-after.js +18 -0
  60. package/script/lib/is-before.d.ts +13 -0
  61. package/script/lib/is-before.d.ts.map +1 -0
  62. package/script/lib/is-before.js +17 -0
  63. package/script/lib/months.d.ts +13 -0
  64. package/script/lib/months.d.ts.map +1 -0
  65. package/script/lib/months.js +28 -0
  66. package/script/lib/start-of.d.ts +14 -0
  67. package/script/lib/start-of.d.ts.map +1 -0
  68. package/script/lib/start-of.js +53 -0
  69. package/script/lib/sub.d.ts +24 -0
  70. package/script/lib/sub.d.ts.map +1 -0
  71. package/script/lib/sub.js +35 -0
  72. package/script/lib/weekdays.d.ts +13 -0
  73. package/script/lib/weekdays.d.ts.map +1 -0
  74. package/script/lib/weekdays.js +28 -0
  75. package/script/mod.d.ts +12 -0
  76. package/script/mod.d.ts.map +1 -0
  77. package/script/mod.js +27 -0
  78. package/script/package.json +3 -0
  79. package/script/types/unit.d.ts +3 -0
  80. package/script/types/unit.d.ts.map +1 -0
  81. package/script/types/unit.js +2 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sub.d.ts","sourceRoot":"","sources":["../../src/lib/sub.ts"],"names":[],"mappings":"AAEA,KAAK,UAAU,GAAG;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;GAUG;AAEH,wBAAgB,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,UAAU,GAAG,IAAI,CAarD"}
package/esm/lib/sub.js ADDED
@@ -0,0 +1,32 @@
1
+ import { add } from "./add.js";
2
+ /**
3
+ * Subtracts specified time units from the given date.
4
+ *
5
+ * @param date - The original date.
6
+ * @param opt - The time units to subtract.
7
+ * @returns A new Date object with the specified time units subtracted.
8
+ * @example
9
+ * const date = new Date('2024-03-31T12:00:00Z');
10
+ * const newDate = sub(date, { years: 1, months: 1 });
11
+ * console.log(newDate.toISOString()); // '2023-02-28T12:00:00.000Z'
12
+ */
13
+ export function sub(date, opt) {
14
+ const out = {};
15
+ if (opt.years)
16
+ out.years = -opt.years;
17
+ if (opt.months)
18
+ out.months = -opt.months;
19
+ if (opt.weeks)
20
+ out.weeks = -opt.weeks;
21
+ if (opt.days)
22
+ out.days = -opt.days;
23
+ if (opt.hours)
24
+ out.hours = -opt.hours;
25
+ if (opt.minutes)
26
+ out.minutes = -opt.minutes;
27
+ if (opt.seconds)
28
+ out.seconds = -opt.seconds;
29
+ if (opt.milliseconds)
30
+ out.milliseconds = -opt.milliseconds;
31
+ return add(date, out);
32
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Returns an array of weekday names for the specified locale and format.
3
+ *
4
+ * @param locale - The locale to use (default: 'en-US').
5
+ * @param format - The format of weekday names: 'long', 'short', or 'narrow' (default: 'long').
6
+ * @returns An array of 7 weekday names starting from Monday.
7
+ * @example
8
+ * console.log(weekdays('en-US', 'long')); // ['Monday', 'Tuesday', 'Wednesday', ...]
9
+ * console.log(weekdays('en-US', 'short')); // ['Mon', 'Tue', 'Wed', ...]
10
+ * console.log(weekdays('pt-BR', 'long')); // ['segunda-feira', 'terça-feira', ...]
11
+ */
12
+ export declare function weekdays(locale?: string, format?: "long" | "short" | "narrow"): string[];
13
+ //# sourceMappingURL=weekdays.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"weekdays.d.ts","sourceRoot":"","sources":["../../src/lib/weekdays.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AAEH,wBAAgB,QAAQ,CACtB,MAAM,SAAU,EAChB,MAAM,GAAE,MAAM,GAAG,OAAO,GAAG,QAAiB,GAC3C,MAAM,EAAE,CAiBV"}
@@ -0,0 +1,25 @@
1
+ const daysCache = new Map();
2
+ /**
3
+ * Returns an array of weekday names for the specified locale and format.
4
+ *
5
+ * @param locale - The locale to use (default: 'en-US').
6
+ * @param format - The format of weekday names: 'long', 'short', or 'narrow' (default: 'long').
7
+ * @returns An array of 7 weekday names starting from Monday.
8
+ * @example
9
+ * console.log(weekdays('en-US', 'long')); // ['Monday', 'Tuesday', 'Wednesday', ...]
10
+ * console.log(weekdays('en-US', 'short')); // ['Mon', 'Tue', 'Wed', ...]
11
+ * console.log(weekdays('pt-BR', 'long')); // ['segunda-feira', 'terça-feira', ...]
12
+ */
13
+ export function weekdays(locale = "en-US", format = "long") {
14
+ const key = `${locale}|${format}`;
15
+ const cached = daysCache.get(key);
16
+ if (cached)
17
+ return cached;
18
+ const fmt = new Intl.DateTimeFormat(locale, {
19
+ weekday: format,
20
+ timeZone: "UTC",
21
+ });
22
+ const result = Array.from({ length: 7 }, (_, i) => fmt.format(new Date(Date.UTC(2024, 0, i + 1))));
23
+ daysCache.set(key, result);
24
+ return result;
25
+ }
package/esm/mod.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ export * from "./lib/add.js";
2
+ export * from "./lib/sub.js";
3
+ export * from "./lib/months.js";
4
+ export * from "./lib/start-of.js";
5
+ export * from "./lib/end-of.js";
6
+ export * from "./lib/format.js";
7
+ export * from "./lib/weekdays.js";
8
+ export * from "./lib/is-after.js";
9
+ export * from "./lib/is-before.js";
10
+ export * from "./lib/difference.js";
11
+ export * from "./types/unit.js";
12
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC"}
package/esm/mod.js ADDED
@@ -0,0 +1,11 @@
1
+ export * from "./lib/add.js";
2
+ export * from "./lib/sub.js";
3
+ export * from "./lib/months.js";
4
+ export * from "./lib/start-of.js";
5
+ export * from "./lib/end-of.js";
6
+ export * from "./lib/format.js";
7
+ export * from "./lib/weekdays.js";
8
+ export * from "./lib/is-after.js";
9
+ export * from "./lib/is-before.js";
10
+ export * from "./lib/difference.js";
11
+ export * from "./types/unit.js";
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,3 @@
1
+ export type Unit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second';
2
+ export type PluralUnit = 'years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds';
3
+ //# sourceMappingURL=unit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unit.d.ts","sourceRoot":"","sources":["../../src/types/unit.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,IAAI,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,QAAQ,GAAG,QAAQ,CAAC;AACpF,MAAM,MAAM,UAAU,GAAG,OAAO,GAAG,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,SAAS,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "chronal",
3
+ "version": "0.0.1-0",
4
+ "description": "A tiny and fast date utility library for modern JavaScript runtimes.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/andreluizsgf/chronal"
8
+ },
9
+ "license": "MIT",
10
+ "bugs": {
11
+ "url": "https://github.com/andreluizsgf/chronal/issues"
12
+ },
13
+ "main": "./script/mod.js",
14
+ "module": "./esm/mod.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": "./esm/mod.js",
18
+ "require": "./script/mod.js"
19
+ }
20
+ },
21
+ "scripts": {},
22
+ "devDependencies": {
23
+ "@types/node": "^20.9.0"
24
+ },
25
+ "_generatedBy": "dnt@dev"
26
+ }
@@ -0,0 +1,2 @@
1
+ export declare function getDTF(locale: string, tz: string): Intl.DateTimeFormat;
2
+ //# sourceMappingURL=dtf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dtf.d.ts","sourceRoot":"","sources":["../../src/core/dtf.ts"],"names":[],"mappings":"AAEA,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,uBAiBhD"}
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDTF = getDTF;
4
+ const dtfCache = new Map();
5
+ function getDTF(locale, tz) {
6
+ const key = `${locale}|${tz}`;
7
+ let dtf = dtfCache.get(key);
8
+ if (!dtf) {
9
+ dtf = new Intl.DateTimeFormat(locale, {
10
+ year: 'numeric',
11
+ month: '2-digit',
12
+ day: '2-digit',
13
+ hour: '2-digit',
14
+ minute: '2-digit',
15
+ second: '2-digit',
16
+ hour12: false,
17
+ timeZone: tz,
18
+ });
19
+ dtfCache.set(key, dtf);
20
+ }
21
+ return dtf;
22
+ }
23
+ ;
@@ -0,0 +1,24 @@
1
+ type AddOptions = {
2
+ years?: number;
3
+ months?: number;
4
+ weeks?: number;
5
+ days?: number;
6
+ hours?: number;
7
+ minutes?: number;
8
+ seconds?: number;
9
+ milliseconds?: number;
10
+ };
11
+ /**
12
+ * Adds specified time units to the given date.
13
+ *
14
+ * @param date - The original date.
15
+ * @param opt - The time units to add.
16
+ * @returns A new Date object with the specified time units added.
17
+ * @example
18
+ * const date = new Date('2024-01-31T12:00:00Z');
19
+ * const newDate = add(date, { years: 1, months: 2, days: 3 });
20
+ * console.log(newDate.toISOString()); // '2025-04-03T12:00:00.000Z'
21
+ */
22
+ export declare function add(date: Date, opt: AddOptions): Date;
23
+ export {};
24
+ //# sourceMappingURL=add.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"add.d.ts","sourceRoot":"","sources":["../../src/lib/add.ts"],"names":[],"mappings":"AAAA,KAAK,UAAU,GAAG;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;GAUG;AAEH,wBAAgB,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,UAAU,GAAG,IAAI,CA8CrD"}
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.add = add;
4
+ /**
5
+ * Adds specified time units to the given date.
6
+ *
7
+ * @param date - The original date.
8
+ * @param opt - The time units to add.
9
+ * @returns A new Date object with the specified time units added.
10
+ * @example
11
+ * const date = new Date('2024-01-31T12:00:00Z');
12
+ * const newDate = add(date, { years: 1, months: 2, days: 3 });
13
+ * console.log(newDate.toISOString()); // '2025-04-03T12:00:00.000Z'
14
+ */
15
+ function add(date, opt) {
16
+ let d = new Date(date.getTime());
17
+ // ===== calendar-based (UTC-safe) =====
18
+ if (opt.years || opt.months) {
19
+ const years = opt.years ?? 0;
20
+ const months = opt.months ?? 0;
21
+ const year = d.getUTCFullYear();
22
+ const month = d.getUTCMonth(); // 0-based
23
+ const day = d.getUTCDate();
24
+ // target year/month (allow overflow, normalize manually)
25
+ let targetMonth = month + months;
26
+ const targetYear = year + years + Math.floor(targetMonth / 12);
27
+ targetMonth = ((targetMonth % 12) + 12) % 12;
28
+ // last valid day of target month
29
+ const lastDayOfTargetMonth = new Date(Date.UTC(targetYear, targetMonth + 1, 0)).getUTCDate();
30
+ const safeDay = Math.min(day, lastDayOfTargetMonth);
31
+ d = new Date(Date.UTC(targetYear, targetMonth, safeDay, d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds()));
32
+ }
33
+ // ===== time-based =====
34
+ let delta = 0;
35
+ if (opt.milliseconds)
36
+ delta += opt.milliseconds;
37
+ if (opt.seconds)
38
+ delta += opt.seconds * 1_000;
39
+ if (opt.minutes)
40
+ delta += opt.minutes * 60_000;
41
+ if (opt.hours)
42
+ delta += opt.hours * 3_600_000;
43
+ if (opt.days)
44
+ delta += opt.days * 86_400_000;
45
+ if (opt.weeks)
46
+ delta += opt.weeks * 604_800_000;
47
+ if (delta)
48
+ d = new Date(d.getTime() + delta);
49
+ return d;
50
+ }
@@ -0,0 +1,15 @@
1
+ import type { PluralUnit } from "../types/unit.js";
2
+ /**
3
+ * Calculates the difference between two dates in the specified unit.
4
+ *
5
+ * @param dateLeft - The first date.
6
+ * @param dateRight - The second date to subtract from the first.
7
+ * @param unit - The unit to express the difference in ('years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds').
8
+ * @returns The difference as a number (can be negative if dateLeft is before dateRight).
9
+ * @example
10
+ * const date1 = new Date('2024-01-20T12:00:00Z');
11
+ * const date2 = new Date('2024-01-15T12:00:00Z');
12
+ * console.log(difference(date1, date2, 'days')); // 5
13
+ */
14
+ export declare function difference(dateLeft: Date, dateRight: Date, unit: PluralUnit): number;
15
+ //# sourceMappingURL=difference.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"difference.d.ts","sourceRoot":"","sources":["../../src/lib/difference.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD;;;;;;;;;;;GAWG;AAEH,wBAAgB,UAAU,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,GAAG,MAAM,CAoDpF"}
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.difference = difference;
4
+ /**
5
+ * Calculates the difference between two dates in the specified unit.
6
+ *
7
+ * @param dateLeft - The first date.
8
+ * @param dateRight - The second date to subtract from the first.
9
+ * @param unit - The unit to express the difference in ('years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds').
10
+ * @returns The difference as a number (can be negative if dateLeft is before dateRight).
11
+ * @example
12
+ * const date1 = new Date('2024-01-20T12:00:00Z');
13
+ * const date2 = new Date('2024-01-15T12:00:00Z');
14
+ * console.log(difference(date1, date2, 'days')); // 5
15
+ */
16
+ function difference(dateLeft, dateRight, unit) {
17
+ const delta = dateLeft.getTime() - dateRight.getTime();
18
+ switch (unit) {
19
+ case 'years': {
20
+ const yearDiff = dateLeft.getUTCFullYear() - dateRight.getUTCFullYear();
21
+ const adjustedDateRight = new Date(Date.UTC(dateRight.getUTCFullYear() + yearDiff, dateRight.getUTCMonth(), dateRight.getUTCDate(), dateRight.getUTCHours(), dateRight.getUTCMinutes(), dateRight.getUTCSeconds(), dateRight.getUTCMilliseconds()));
22
+ if (adjustedDateRight > dateLeft) {
23
+ return yearDiff - 1;
24
+ }
25
+ return yearDiff;
26
+ }
27
+ case 'months': {
28
+ const yearDiff = dateLeft.getUTCFullYear() - dateRight.getUTCFullYear();
29
+ const monthDiff = dateLeft.getUTCMonth() - dateRight.getUTCMonth();
30
+ let totalMonths = yearDiff * 12 + monthDiff;
31
+ const adjustedDateRight = new Date(Date.UTC(dateRight.getUTCFullYear() + Math.floor(totalMonths / 12), (dateRight.getUTCMonth() + (totalMonths % 12) + 12) % 12, dateRight.getUTCDate(), dateRight.getUTCHours(), dateRight.getUTCMinutes(), dateRight.getUTCSeconds(), dateRight.getUTCMilliseconds()));
32
+ if (adjustedDateRight > dateLeft) {
33
+ totalMonths -= 1;
34
+ }
35
+ return totalMonths;
36
+ }
37
+ case 'weeks':
38
+ return Math.floor(delta / 604800000); // 7 * 24 * 60 * 60 * 1000
39
+ case 'days':
40
+ return Math.floor(delta / 86400000); // 24 * 60 * 60 * 1000
41
+ case 'hours':
42
+ return Math.floor(delta / 3600000); // 60 * 60 * 1000
43
+ case 'minutes':
44
+ return Math.floor(delta / 60000); // 60 * 1000
45
+ case 'seconds':
46
+ return Math.floor(delta / 1000);
47
+ default:
48
+ throw new Error(`Unsupported unit: ${unit}`);
49
+ }
50
+ }
@@ -0,0 +1,14 @@
1
+ import type { Unit } from "../types/unit.js";
2
+ /**
3
+ * Returns the end of the specified time unit for the given date.
4
+ *
5
+ * @param date - The original date.
6
+ * @param unit - The time unit to get the end of ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second').
7
+ * @returns A new Date object set to the end of the specified unit (last millisecond).
8
+ * @example
9
+ * const date = new Date('2024-06-15T14:35:22.500Z');
10
+ * const endOfDay = endOf(date, 'day');
11
+ * console.log(endOfDay.toISOString()); // '2024-06-15T23:59:59.999Z'
12
+ */
13
+ export declare function endOf(date: Date, unit: Unit): Date;
14
+ //# sourceMappingURL=end-of.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"end-of.d.ts","sourceRoot":"","sources":["../../src/lib/end-of.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;;;;;;;;;GAUG;AAEH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,CA2ClD"}
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.endOf = endOf;
4
+ /**
5
+ * Returns the end of the specified time unit for the given date.
6
+ *
7
+ * @param date - The original date.
8
+ * @param unit - The time unit to get the end of ('year' | 'month' | 'day' | 'hour' | 'minute' | 'second').
9
+ * @returns A new Date object set to the end of the specified unit (last millisecond).
10
+ * @example
11
+ * const date = new Date('2024-06-15T14:35:22.500Z');
12
+ * const endOfDay = endOf(date, 'day');
13
+ * console.log(endOfDay.toISOString()); // '2024-06-15T23:59:59.999Z'
14
+ */
15
+ function endOf(date, unit) {
16
+ const time = new Date(date.getTime());
17
+ switch (unit) {
18
+ case 'year': {
19
+ time.setUTCMonth(11, 31);
20
+ time.setUTCHours(23, 59, 59, 999);
21
+ break;
22
+ }
23
+ case 'month': {
24
+ const year = time.getUTCFullYear();
25
+ const month = time.getUTCMonth();
26
+ const lastDay = new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
27
+ time.setUTCDate(lastDay);
28
+ time.setUTCHours(23, 59, 59, 999);
29
+ break;
30
+ }
31
+ case 'week': {
32
+ const day = time.getUTCDay();
33
+ const diff = (day === 0 ? 0 : 7) - day; // Adjust when day is Sunday
34
+ time.setUTCDate(time.getUTCDate() + diff);
35
+ time.setUTCHours(23, 59, 59, 999);
36
+ break;
37
+ }
38
+ case 'day': {
39
+ time.setUTCHours(23, 59, 59, 999);
40
+ break;
41
+ }
42
+ case 'hour': {
43
+ time.setUTCMinutes(59, 59, 999);
44
+ break;
45
+ }
46
+ case 'minute': {
47
+ time.setUTCSeconds(59, 999);
48
+ break;
49
+ }
50
+ case 'second': {
51
+ time.setUTCMilliseconds(999);
52
+ break;
53
+ }
54
+ }
55
+ return time;
56
+ }
@@ -0,0 +1,21 @@
1
+ type FormatOptions = {
2
+ tz?: string;
3
+ locale?: string;
4
+ };
5
+ /**
6
+ * Formats a date into a string using the specified format pattern.
7
+ *
8
+ * @param date - The date to format.
9
+ * @param fmt - The format string with tokens (YYYY, MM, DD, HH, mm, ss, etc.).
10
+ * @param options - Optional formatting options (locale and timezone).
11
+ * @returns The formatted date string.
12
+ * @example
13
+ * const date = new Date('2024-06-15T14:35:22Z');
14
+ * console.log(format(date, 'YYYY-MM-DD')); // '2024-06-15'
15
+ * console.log(format(date, 'YYYY-MM-DD HH:mm:ss')); // '2024-06-15 14:35:22'
16
+ * console.log(format(date, 'DD/MM/YYYY [at] HH:mm')); // '15/06/2024 at 14:35'
17
+ * console.log(format(date, 'MMMM D, YYYY', { locale: 'en-US' })); // 'June 15, 2024'
18
+ */
19
+ export declare function format(date: Date, fmt: string, options?: FormatOptions): string;
20
+ export {};
21
+ //# sourceMappingURL=format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../src/lib/format.ts"],"names":[],"mappings":"AAGA,KAAK,aAAa,GAAG;IACnB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAkLF;;;;;;;;;;;;;GAaG;AAEH,wBAAgB,MAAM,CACpB,IAAI,EAAE,IAAI,EACV,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAqBR"}
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.format = format;
4
+ const dtf_js_1 = require("../core/dtf.js");
5
+ const months_js_1 = require("./months.js");
6
+ // Pre-compute pad functions to avoid repeated padStart calls
7
+ const pad2 = (n) => n < 10 ? '0' + n : String(n);
8
+ const tokenMap = {
9
+ YYYY: p => String(p.year),
10
+ YY: p => String(p.year).slice(-2),
11
+ M: p => String(p.month),
12
+ MM: p => pad2(p.month),
13
+ MMM: (p, l) => (0, months_js_1.months)(l, 'short')[p.month - 1],
14
+ MMMM: (p, l) => (0, months_js_1.months)(l, 'long')[p.month - 1],
15
+ D: p => String(p.day),
16
+ DD: p => pad2(p.day),
17
+ H: p => String(p.hour),
18
+ HH: p => pad2(p.hour),
19
+ m: p => String(p.minute),
20
+ mm: p => pad2(p.minute),
21
+ s: p => String(p.second),
22
+ ss: p => pad2(p.second),
23
+ };
24
+ const tokenRegex = /YYYY|MMMM|MMM|YY|MM|M|DD|D|HH|H|mm|m|ss|s/g;
25
+ const formatCache = new Map();
26
+ function compileFormat(fmt) {
27
+ const cached = formatCache.get(fmt);
28
+ if (cached)
29
+ return cached;
30
+ const hasLiterals = fmt.includes('[');
31
+ const parts = [];
32
+ if (!hasLiterals) {
33
+ // Fast path: no literals, just parse tokens
34
+ let lastIndex = 0;
35
+ tokenRegex.lastIndex = 0;
36
+ let match;
37
+ while ((match = tokenRegex.exec(fmt)) !== null) {
38
+ if (match.index > lastIndex) {
39
+ parts.push({ type: 'literal', value: fmt.slice(lastIndex, match.index) });
40
+ }
41
+ parts.push({ type: 'token', value: match[0] });
42
+ lastIndex = match.index + match[0].length;
43
+ }
44
+ if (lastIndex < fmt.length) {
45
+ parts.push({ type: 'literal', value: fmt.slice(lastIndex) });
46
+ }
47
+ }
48
+ else {
49
+ // Slow path: handle escaped literals
50
+ let i = 0;
51
+ let currentLiteral = '';
52
+ let inBracket = false;
53
+ while (i < fmt.length) {
54
+ if (fmt[i] === '[' && !inBracket) {
55
+ if (currentLiteral) {
56
+ // Process current literal for tokens
57
+ let lastIndex = 0;
58
+ tokenRegex.lastIndex = 0;
59
+ let match;
60
+ while ((match = tokenRegex.exec(currentLiteral)) !== null) {
61
+ if (match.index > lastIndex) {
62
+ parts.push({ type: 'literal', value: currentLiteral.slice(lastIndex, match.index) });
63
+ }
64
+ parts.push({ type: 'token', value: match[0] });
65
+ lastIndex = match.index + match[0].length;
66
+ }
67
+ if (lastIndex < currentLiteral.length) {
68
+ parts.push({ type: 'literal', value: currentLiteral.slice(lastIndex) });
69
+ }
70
+ currentLiteral = '';
71
+ }
72
+ inBracket = true;
73
+ i++;
74
+ }
75
+ else if (fmt[i] === ']' && inBracket) {
76
+ // Escaped literal content - add as-is
77
+ if (currentLiteral) {
78
+ parts.push({ type: 'literal', value: currentLiteral });
79
+ currentLiteral = '';
80
+ }
81
+ inBracket = false;
82
+ i++;
83
+ }
84
+ else {
85
+ currentLiteral += fmt[i];
86
+ i++;
87
+ }
88
+ }
89
+ if (currentLiteral) {
90
+ let lastIndex = 0;
91
+ tokenRegex.lastIndex = 0;
92
+ let match;
93
+ while ((match = tokenRegex.exec(currentLiteral)) !== null) {
94
+ if (match.index > lastIndex) {
95
+ parts.push({ type: 'literal', value: currentLiteral.slice(lastIndex, match.index) });
96
+ }
97
+ parts.push({ type: 'token', value: match[0] });
98
+ lastIndex = match.index + match[0].length;
99
+ }
100
+ if (lastIndex < currentLiteral.length) {
101
+ parts.push({ type: 'literal', value: currentLiteral.slice(lastIndex) });
102
+ }
103
+ }
104
+ }
105
+ const compiled = { hasLiterals, parts };
106
+ formatCache.set(fmt, compiled);
107
+ return compiled;
108
+ }
109
+ // Fast UTC date parts extraction
110
+ function getUTCParts(date) {
111
+ return {
112
+ year: date.getUTCFullYear(),
113
+ month: date.getUTCMonth() + 1,
114
+ day: date.getUTCDate(),
115
+ hour: date.getUTCHours(),
116
+ minute: date.getUTCMinutes(),
117
+ second: date.getUTCSeconds(),
118
+ };
119
+ }
120
+ // Fallback for non-UTC timezones
121
+ function getPartsWithTZ(date, locale, tz) {
122
+ const partsRaw = (0, dtf_js_1.getDTF)(locale, tz).formatToParts(date);
123
+ const p = {
124
+ year: 0,
125
+ month: 0,
126
+ day: 0,
127
+ hour: 0,
128
+ minute: 0,
129
+ second: 0,
130
+ };
131
+ for (const part of partsRaw) {
132
+ switch (part.type) {
133
+ case "year":
134
+ p.year = +part.value;
135
+ break;
136
+ case "month":
137
+ p.month = +part.value;
138
+ break;
139
+ case "day":
140
+ p.day = +part.value;
141
+ break;
142
+ case "hour":
143
+ p.hour = +part.value;
144
+ break;
145
+ case "minute":
146
+ p.minute = +part.value;
147
+ break;
148
+ case "second":
149
+ p.second = +part.value;
150
+ break;
151
+ }
152
+ }
153
+ return p;
154
+ }
155
+ /**
156
+ * Formats a date into a string using the specified format pattern.
157
+ *
158
+ * @param date - The date to format.
159
+ * @param fmt - The format string with tokens (YYYY, MM, DD, HH, mm, ss, etc.).
160
+ * @param options - Optional formatting options (locale and timezone).
161
+ * @returns The formatted date string.
162
+ * @example
163
+ * const date = new Date('2024-06-15T14:35:22Z');
164
+ * console.log(format(date, 'YYYY-MM-DD')); // '2024-06-15'
165
+ * console.log(format(date, 'YYYY-MM-DD HH:mm:ss')); // '2024-06-15 14:35:22'
166
+ * console.log(format(date, 'DD/MM/YYYY [at] HH:mm')); // '15/06/2024 at 14:35'
167
+ * console.log(format(date, 'MMMM D, YYYY', { locale: 'en-US' })); // 'June 15, 2024'
168
+ */
169
+ function format(date, fmt, options = {}) {
170
+ const locale = options.locale ?? "en-US";
171
+ const tz = options.tz ?? "UTC";
172
+ // Get compiled format (cached)
173
+ const compiled = compileFormat(fmt);
174
+ // Fast path for UTC
175
+ const p = tz === "UTC" ? getUTCParts(date) : getPartsWithTZ(date, locale, tz);
176
+ // Build result string from compiled parts
177
+ let result = '';
178
+ for (const part of compiled.parts) {
179
+ if (part.type === 'literal') {
180
+ result += part.value;
181
+ }
182
+ else {
183
+ result += tokenMap[part.value](p, locale);
184
+ }
185
+ }
186
+ return result;
187
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Checks if the first date is after the second date.
3
+ *
4
+ * @param dateLeft - The date to compare.
5
+ * @param dateRight - The date to compare against.
6
+ * @returns True if dateLeft is after dateRight, false otherwise.
7
+ * @example
8
+ * const date1 = new Date('2024-01-20T12:00:00Z');
9
+ * const date2 = new Date('2024-01-15T12:00:00Z');
10
+ * console.log(isAfter(date1, date2)); // true
11
+ */
12
+ export declare function isAfter(dateLeft: Date, dateRight: Date): boolean;
13
+ //# sourceMappingURL=is-after.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-after.d.ts","sourceRoot":"","sources":["../../src/lib/is-after.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,wBAAgB,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,GAAG,OAAO,CAEhE"}