nhb-toolbox 3.4.0 → 3.4.2

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,16 +1,26 @@
1
1
  export declare class Chronos {
2
- private readonly _date;
2
+ private readonly date;
3
3
  /**
4
4
  * * Creates a new immutable Chronos instance.
5
- * @param value - A date value (`timestamp`, `string`, or `Date`).
5
+ * @param value - A date value (`timestamp`, `string`, `Date`, `Chronos`).
6
6
  */
7
- constructor(value?: number | string | Date);
7
+ constructor(value?: number | string | Date | Chronos);
8
+ /** * Gets the native `Date` instance (read-only). */
9
+ toDate(): Date;
10
+ /** * Returns a string representation of a date. The format of the string depends on the locale. */
11
+ toString(): string;
12
+ /** * Returns a date as a string value in ISO format. */
13
+ toISOString(): string;
14
+ /** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
15
+ getTimeStamp(): number;
16
+ /** * Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC). */
17
+ static now(): number;
8
18
  /**
9
19
  * * Formats the date into a custom string format.
10
- * @param format - The desired format (e.g., "YYYY-MM-DD").
11
- * @returns Formatted date string.
20
+ * @param format - The desired format (Default format is `DD-MM-YYYY` = `30-06-1995`).
21
+ * @returns Formatted date string in desired format.
12
22
  */
13
- format(format: string): string;
23
+ format(format?: string): string;
14
24
  /**
15
25
  * * Adds days and returns a new immutable instance.
16
26
  * @param days - Number of days to add.
@@ -23,7 +33,22 @@ export declare class Chronos {
23
33
  * @returns A new `Chronos` instance with the updated date.
24
34
  */
25
35
  subtractDays(days: number): Chronos;
26
- /** * Gets the native `Date` instance (read-only). */
27
- toDate(): Date;
36
+ /**
37
+ * * Determines if the given date is today, tomorrow, yesterday or any relative day.
38
+ * @param date - The date to compare (Date object).
39
+ * @returns
40
+ * - `-1` if the date is yesterday.
41
+ * - `0` if the date is today.
42
+ * - `1` if the date is tomorrow.
43
+ * - Other positive or negative numbers for other relative days (e.g., `-2` for two days ago, `2` for two days ahead).
44
+ */
45
+ getRelativeDay(): number;
46
+ /**
47
+ * * Checks if the year is a leap year.
48
+ * - A year is a leap year if it is divisible by 4, but not divisible by 100, unless it is also divisible by 400.
49
+ * - For example, 2000 and 2400 are leap years, but 1900 and 2100 are not.
50
+ * @returns `true` if the year is a leap year, `false` otherwise.
51
+ */
52
+ isLeapYear(): boolean;
28
53
  }
29
54
  //# sourceMappingURL=Chronos.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Chronos.d.ts","sourceRoot":"","sources":["../../src/date/Chronos.ts"],"names":[],"mappings":"AAGA,qBAAa,OAAO;IACnB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAO;IAE7B;;;OAGG;gBACS,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAW1C;;;;OAIG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IA2E9B;;;;OAIG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAM9B;;;;OAIG;IACH,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAInC,qDAAqD;IACrD,MAAM,IAAI,IAAI;CAwBd"}
1
+ {"version":3,"file":"Chronos.d.ts","sourceRoot":"","sources":["../../src/date/Chronos.ts"],"names":[],"mappings":"AAGA,qBAAa,OAAO;IACnB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAO;IAE5B;;;OAGG;gBACS,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAcpD,qDAAqD;IACrD,MAAM,IAAI,IAAI;IAId,mGAAmG;IACnG,QAAQ,IAAI,MAAM;IAIlB,wDAAwD;IACxD,WAAW,IAAI,MAAM;IAIrB,oFAAoF;IACpF,YAAY,IAAI,MAAM;IAItB,qHAAqH;IACrH,MAAM,CAAC,GAAG,IAAI,MAAM;IAIpB;;;;OAIG;IACH,MAAM,CAAC,MAAM,GAAE,MAAqB,GAAG,MAAM;IA2E7C;;;;OAIG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAM9B;;;;OAIG;IACH,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAInC;;;;;;;;OAQG;IACH,cAAc,IAAI,MAAM;IAexB;;;;;OAKG;IACH,UAAU,IAAI,OAAO;CAKrB"}
@@ -1,32 +1,54 @@
1
1
  import { DAYS, MONTHS, sortedFormats } from './constants';
