nhb-toolbox 3.6.0 → 3.6.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.
@@ -107,32 +107,56 @@ export declare class Chronos {
107
107
  * @returns The difference as string, e.g., `2 years 1 month 9 days 18 hours 56 minutes 9 seconds ago`.
108
108
  */
109
109
  fromNow(time?: string | number | Date | Chronos): string;
110
- /** * Returns the number of full years between the input date and now. */
111
- getRelativeYear(): number;
112
- /** * Returns the number of full months between the input date and now. */
113
- getRelativeMonth(): number;
110
+ /**
111
+ * * Returns the number of full years between the input date and now.
112
+ * @param time Optional time to compare with the date/time.
113
+ * @returns The difference in number.
114
+ */
115
+ getRelativeYear(time?: string | number | Date | Chronos): number;
116
+ /**
117
+ * * Returns the number of full months between the input date and now.
118
+ * @param time Optional time to compare with the date/time.
119
+ * @returns The difference in number.
120
+ */
121
+ getRelativeMonth(time?: string | number | Date | Chronos): number;
114
122
  /**
115
123
  * * Determines if the given date is today, tomorrow, yesterday or any relative day.
116
124
  * @param date - The date to compare (Date object).
125
+ * @param time Optional time to compare with the date/time.
117
126
  * @returns
118
127
  * - `-1` if the date is yesterday.
119
128
  * - `0` if the date is today.
120
129
  * - `1` if the date is tomorrow.
121
130
  * - Other positive or negative numbers for other relative days (e.g., `-2` for two days ago, `2` for two days ahead).
122
131
  */
123
- getRelativeDay(): number;
124
- /** * Returns the number of full hours between the input date and now. */
125
- getRelativeHour(): number;
126
- /** * Returns the number of full minutes between the input date and now. */
127
- getRelativeMinute(): number;
128
- /** * Returns the number of full seconds between the input date and now. */
129
- getRelativeSecond(): number;
132
+ getRelativeDay(time?: string | number | Date | Chronos): number;
133
+ /**
134
+ * * Returns the number of full hours between the input date and now.
135
+ * @param time Optional time to compare with the date/time.
136
+ * @returns The difference in number.
137
+ */
138
+ getRelativeHour(time?: string | number | Date | Chronos): number;
139
+ /**
140
+ * * Returns the number of full minutes between the input date and now.
141
+ * @param time Optional time to compare with the date/time.
142
+ * @returns The difference in number.
143
+ */
144
+ getRelativeMinute(time?: string | number | Date | Chronos): number;
145
+ /**
146
+ * * Returns the number of full seconds between the input date and now.
147
+ * @param time Optional time to compare with the date/time.
148
+ * @returns The difference in number.
149
+ */
150
+ getRelativeSecond(time?: string | number | Date | Chronos): number;
130
151
  /** * Returns the number of milliseconds between the input date and now. */
131
- getRelativeMilliSecond(): number;
152
+ getRelativeMilliSecond(time?: string | number | Date | Chronos): number;
132
153
  /**
133
154
  * * Compares the stored date with now, returning the difference in the specified unit.
155
+ *
134
156
  * @param unit The time unit to compare by. Defaults to 'minute'.
157
+ * @param time Optional time to compare with the date/time.
158
+ * @returns The difference in number.
135
159
  */
136
- compare(unit?: TimeUnit): number;
160
+ compare(unit?: TimeUnit, time?: string | number | Date | Chronos): number;
137
161
  }
138
162
  //# 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,EACb,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,MAAM,SAAS,CAAC;AAGjB,qBAAa,OAAO;;IAInB;;;;;;;;OAQG;gBACS,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAepD,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,MAAM,CAEjC;IAED;;;;OAIG;IACH,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAKnD,4EAA4E;IAC5E,MAAM,IAAI,MAAM;IAIhB,6EAA6E;IAC7E,OAAO,IAAI,MAAM;IAIjB,qDAAqD;IACrD,MAAM,IAAI,IAAI;IAId,mGAAmG;IACnG,QAAQ,IAAI,MAAM;IAIlB,wDAAwD;IACxD,WAAW,IAAI,MAAM;IAIrB,oFAAoF;IACpF,YAAY,IAAI,MAAM;IAItB,mDAAmD;I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,EACzB,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO,GACrC,MAAM;CAoBT"}
