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.
- package/dist/date/Chronos.d.ts +37 -13
- package/dist/date/Chronos.d.ts.map +1 -1
- package/dist/date/Chronos.js +68 -46
- package/package.json +1 -1
package/dist/date/Chronos.d.ts
CHANGED
|
@@ -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
|
-
/**
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
/**
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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,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IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO,GACrC,MAAM;CAoBT"}
|
package/dist/date/Chronos.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
-
/**
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
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 (
|
|
321
|
+
if (noYearMonthDay) {
|
|
320
322
|
years--;
|
|
321
323
|
}
|
|
322
324
|
return years;
|
|
323
325
|
}
|
|
324
|
-
/**
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
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 =
|
|
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
|
-
/**
|
|
356
|
-
|
|
357
|
-
|
|
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
|
-
/**
|
|
361
|
-
|
|
362
|
-
|
|
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
|
-
/**
|
|
366
|
-
|
|
367
|
-
|
|
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
|
|
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