nhb-toolbox 3.4.0 → 3.4.1

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.
@@ -4,13 +4,13 @@ export declare class Chronos {
4
4
  * * Creates a new immutable Chronos instance.
5
5
  * @param value - A date value (`timestamp`, `string`, or `Date`).
6
6
  */
7
- constructor(value?: number | string | Date);
7
+ constructor(value?: number | string | Date | Chronos);
8
8
  /**
9
9
  * * Formats the date into a custom string format.
10
- * @param format - The desired format (e.g., "YYYY-MM-DD").
11
- * @returns Formatted date string.
10
+ * @param format - The desired format (Default format is `DD-MM-YYYY` = `30-06-1995`).
11
+ * @returns Formatted date string in desired format.
12
12
  */
13
- format(format: string): string;
13
+ format(format?: string): string;
14
14
  /**
15
15
  * * Adds days and returns a new immutable instance.
16
16
  * @param days - Number of days to add.
@@ -23,6 +23,16 @@ export declare class Chronos {
23
23
  * @returns A new `Chronos` instance with the updated date.
24
24
  */
25
25
  subtractDays(days: number): Chronos;
26
+ /**
27
+ * * Determines if the given date is today, tomorrow, yesterday or any relative day.
28
+ * @param date - The date to compare (Date object).
29
+ * @returns
30
+ * - `-1` if the date is yesterday.
31
+ * - `0` if the date is today.
32
+ * - `1` if the date is tomorrow.
33
+ * - Other positive or negative numbers for other relative days (e.g., `-2` for two days ago, `2` for two days ahead).
34
+ */
35
+ getRelativeDay(): number;
26
36
  /** * Gets the native `Date` instance (read-only). */
27
37
  toDate(): Date;
28
38
  }
@@ -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,KAAK,CAAO;IAE7B;;;OAGG;gBACS,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAcpD;;;;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,qDAAqD;IACrD,MAAM,IAAI,IAAI;CAGd"}
@@ -6,7 +6,9 @@ export class Chronos {
6
6
  * @param value - A date value (`timestamp`, `string`, or `Date`).
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!');
@@ -15,10 +17,10 @@ export class Chronos {
15
17
  }
16
18
  /**
17
19
  * * Formats the date into a custom string format.
18
- * @param format - The desired format (e.g., "YYYY-MM-DD").
19
- * @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.
20
22
  */
21
- format(format) {
23
+ format(format = 'DD-MM-YYYY') {
22
24
  const year = this._date.getFullYear();
23
25
  const month = this._date.getMonth();
24
26
  const day = this._date.getDay();
@@ -101,6 +103,26 @@ export class Chronos {
101
103
  subtractDays(days) {
102
104
  return this.addDays(-days);
103
105
  }
106
+ /**
107
+ * * Determines if the given date is today, tomorrow, yesterday or any relative day.
108
+ * @param date - The date to compare (Date object).
109
+ * @returns
110
+ * - `-1` if the date is yesterday.
111
+ * - `0` if the date is today.
112
+ * - `1` if the date is tomorrow.
113
+ * - Other positive or negative numbers for other relative days (e.g., `-2` for two days ago, `2` for two days ahead).
114
+ */
115
+ getRelativeDay() {
116
+ const today = new Date();
117
+ // Set the time of today to 00:00:00 for comparison purposes
118
+ today.setHours(0, 0, 0, 0);
119
+ // Normalize the input date to 00:00:00
120
+ const inputDate = new Date(this._date);
121
+ inputDate.setHours(0, 0, 0, 0);
122
+ const diffTime = inputDate.getTime() - today.getTime();
123
+ const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
124
+ return diffDays;
125
+ }
104
126
  /** * Gets the native `Date` instance (read-only). */
105
127
  toDate() {
106
128
  return new Date(this._date);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "3.4.0",
3
+ "version": "3.4.1",
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",