chronal 0.0.21 → 0.0.22

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 (55) hide show
  1. package/esm/chainable/chronal.d.ts.map +1 -1
  2. package/esm/chainable/chronal.js +27 -81
  3. package/esm/core/dtf.js +1 -1
  4. package/esm/core/rtf.js +1 -1
  5. package/esm/lib/add-time.d.ts.map +1 -1
  6. package/esm/lib/add-time.js +2 -3
  7. package/esm/lib/days-in-month.d.ts.map +1 -1
  8. package/esm/lib/days-in-month.js +2 -8
  9. package/esm/lib/format-date.d.ts.map +1 -1
  10. package/esm/lib/format-date.js +50 -110
  11. package/esm/lib/from-now.js +14 -14
  12. package/esm/lib/is-between.d.ts.map +1 -1
  13. package/esm/lib/is-between.js +1 -2
  14. package/esm/lib/is-same.d.ts.map +1 -1
  15. package/esm/lib/is-same.js +18 -55
  16. package/esm/lib/months.js +1 -1
  17. package/esm/lib/parse-date.d.ts.map +1 -1
  18. package/esm/lib/parse-date.js +6 -5
  19. package/esm/lib/set-unit.d.ts.map +1 -1
  20. package/esm/lib/set-unit.js +2 -0
  21. package/esm/lib/subtract-time.d.ts.map +1 -1
  22. package/esm/lib/subtract-time.js +10 -18
  23. package/esm/lib/to-now.d.ts.map +1 -1
  24. package/esm/lib/to-now.js +14 -14
  25. package/esm/lib/week-of-year.d.ts.map +1 -1
  26. package/esm/lib/week-of-year.js +1 -8
  27. package/esm/lib/weekdays.js +1 -1
  28. package/package.json +1 -1
  29. package/script/chainable/chronal.d.ts.map +1 -1
  30. package/script/chainable/chronal.js +27 -81
  31. package/script/core/dtf.js +1 -1
  32. package/script/core/rtf.js +1 -1
  33. package/script/lib/add-time.d.ts.map +1 -1
  34. package/script/lib/add-time.js +2 -3
  35. package/script/lib/days-in-month.d.ts.map +1 -1
  36. package/script/lib/days-in-month.js +2 -8
  37. package/script/lib/format-date.d.ts.map +1 -1
  38. package/script/lib/format-date.js +50 -110
  39. package/script/lib/from-now.js +14 -14
  40. package/script/lib/is-between.d.ts.map +1 -1
  41. package/script/lib/is-between.js +1 -2
  42. package/script/lib/is-same.d.ts.map +1 -1
  43. package/script/lib/is-same.js +18 -55
  44. package/script/lib/months.js +1 -1
  45. package/script/lib/parse-date.d.ts.map +1 -1
  46. package/script/lib/parse-date.js +6 -5
  47. package/script/lib/set-unit.d.ts.map +1 -1
  48. package/script/lib/set-unit.js +2 -0
  49. package/script/lib/subtract-time.d.ts.map +1 -1
  50. package/script/lib/subtract-time.js +10 -18
  51. package/script/lib/to-now.d.ts.map +1 -1
  52. package/script/lib/to-now.js +14 -14
  53. package/script/lib/week-of-year.d.ts.map +1 -1
  54. package/script/lib/week-of-year.js +1 -8
  55. package/script/lib/weekdays.js +1 -1