2
2
  export class Chronos {
3
- _date;
3
+ date;
4
4
  /**
5
5
  * * Creates a new immutable Chronos instance.
6
- * @param value - A date value (`timestamp`, `string`, or `Date`).
6
+ * @param value - A date value (`timestamp`, `string`, `Date`, `Chronos`).
7
7
  */
8
8
  constructor(value) {
9
- const date = new Date(value ?? Date.now());
9
+ const date = value instanceof Chronos ?
10
+ value.toDate()
11
+ : new Date(value ?? Date.now());
10
12
  // Check if the date is invalid
11
13
  if (isNaN(date.getTime())) {
12
14
  throw new Error('Invalid date provided!');
13
15
  }
14
- this._date = date;
16
+ this.date = date;
17
+ }
18
+ /** * Gets the native `Date` instance (read-only). */
19
+ toDate() {
20
+ return new Date(this.date);
21
+ }
22
+ /** * Returns a string representation of a date. The format of the string depends on the locale. */
23
+ toString() {
24
+ return this.date.toString();
25
+ }
26
+ /** * Returns a date as a string value in ISO format. */
27
+ toISOString() {
28
+ return this.date.toISOString();
29
+ }
30
+ /** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
31
+ getTimeStamp() {
32
+ return this.date.getTime();
33
+ }
34
+ /** * Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC). */
35
+ static now() {
36
+ return Date.now();
15
37
  }
16
38
  /**
17
39
  * * Formats the date into a custom string format.
18
- * @param format - The desired format (e.g., "YYYY-MM-DD").
19
- * @returns Formatted date string.
40
+ * @param format - The desired format (Default format is `DD-MM-YYYY` = `30-06-1995`).
41
+ * @returns Formatted date string in desired format.
20
42
  */
21
- format(format) {
22
- const year = this._date.getFullYear();
23
- const month = this._date.getMonth();
24
- const day = this._date.getDay();
25
- const date = this._date.getDate();
26
- const hours = this._date.getHours();
27
- const minutes = this._date.getMinutes();
28
- const seconds = this._date.getSeconds();
29
- const milliseconds = this._date.getMilliseconds();
43
+ format(format = 'DD-MM-YYYY') {
44
+ const year = this.date.getFullYear();
45
+ const month = this.date.getMonth();
46
+ const day = this.date.getDay();
47
+ const date = this.date.getDate();
48
+ const hours = this.date.getHours();
49
+ const minutes = this.date.getMinutes();
50
+ const seconds = this.date.getSeconds();
51
+ const milliseconds = this.date.getMilliseconds();
30
52
  const dateComponents = {
31
53
  YYYY: String(year),
32
54
  YY: String(year).slice(-2),
@@ -89,7 +111,7 @@ export class Chronos {
89
111
  * @returns A new `Chronos` instance with the updated date.
90
112
  */
91
113
  addDays(days) {
92
- const newDate = new Date(this._date);
114
+ const newDate = new Date(this.date);
93
115
  newDate.setDate(newDate.getDate() + days);
94
116
  return new Chronos(newDate);
95
117
  }
@@ -101,8 +123,34 @@ export class Chronos {
101
123
  subtractDays(days) {
102
124
  return this.addDays(-days);
103
125
  }
104
- /** * Gets the native `Date` instance (read-only). */
105
- toDate() {
106
- return new Date(this._date);
126
+ /**
127
+ * * Determines if the given date is today, tomorrow, yesterday or any relative day.
128
+ * @param date - The date to compare (Date object).
129
+ * @returns
130
+ * - `-1` if the date is yesterday.
131
+ * - `0` if the date is today.
132
+ * - `1` if the date is tomorrow.
133
+ * - Other positive or negative numbers for other relative days (e.g., `-2` for two days ago, `2` for two days ahead).
134
+ */
135
+ getRelativeDay() {
136
+ const today = new Date();
137
+ // Set the time of today to 00:00:00 for comparison purposes
138
+ today.setHours(0, 0, 0, 0);
139
+ // Normalize the input date to 00:00:00
140
+ const inputDate = new Date(this.date);
141
+ inputDate.setHours(0, 0, 0, 0);
142
+ const diffTime = inputDate.getTime() - today.getTime();
143
+ const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
144
+ return diffDays;
145
+ }
146
+ /**
147
+ * * Checks if the year is a leap year.
148
+ * - A year is a leap year if it is divisible by 4, but not divisible by 100, unless it is also divisible by 400.
149
+ * - For example, 2000 and 2400 are leap years, but 1900 and 2100 are not.
150
+ * @returns `true` if the year is a leap year, `false` otherwise.
151
+ */
152
+ isLeapYear() {
153
+ const year = this.date.getFullYear();
154
+ return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
107
155
  }
108
156
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "3.4.0",
3
+ "version": "3.4.2",
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",