nhb-toolbox 3.6.4 → 3.6.50

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.
@@ -1,3 +1,4 @@
1
+ import type { LocaleCode } from '../number/types';
1
2
  import type { FormatOptions, TimeUnit, TimeZone, UTCOffSet } from './types';
2
3
  export declare class Chronos {
3
4
  #private;
@@ -28,14 +29,30 @@ export declare class Chronos {
28
29
  toDate(): Date;
29
30
  /** * Returns a string representation of a date. The format of the string depends on the locale. */
30
31
  toString(): string;
32
+ /**
33
+ * * Wrapper over native `toLocaleString`
34
+ * @description Converts a date and time to a string by using the current or specified locale.
35
+ *
36
+ * @param locales A locale string, array of locale strings, Intl.Locale object, or array of Intl.Locale objects that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.
37
+ * @param options An object that contains one or more properties that specify comparison options.
38
+ */
39
+ toLocaleString(locale?: LocaleCode | Intl.Locale | (LocaleCode | Intl.Locale)[], options?: Intl.DateTimeFormatOptions): string;
31
40
  /** * Returns a date as a string value in ISO format. */
32
41
  toISOString(): string;
33
42
  /** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
34
43
  getTimeStamp(): number;
35
- /** * Returns the date value as a `Date` object. */
36
- get date(): Date;
37
44
  /** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
38
45
  get unix(): number;
46
+ get year(): number;
47
+ get month(): number;
48
+ get date(): number;
49
+ get day(): number;
50
+ get hour(): number;
51
+ get minute(): number;
52
+ get second(): number;
53
+ get millisecond(): number;
54
+ /** * ISO weekday: 1 = Monday, 7 = Sunday */
55
+ get isoWeekday(): number;
39
56
  /**
40
57
  * @instance Returns the current date and time in a specified format in local time.
41
58
  * * Default format is dd, `MMM DD, YYYY HH:mm:ss` = `Sun, Apr 06, 2025 16:11:55:379`
@@ -201,7 +218,7 @@ export declare class Chronos {
201
218
  * * Returns a new Chronos instance at the start of a given unit.
202
219
  * @param unit The unit to reset (e.g., year, month, day).
203
220
  */
204
- startOf(unit: TimeUnit): Chronos;
221
+ startOf(unit: TimeUnit | 'week'): Chronos;
205
222
  /**
206
223
  * * Returns a new Chronos instance at the end of a given unit.
207
224
  * @param unit The unit to adjust (e.g., year, month, day).
@@ -248,5 +265,36 @@ export declare class Chronos {
248
265
  * @param unit The unit to compare.
249
266
  */
250
267
  isAfter(other: number | string | Date | Chronos, unit: TimeUnit): boolean;
268
+ /**
269
+ * * Returns a human-readable relative calendar time like "Today at 3:00 PM"
270
+ * @param baseDate Optional base date to compare with.
271
+ */
272
+ calendar(baseDate?: number | string | Date | Chronos): string;
273
+ /** * Returns a short human-readable string like "2h ago", "in 5m" */
274
+ fromNowShort(): string;
275
+ /** * Returns ISO week number */
276
+ getWeek(): number;
277
+ /** * Returns ISO week year */
278
+ getWeekYear(): number;
279
+ /** * Returns day of year (1 - 366) */
280
+ getDayOfYear(): number;
281
+ /**
282
+ * * Checks if the current date is between the given start and end dates.
283
+ *
284
+ * @param start - The start of the range.
285
+ * @param end - The end of the range.
286
+ * @param inclusive - Specifies whether the comparison is inclusive or exclusive:
287
+ * - `'[]'`: inclusive of both start and end (≥ start and ≤ end)
288
+ * - `'[)'`: inclusive of start, exclusive of end (≥ start and < end)
289
+ * - `'(]'`: exclusive of start, inclusive of end (> start and ≤ end)
290
+ * - `'()'`: exclusive of both start and end (> start and < end)
291
+ *
292
+ * @returns `true` if the current date is within the specified range based on the `inclusive` mode.
293
+ */
294
+ isBetween(start: number | string | Date | Chronos, end: number | string | Date | Chronos, inclusive?: '[]' | '[)' | '(]' | '()'): boolean;
295
+ /**
296
+ * Returns number of days in current month
297
+ */
298
+ daysInMonth(): number;
251
299
  }
252
300
  //# sourceMappingURL=Chronos.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Chronos.d.ts","sourceRoot":"","sources":["../../src/date/Chronos.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEX,aAAa,E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ronos {
51
51
  toString() {
52
52
  return this.#date.toString();
53
53
  }
54
+ /**
55
+ * * Wrapper over native `toLocaleString`
56
+ * @description Converts a date and time to a string by using the current or specified locale.
57
+ *
58
+ * @param locales A locale string, array of locale strings, Intl.Locale object, or array of Intl.Locale objects that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.
59
+ * @param options An object that contains one or more properties that specify comparison options.
60
+ */
61
+ toLocaleString(locale, options) {
62
+ return this.#date.toLocaleString(locale, options);
63
+ }
54
64
  /** * Returns a date as a string value in ISO format. */
55
65
  toISOString() {
56
66
  return this.#date.toISOString();
@@ -59,14 +69,39 @@ export class Chronos {
59
69
  getTimeStamp() {
60
70
  return this.#date.getTime();
61
71
  }
62
- /** * Returns the date value as a `Date` object. */
63
- get date() {
64
- return this.#date;
65
- }
66
72
  /** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
67
73
  get unix() {
68
74
  return this.#date.getTime();
69
75
  }
76
+ get year() {
77
+ return this.#date.getFullYear();
78
+ }
79
+ get month() {
80
+ return this.#date.getMonth();
81
+ }
82
+ get date() {
83
+ return this.#date.getDate();
84
+ }
85
+ get day() {
86
+ return this.#date.getDay();
87
+ }
88
+ get hour() {
89
+ return this.#date.getHours();
90
+ }
91
+ get minute() {
92
+ return this.#date.getMinutes();
93
+ }
94
+ get second() {
95
+ return this.#date.getSeconds();
96
+ }
97
+ get millisecond() {
98
+ return this.#date.getMilliseconds();
99
+ }
100
+ /** * ISO weekday: 1 = Monday, 7 = Sunday */
101
+ get isoWeekday() {
102
+ const day = this.day;
103
+ return day === 0 ? 7 : day;
104
+ }
70
105
  /**
71
106
  * @instance Returns the current date and time in a specified format in local time.
72
107
  * * Default format is dd, `MMM DD, YYYY HH:mm:ss` = `Sun, Apr 06, 2025 16:11:55:379`
@@ -519,6 +554,13 @@ export class Chronos {
519
554
  d.setDate(1);
520
555
  d.setHours(0, 0, 0, 0);
521
556
  break;
557
+ case 'week': {
558
+ const day = d.getDay(); // 0 (Sun) - 6 (Sat)
559
+ const diff = (day + 6) % 7; // convert Sunday=0 to 6, Monday=1 to 0, etc.
560
+ d.setDate(d.getDate() - diff);
561
+ d.setHours(0, 0, 0, 0);
562
+ break;
563
+ }
522
564
  case 'day':
523
565
  d.setHours(0, 0, 0, 0);
524
566
  break;
@@ -685,4 +727,114 @@ export class Chronos {
685
727
  return (this.startOf(unit).toDate().getTime() >
686
728
  time.startOf(unit).toDate().getTime());
687
729
  }
730
+ /**
731
+ * * Returns a human-readable relative calendar time like "Today at 3:00 PM"
732
+ * @param baseDate Optional base date to compare with.
733
+ */
734
+ calendar(baseDate) {
735
+ const base = baseDate ? new Chronos(baseDate) : new Chronos();
736
+ const input = this.startOf('day');
737
+ const comparison = base.startOf('day');
738
+ const diff = input.diff(comparison, 'day');
739
+ const timeStr = this.toDate().toLocaleString(undefined, {
740
+ hour: 'numeric',
741
+ minute: '2-digit',
742
+ });
743
+ if (diff === 0)
744
+ return `Today at ${timeStr}`;
745
+ if (diff === 1)
746
+ return `Tomorrow at ${timeStr}`;
747
+ if (diff === -1)
748
+ return `Yesterday at ${timeStr}`;
749
+ return this.toDate().toLocaleString(undefined, {
750
+ month: 'long',
751
+ day: '2-digit',
752
+ year: 'numeric',
753
+ weekday: 'long',
754
+ hour: 'numeric',
755
+ minute: '2-digit',
756
+ });
757
+ }
758
+ /** * Returns a short human-readable string like "2h ago", "in 5m" */
759
+ fromNowShort() {
760
+ const now = new Chronos();
761
+ const diffInSeconds = this.diff(now, 'second');
762
+ const abs = Math.abs(diffInSeconds);
763
+ const suffix = diffInSeconds >= 0 ? 'in ' : '';
764
+ const postfix = diffInSeconds < 0 ? ' ago' : '';
765
+ if (abs < 60) {
766
+ return `${suffix}${Math.floor(abs)}s${postfix}`;
767
+ }
768
+ else if (abs < 3600) {
769
+ return `${suffix}${Math.floor(abs / 60)}m${postfix}`;
770
+ }
771
+ else if (abs < 86400) {
772
+ return `${suffix}${Math.floor(abs / 3600)}h${postfix}`;
773
+ }
774
+ else if (abs < 2592000) {
775
+ return `${suffix}${Math.floor(abs / 86400)}d${postfix}`;
776
+ }
777
+ else if (abs < 31536000) {
778
+ return `${suffix}${Math.floor(abs / 2592000)}mo${postfix}`;
779
+ }
780
+ else {
781
+ return `${suffix}${Math.floor(abs / 31536000)}y${postfix}`;
782
+ }
783
+ }
784
+ /** * Returns ISO week number */
785
+ getWeek() {
786
+ // ISO week starts on Monday
787
+ const target = this.startOf('week').add(3, 'day');
788
+ const firstThursday = new Chronos(new Date(target.year, 0, 4))
789
+ .startOf('week')
790
+ .add(3, 'day');
791
+ const daysDiff = target.diff(firstThursday, 'day');
792
+ const week = Math.floor(daysDiff / 7) + 1;
793
+ return week;
794
+ }
795
+ /** * Returns ISO week year */
796
+ getWeekYear() {
797
+ const d = this.startOf('week').add(3, 'day'); // Thursday of current ISO week
798
+ return d.year;
799
+ }
800
+ /** * Returns day of year (1 - 366) */
801
+ getDayOfYear() {
802
+ const start = new Date(this.year, 0, 1);
803
+ const diff = this.#date.getTime() - start.getTime();
804
+ return Math.floor(diff / 86400000) + 1;
805
+ }
806
+ /**
807
+ * * Checks if the current date is between the given start and end dates.
808
+ *
809
+ * @param start - The start of the range.
810
+ * @param end - The end of the range.
811
+ * @param inclusive - Specifies whether the comparison is inclusive or exclusive:
812
+ * - `'[]'`: inclusive of both start and end (≥ start and ≤ end)
813
+ * - `'[)'`: inclusive of start, exclusive of end (≥ start and < end)
814
+ * - `'(]'`: exclusive of start, inclusive of end (> start and ≤ end)
815
+ * - `'()'`: exclusive of both start and end (> start and < end)
816
+ *
817
+ * @returns `true` if the current date is within the specified range based on the `inclusive` mode.
818
+ */
819
+ isBetween(start, end, inclusive = '()') {
820
+ const s = new Chronos(start).valueOf();
821
+ const e = new Chronos(end).valueOf();
822
+ const t = this.valueOf();
823
+ switch (inclusive) {
824
+ case '[]':
825
+ return t >= s && t <= e;
826
+ case '[)':
827
+ return t >= s && t < e;
828
+ case '(]':
829
+ return t > s && t <= e;
830
+ case '()':
831
+ return t > s && t < e;
832
+ }
833
+ }
834
+ /**
835
+ * Returns number of days in current month
836
+ */
837
+ daysInMonth() {
838
+ return new Date(this.year, this.month + 1, 0).getDate();
839
+ }
688
840
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "3.6.4",
3
+ "version": "3.6.50",
4
4
  "description": "A versatile collection of smart, efficient, and reusable utility functions for everyday development needs.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",