@@ -30,8 +30,7 @@ function isBetween(date, start, end, inclusivity = "[]") {
30
30
  const startTime = start.getTime();
31
31
  const endTime = end.getTime();
32
32
  // Handle reversed bounds
33
- const min = startTime < endTime ? startTime : endTime;
34
- const max = startTime < endTime ? endTime : startTime;
33
+ const [min, max] = startTime < endTime ? [startTime, endTime] : [endTime, startTime];
35
34
  switch (inclusivity) {
36
35
  case "[]":
37
36
  return time >= min && time <= max;
@@ -1 +1 @@
1
- {"version":3,"file":"is-same.d.ts","sourceRoot":"","sources":["../../src/lib/is-same.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;;;;;;;;;;;GAYG;AAEH,wBAAgB,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAuF3E"}
1
+ {"version":3,"file":"is-same.d.ts","sourceRoot":"","sources":["../../src/lib/is-same.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;;;;;;;;;;;GAYG;AAEH,wBAAgB,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CA4D3E"}
@@ -19,9 +19,8 @@ function isSame(dateLeft, dateRight, unit) {
19
19
  case "year":
20
20
  return dateLeft.getUTCFullYear() === dateRight.getUTCFullYear();
21
21
  case "month": {
22
- const yl = dateLeft.getUTCFullYear();
23
- const yr = dateRight.getUTCFullYear();
24
- return yl === yr && dateLeft.getUTCMonth() === dateRight.getUTCMonth();
22
+ return dateLeft.getUTCFullYear() === dateRight.getUTCFullYear() &&
23
+ dateLeft.getUTCMonth() === dateRight.getUTCMonth();
25
24
  }
26
25
  case "week": {
27
26
  // Calculate week start by getting days since Sunday
@@ -36,65 +35,29 @@ function isSame(dateLeft, dateRight, unit) {
36
35
  return weekStartLeft === weekStartRight;
37
36
  }
38
37
  case "day": {
39
- const yl = dateLeft.getUTCFullYear();
40
- const yr = dateRight.getUTCFullYear();
41
- if (yl !== yr)
42
- return false;
43
- const ml = dateLeft.getUTCMonth();
44
- const mr = dateRight.getUTCMonth();
45
- return ml === mr && dateLeft.getUTCDate() === dateRight.getUTCDate();
38
+ return dateLeft.getUTCFullYear() === dateRight.getUTCFullYear() &&
39
+ dateLeft.getUTCMonth() === dateRight.getUTCMonth() &&
40
+ dateLeft.getUTCDate() === dateRight.getUTCDate();
46
41
  }
47
42
  case "hour": {
48
- const yl = dateLeft.getUTCFullYear();
49
- const yr = dateRight.getUTCFullYear();
50
- if (yl !== yr)
51
- return false;
52
- const ml = dateLeft.getUTCMonth();
53
- const mr = dateRight.getUTCMonth();
54
- if (ml !== mr)
55
- return false;
56
- const dl = dateLeft.getUTCDate();
57
- const dr = dateRight.getUTCDate();
58
- return dl === dr && dateLeft.getUTCHours() === dateRight.getUTCHours();
43
+ return dateLeft.getUTCFullYear() === dateRight.getUTCFullYear() &&
44
+ dateLeft.getUTCMonth() === dateRight.getUTCMonth() &&
45
+ dateLeft.getUTCDate() === dateRight.getUTCDate() &&
46
+ dateLeft.getUTCHours() === dateRight.getUTCHours();
59
47
  }
60
48
  case "minute": {
61
- const yl = dateLeft.getUTCFullYear();
62
- const yr = dateRight.getUTCFullYear();
63
- if (yl !== yr)
64
- return false;
65
- const ml = dateLeft.getUTCMonth();
66
- const mr = dateRight.getUTCMonth();
67
- if (ml !== mr)
68
- return false;
69
- const dl = dateLeft.getUTCDate();
70
- const dr = dateRight.getUTCDate();
71
- if (dl !== dr)
72
- return false;
73
- const hl = dateLeft.getUTCHours();
74
- const hr = dateRight.getUTCHours();
75
- return hl === hr &&
49
+ return dateLeft.getUTCFullYear() === dateRight.getUTCFullYear() &&
50
+ dateLeft.getUTCMonth() === dateRight.getUTCMonth() &&
51
+ dateLeft.getUTCDate() === dateRight.getUTCDate() &&
52
+ dateLeft.getUTCHours() === dateRight.getUTCHours() &&
76
53
  dateLeft.getUTCMinutes() === dateRight.getUTCMinutes();
77
54
  }
78
55
  case "second": {
79
- const yl = dateLeft.getUTCFullYear();
80
- const yr = dateRight.getUTCFullYear();
81
- if (yl !== yr)
82
- return false;
83
- const ml = dateLeft.getUTCMonth();
84
- const mr = dateRight.getUTCMonth();
85
- if (ml !== mr)
86
- return false;
87
- const dl = dateLeft.getUTCDate();
88
- const dr = dateRight.getUTCDate();
89
- if (dl !== dr)
90
- return false;
91
- const hl = dateLeft.getUTCHours();
92
- const hr = dateRight.getUTCHours();
93
- if (hl !== hr)
94
- return false;
95
- const minl = dateLeft.getUTCMinutes();
96
- const minr = dateRight.getUTCMinutes();
97
- return minl === minr &&
56
+ return dateLeft.getUTCFullYear() === dateRight.getUTCFullYear() &&
57
+ dateLeft.getUTCMonth() === dateRight.getUTCMonth() &&
58
+ dateLeft.getUTCDate() === dateRight.getUTCDate() &&
59
+ dateLeft.getUTCHours() === dateRight.getUTCHours() &&
60
+ dateLeft.getUTCMinutes() === dateRight.getUTCMinutes() &&
98
61
  dateLeft.getUTCSeconds() === dateRight.getUTCSeconds();
99
62
  }
100
63
  default:
@@ -16,7 +16,7 @@ const monthsCache = new Map();
16
16
  */
17
17
  function months(format = "long", opt = {}) {
18
18
  const locale = opt.locale ?? config_js_1.config.locale;
19
- const key = `${locale}|${format}`;
19
+ const key = locale + "|" + format;
20
20
  const cached = monthsCache.get(key);
21
21
  if (cached)
22
22
  return cached;
@@ -1 +1 @@
1
- {"version":3,"file":"parse-date.d.ts","sourceRoot":"","sources":["../../src/lib/parse-date.ts"],"names":[],"mappings":"AAGA,KAAK,gBAAgB,GAAG;IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,EAAE,CAAC,EAAE,MAAM,CAAC;CACb,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,SAAS,CACvB,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,MAAM,GAAG,gBAAgB,GAClC,IAAI,CA6FN"}
1
+ {"version":3,"file":"parse-date.d.ts","sourceRoot":"","sources":["../../src/lib/parse-date.ts"],"names":[],"mappings":"AAGA,KAAK,gBAAgB,GAAG;IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,EAAE,CAAC,EAAE,MAAM,CAAC;CACb,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,SAAS,CACvB,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,MAAM,GAAG,gBAAgB,GAClC,IAAI,CA8FN"}
@@ -90,13 +90,14 @@ function parseDate(dateString, options) {
90
90
  if (!match) {
91
91
  throw new Error("Invalid date");
92
92
  }
93
- // Extract values in order
94
- const getValue = (token) => {
95
- const index = tokenOrder.indexOf(token);
96
- return index !== -1 ? parseInt(match[index + 1], 10) : 0;
93
+ // Extract values in order - cache indices for efficiency
94
+ const getIdx = (t) => tokenOrder.indexOf(t);
95
+ const getVal = (t) => {
96
+ const i = getIdx(t);
97
+ return i !== -1 ? parseInt(match[i + 1], 10) : 0;
97
98
  };
98
99
  // Create date (months are 0-indexed)
99
- const date = new Date(Date.UTC(getValue("YYYY") || 1970, (getValue("MM") || 1) - 1, getValue("DD") || 1, getValue("HH"), getValue("mm"), getValue("ss")));
100
+ const date = new Date(Date.UTC(getVal("YYYY") || 1970, (getVal("MM") || 1) - 1, getVal("DD") || 1, getVal("HH"), getVal("mm"), getVal("ss")));
100
101
  if (isNaN(date.getTime())) {
101
102
  throw new Error("Invalid date");
102
103
  }
@@ -1 +1 @@
1
- {"version":3,"file":"set-unit.d.ts","sourceRoot":"","sources":["../../src/lib/set-unit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,KAAK,cAAc,GAAG;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,wBAAgB,OAAO,CACrB,IAAI,EAAE,IAAI,EACV,GAAG,EAAE,cAAc,GAClB,IAAI,CAYN"}
1
+ {"version":3,"file":"set-unit.d.ts","sourceRoot":"","sources":["../../src/lib/set-unit.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,KAAK,cAAc,GAAG;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,wBAAgB,OAAO,CACrB,IAAI,EAAE,IAAI,EACV,GAAG,EAAE,cAAc,GAClB,IAAI,CAaN"}
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setUnit = setUnit;
4
4
  function setUnit(date, opt) {
5
5
  const result = new Date(date.getTime());
6
+ if (opt.day !== undefined)
7
+ result.setUTCDate(1);
6
8
  if (opt.year !== undefined)
7
9
  result.setUTCFullYear(opt.year);
8
10
  if (opt.month !== undefined)
@@ -1 +1 @@
1
- {"version":3,"file":"subtract-time.d.ts","sourceRoot":"","sources":["../../src/lib/subtract-time.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,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,UAAU,GAAG,IAAI,CAa9D"}
1
+ {"version":3,"file":"subtract-time.d.ts","sourceRoot":"","sources":["../../src/lib/subtract-time.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,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,UAAU,GAAG,IAAI,CAW9D"}
@@ -14,22 +14,14 @@ const add_time_js_1 = require("./add-time.js");
14
14
  * console.log(newDate.toISOString()); // '2023-02-28T12:00:00.000Z'
15
15
  */
16
16
  function subtractTime(date, opt) {
17
- const out = {};
18
- if (opt.years)
19
- out.years = -opt.years;
20
- if (opt.months)
21
- out.months = -opt.months;
22
- if (opt.weeks)
23
- out.weeks = -opt.weeks;
24
- if (opt.days)
25
- out.days = -opt.days;
26
- if (opt.hours)
27
- out.hours = -opt.hours;
28
- if (opt.minutes)
29
- out.minutes = -opt.minutes;
30
- if (opt.seconds)
31
- out.seconds = -opt.seconds;
32
- if (opt.milliseconds)
33
- out.milliseconds = -opt.milliseconds;
34
- return (0, add_time_js_1.addTime)(date, out);
17
+ return (0, add_time_js_1.addTime)(date, {
18
+ years: opt.years ? -opt.years : undefined,
19
+ months: opt.months ? -opt.months : undefined,
20
+ weeks: opt.weeks ? -opt.weeks : undefined,
21
+ days: opt.days ? -opt.days : undefined,
22
+ hours: opt.hours ? -opt.hours : undefined,
23
+ minutes: opt.minutes ? -opt.minutes : undefined,
24
+ seconds: opt.seconds ? -opt.seconds : undefined,
25
+ milliseconds: opt.milliseconds ? -opt.milliseconds : undefined,
26
+ });
35
27
  }
@@ -1 +1 @@
1
- {"version":3,"file":"to-now.d.ts","sourceRoot":"","sources":["../../src/lib/to-now.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAyCxE"}
1
+ {"version":3,"file":"to-now.d.ts","sourceRoot":"","sources":["../../src/lib/to-now.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAwCxE"}
@@ -23,35 +23,35 @@ const config_js_1 = require("./config.js");
23
23
  * ```
24
24
  */
25
25
  function toNow(date, locale = config_js_1.config.locale) {
26
- const now = Date.now();
27
- const diff = date.getTime() - now;
26
+ const diff = date.getTime() - Date.now();
28
27
  const absDiff = Math.abs(diff);
29
28
  const seconds = Math.floor(absDiff / 1000);
30
- const minutes = Math.floor(seconds / 60);
31
- const hours = Math.floor(minutes / 60);
32
- const days = Math.floor(hours / 24);
33
- const weeks = Math.floor(days / 7);
34
- const months = Math.floor(days / 30);
35
- const years = Math.floor(days / 365);
36
29
  // Use auto for "now", always for everything else
37
30
  if (seconds < 60) {
38
31
  return (0, rtf_js_1.getRTF)(locale, "auto").format(0, "second");
39
32
  }
40
33
  const rtf = (0, rtf_js_1.getRTF)(locale, "always");
34
+ const sign = diff > 0 ? 1 : -1;
35
+ const minutes = Math.floor(seconds / 60);
41
36
  if (minutes < 60) {
42
- return rtf.format(diff > 0 ? minutes : -minutes, "minute");
37
+ return rtf.format(sign * minutes, "minute");
43
38
  }
39
+ const hours = Math.floor(minutes / 60);
44
40
  if (hours < 24) {
45
- return rtf.format(diff > 0 ? hours : -hours, "hour");
41
+ return rtf.format(sign * hours, "hour");
46
42
  }
43
+ const days = Math.floor(hours / 24);
47
44
  if (days < 7) {
48
- return rtf.format(diff > 0 ? days : -days, "day");
45
+ return rtf.format(sign * days, "day");
49
46
  }
47
+ const weeks = Math.floor(days / 7);
50
48
  if (days < 30) {
51
- return rtf.format(diff > 0 ? weeks : -weeks, "week");
49
+ return rtf.format(sign * weeks, "week");
52
50
  }
51
+ const months = Math.floor(days / 30);
53
52
  if (months < 12) {
54
- return rtf.format(diff > 0 ? months : -months, "month");
53
+ return rtf.format(sign * months, "month");
55
54
  }
56
- return rtf.format(diff > 0 ? years : -years, "year");
55
+ const years = Math.floor(days / 365);
56
+ return rtf.format(sign * years, "year");
57
57
  }
@@ -1 +1 @@
1
- {"version":3,"file":"week-of-year.d.ts","sourceRoot":"","sources":["../../src/lib/week-of-year.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAkB7C"}
1
+ {"version":3,"file":"week-of-year.d.ts","sourceRoot":"","sources":["../../src/lib/week-of-year.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAK7C"}
@@ -16,15 +16,8 @@ exports.weekOfYear = weekOfYear;
16
16
  * ```
17
17
  */
18
18
  function weekOfYear(date) {
19
- // Get the start of the year
20
19
  const year = date.getUTCFullYear();
21
20
  const startOfYear = new Date(Date.UTC(year, 0, 1));
22
- // Get day of week for start of year (0 = Sunday)
23
- const startDay = startOfYear.getUTCDay();
24
- // Get the date's day of year
25
21
  const dayOfYear = Math.floor((date.getTime() - startOfYear.getTime()) / 86400000) + 1;
26
- // Calculate week number
27
- // Add startDay to align with week boundaries (Sunday = start of week)
28
- const weekNum = Math.ceil((dayOfYear + startDay) / 7);
29
- return weekNum;
22
+ return Math.ceil((dayOfYear + startOfYear.getUTCDay()) / 7);
30
23
  }
@@ -16,7 +16,7 @@ const daysCache = new Map();
16
16
  */
17
17
  function weekdays(format = "long", opt = {}) {
18
18
  const locale = opt.locale ?? config_js_1.config.locale;
19
- const key = `${locale}|${format}`;
19
+ const key = locale + "|" + format;
20
20
  const cached = daysCache.get(key);
21
21
  if (cached)
22
22
  return cached;