@@ -14,13 +14,7 @@ export class Chronos {
14
14
  * - If a Chronos object is provided, it will be converted to a Date object.
15
15
  */
16
16
  constructor(value) {
17
- const date = value instanceof Chronos ?
18
- value.toDate()
19
- : new Date(value ?? Date.now());
20
- // Check if the date is invalid
21
- if (isNaN(date.getTime())) {
22
- throw new Error('Provided date is invalid!');
23
- }
17
+ const date = this.#toNewDate(value);
24
18
  this.#date = date;
25
19
  // this.preview = this.toISOString();
26
20
  }
@@ -99,6 +93,16 @@ export class Chronos {
99
93
  static now() {
100
94
  return Date.now();
101
95
  }
96
+ #toNewDate(value) {
97
+ const date = value instanceof Chronos ?
98
+ value.toDate()
99
+ : new Date(value ?? Date.now());
100
+ // Check if the date is invalid
101
+ if (isNaN(date.getTime())) {
102
+ throw new Error('Provided date is invalid!');
103
+ }
104
+ return date;
105
+ }
102
106
  /**
103
107
  * @private Formats the current `Chronos` date using the specified template.
104
108
  *
@@ -255,13 +259,7 @@ export class Chronos {
255
259
  * @returns The difference as string, e.g., `2 years 1 month 9 days 18 hours 56 minutes 9 seconds ago`.
256
260
  */
257
261
  fromNow(time) {
258
- const now = time instanceof Chronos ?
259
- time.toDate()
260
- : new Date(time ?? Date.now());
261
- // Check if the date is invalid
262
- if (isNaN(now.getTime())) {
263
- throw new Error('Provided date is invalid!');
264
- }
262
+ const now = this.#toNewDate(time);
265
263
  const target = this.#date;
266
264
  const isFuture = target > now;
267
265
  const from = isFuture ? now : target;
@@ -309,23 +307,31 @@ export class Chronos {
309
307
  parts.push(`${seconds} second${seconds !== 1 ? 's' : ''}`);
310
308
  return `${isFuture ? 'in ' : ''}${parts.join(' ')}${isFuture ? '' : ' ago'}`;
311
309
  }
312
- /** * Returns the number of full years between the input date and now. */
313
- getRelativeYear() {
314
- const now = new Date();
315
- let years = now.getFullYear() - this.#date.getFullYear();
316
- const hasNotHadBirthday = now.getMonth() < this.#date.getMonth() ||
310
+ /**
311
+ * * Returns the number of full years between the input date and now.
312
+ * @param time Optional time to compare with the date/time.
313
+ * @returns The difference in number.
314
+ */
315
+ getRelativeYear(time) {
316
+ const now = this.#toNewDate(time);
317
+ let years = this.#date.getFullYear() - now.getFullYear();
318
+ const noYearMonthDay = now.getMonth() < this.#date.getMonth() ||
317
319
  (now.getMonth() === this.#date.getMonth() &&
318
320
  now.getDate() < this.#date.getDate());
319
- if (hasNotHadBirthday) {
321
+ if (noYearMonthDay) {
320
322
  years--;
321
323
  }
322
324
  return years;
323
325
  }
324
- /** * Returns the number of full months between the input date and now. */
325
- getRelativeMonth() {
326
- const now = new Date();
327
- let months = (now.getFullYear() - this.#date.getFullYear()) * 12 +
328
- (now.getMonth() - this.#date.getMonth());
326
+ /**
327
+ * * Returns the number of full months between the input date and now.
328
+ * @param time Optional time to compare with the date/time.
329
+ * @returns The difference in number.
330
+ */
331
+ getRelativeMonth(time) {
332
+ const now = this.#toNewDate(time);
333
+ let months = (this.#date.getFullYear() - now.getFullYear()) * 12 +
334
+ (this.#date.getMonth() - now.getMonth());
329
335
  const hasNotHadMonthDay = now.getDate() < this.#date.getDate();
330
336
  if (hasNotHadMonthDay) {
331
337
  months--;
@@ -335,14 +341,15 @@ export class Chronos {
335
341
  /**
336
342
  * * Determines if the given date is today, tomorrow, yesterday or any relative day.
337
343
  * @param date - The date to compare (Date object).
344
+ * @param time Optional time to compare with the date/time.
338
345
  * @returns
339
346
  * - `-1` if the date is yesterday.
340
347
  * - `0` if the date is today.
341
348
  * - `1` if the date is tomorrow.
342
349
  * - Other positive or negative numbers for other relative days (e.g., `-2` for two days ago, `2` for two days ahead).
343
350
  */
344
- getRelativeDay() {
345
- const today = new Date();
351
+ getRelativeDay(time) {
352
+ const today = this.#toNewDate(time);
346
353
  // Set the time of today to 00:00:00 for comparison purposes
347
354
  today.setHours(0, 0, 0, 0);
348
355
  // Normalize the input date to 00:00:00
@@ -352,45 +359,60 @@ export class Chronos {
352
359
  const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
353
360
  return diffDays;
354
361
  }
355
- /** * Returns the number of full hours between the input date and now. */
356
- getRelativeHour() {
357
- const diff = new Date().getTime() - this.#date.getTime();
362
+ /**
363
+ * * Returns the number of full hours between the input date and now.
364
+ * @param time Optional time to compare with the date/time.
365
+ * @returns The difference in number.
366
+ */
367
+ getRelativeHour(time) {
368
+ const diff = this.#date.getTime() - this.#toNewDate(time).getTime();
358
369
  return Math.floor(diff / (1000 * 60 * 60));
359
370
  }
360
- /** * Returns the number of full minutes between the input date and now. */
361
- getRelativeMinute() {
362
- const diff = new Date().getTime() - this.#date.getTime();
371
+ /**
372
+ * * Returns the number of full minutes between the input date and now.
373
+ * @param time Optional time to compare with the date/time.
374
+ * @returns The difference in number.
375
+ */
376
+ getRelativeMinute(time) {
377
+ const diff = this.#date.getTime() - this.#toNewDate(time).getTime();
363
378
  return Math.floor(diff / (1000 * 60));
364
379
  }
365
- /** * Returns the number of full seconds between the input date and now. */
366
- getRelativeSecond() {
367
- const diff = new Date().getTime() - this.#date.getTime();
380
+ /**
381
+ * * Returns the number of full seconds between the input date and now.
382
+ * @param time Optional time to compare with the date/time.
383
+ * @returns The difference in number.
384
+ */
385
+ getRelativeSecond(time) {
386
+ const diff = this.#date.getTime() - this.#toNewDate(time).getTime();
368
387
  return Math.floor(diff / 1000);
369
388
  }
370
389
  /** * Returns the number of milliseconds between the input date and now. */
371
- getRelativeMilliSecond() {
372
- return new Date().getTime() - this.#date.getTime();
390
+ getRelativeMilliSecond(time) {
391
+ return this.#date.getTime() - this.#toNewDate(time).getTime();
373
392
  }
374
393
  /**
375
394
  * * Compares the stored date with now, returning the difference in the specified unit.
395
+ *
376
396
  * @param unit The time unit to compare by. Defaults to 'minute'.
397
+ * @param time Optional time to compare with the date/time.
398
+ * @returns The difference in number.
377
399
  */
378
- compare(unit = 'minute') {
400
+ compare(unit = 'minute', time) {
379
401
  switch (unit) {
380
402
  case 'year':
381
- return this.getRelativeYear();
403
+ return this.getRelativeYear(time);
382
404
  case 'month':
383
- return this.getRelativeMonth();
405
+ return this.getRelativeMonth(time);
384
406
  case 'day':
385
- return this.getRelativeDay();
407
+ return this.getRelativeDay(time);
386
408
  case 'hour':
387
- return this.getRelativeHour();
409
+ return this.getRelativeHour(time);
388
410
  case 'minute':
389
- return this.getRelativeMinute();
411
+ return this.getRelativeMinute(time);
390
412
  case 'second':
391
- return this.getRelativeSecond();
413
+ return this.getRelativeSecond(time);
392
414
  case 'millisecond':
393
- return this.getRelativeMilliSecond();
415
+ return this.getRelativeMilliSecond(time);
394
416
  default:
395
417
  throw new Error(`Unsupported time unit: ${unit}`);
396
418
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "3.6.0",
3
+ "version": "3.6.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",