Skip to content

Commit 8423490

Browse files
committed
refactor: commenting into to create and diff
1 parent e9217d1 commit 8423490

3 files changed

Lines changed: 85 additions & 36 deletions

File tree

src/create/create-ts-date.ts

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import {ValidDate} from '../valid-date';
22

3+
/**
4+
* Type guard for ValidDate
5+
*/
6+
export function isValidDate(d: Date | null): d is ValidDate {
7+
return d !== null && d instanceof Date && isFinite(d.getTime());
8+
}
9+
10+
/**
11+
* Converts to the `ValidDate`, creating new instance of Date
12+
* @deprecated consider use of `isValidDate`
13+
*/
314
export function fromDate(date: Date | number | undefined): ValidDate | null {
415
const d = new Date(+(date as Date));
516
return asValidDateOrNull(d);
617
}
718

19+
/**
20+
* Converts to the `ValidDate`, creating new instance of Date
21+
* @throws Will throw TypeError if input not a valid Date or timestamp
22+
* @deprecated Consider use of `isValidDate`
23+
*/
824
export function fromDateOrThrow(date: Date | number | undefined): ValidDate {
925
const d = new Date(date instanceof Date ? date.getTime() : (date as number));
1026
return asValidDateOrThrow(d, date);
@@ -14,58 +30,57 @@ export function asValidDateOrNull(date: Date): ValidDate | null {
1430
if (isValidDate(date)) return date;
1531
return null;
1632
}
17-
export function asValidDateOrThrow(date: Date, origin: any): ValidDate {
33+
function asValidDateOrThrow(date: Date, origin: any): ValidDate {
1834
if (isValidDate(date)) return date;
1935
throw new TypeError(`Cant parse date from "${origin}"`);
2036
}
2137

22-
export interface NewValidDateFn {
23-
(): ValidDate;
24-
(value: number): ValidDate | null;
25-
(value: string): ValidDate | null;
26-
(
27-
year: number,
28-
month: number,
29-
date?: number,
30-
hours?: number,
31-
minutes?: number,
32-
seconds?: number,
33-
ms?: number,
34-
): ValidDate | null;
35-
}
36-
export const newValidDate = function(...args: Array<number | string>) {
38+
/**
39+
* Create the `ValidDate`
40+
* In case of failure returns `null`
41+
*/
42+
export function newValidDate(): ValidDate;
43+
export function newValidDate(value: number): ValidDate | null;
44+
export function newValidDate(value: string): ValidDate | null;
45+
export function newValidDate(
46+
year: number,
47+
month: number,
48+
date?: number,
49+
hours?: number,
50+
minutes?: number,
51+
seconds?: number,
52+
ms?: number,
53+
): ValidDate | null;
54+
export function newValidDate(...args: Array<number | string | undefined>): ValidDate | null {
3755
const dateArgs: [any, ...any[]] = [undefined];
3856
for (let i = 0; i < arguments.length; i++) {
3957
dateArgs[i + 1] = arguments[i];
4058
}
4159
const result = new (Date.bind.apply(Date, dateArgs) as any)();
4260
return asValidDateOrNull(result);
43-
} as NewValidDateFn;
44-
45-
export interface NewValidDateOrThrowFn {
46-
(): ValidDate;
47-
(value: number): ValidDate;
48-
(value: string): ValidDate;
49-
(
50-
year: number,
51-
month: number,
52-
date?: number,
53-
hours?: number,
54-
minutes?: number,
55-
seconds?: number,
56-
ms?: number,
57-
): ValidDate;
5861
}
5962

60-
export const newValidDateOrThrow = function(...args: Array<number | string>) {
63+
/**
64+
* Create the `ValidDate`
65+
* In case of failure returns `null`
66+
*/
67+
export function newValidDateOrThrow(): ValidDate;
68+
export function newValidDateOrThrow(value: number): ValidDate;
69+
export function newValidDateOrThrow(value: string): ValidDate;
70+
export function newValidDateOrThrow(
71+
year: number,
72+
month: number,
73+
date?: number,
74+
hours?: number,
75+
minutes?: number,
76+
seconds?: number,
77+
ms?: number,
78+
): ValidDate;
79+
export function newValidDateOrThrow(...args: Array<number | string | undefined>): ValidDate {
6180
const dateArgs: [any, ...any[]] = [undefined];
6281
for (let i = 0; i < arguments.length; i++) {
6382
dateArgs[i + 1] = arguments[i];
6483
}
6584
const result = new (Date.bind.apply(Date, dateArgs) as any)();
6685
return asValidDateOrThrow(result, dateArgs);
67-
} as NewValidDateOrThrowFn;
68-
69-
export function isValidDate(d: Date | null): d is ValidDate {
70-
return d !== null && d instanceof Date && isFinite(d.getTime());
7186
}

src/diff/diff-calendar-unit.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,34 @@ import {DiffUnitFn, MS} from '../utils/basic-types';
22
import {isValidDate} from '../create/create-ts-date';
33
import {absFloor} from '../utils/utils';
44

5+
/**
6+
* Returns the number of years between two dates.
7+
* Months, days and time are ignored
8+
* @example
9+
* diffCalendarYear(parseIso('2020-01-01'), parseIso('2019-12-31')) // 1 = 2020 - 2019
10+
*/
511
export function diffCalendarYear<T extends Date | null>(d1: T, d2: T): DiffUnitFn<T> {
612
if (!isValidDate(d1) || !isValidDate(d2)) return null as any;
713
return d1.getFullYear() - d2.getFullYear();
814
}
915

16+
/**
17+
* Returns the number of months between two dates.
18+
* Days and time are ignored
19+
* @example
20+
* diffCalendarMonth(parseIso('2020-05-01'), parseIso('2020-03-31')) // 2 = 5 - 3
21+
*/
1022
export function diffCalendarMonth<T extends Date | null>(d1: T, d2: T): DiffUnitFn<T> {
1123
if (!isValidDate(d1) || !isValidDate(d2)) return null as any;
1224
return (d1.getFullYear() - d2.getFullYear()) * 12 + d1.getMonth() - d2.getMonth();
1325
}
1426

27+
/**
28+
* Returns the number of days between two dates.
29+
* Time is ignored
30+
* @example
31+
* diffCalendarDate(parseIso('2020-05-01'), parseIso('2020-04-20')) // 11 = 10 days in april and 1 in march
32+
*/
1533
export function diffCalendarDate<T extends Date | null>(d1: T, d2: T): DiffUnitFn<T> {
1634
if (!isValidDate(d1) || !isValidDate(d2)) return null as any;
1735
const u1 = Date.UTC(d1.getFullYear(), d1.getMonth(), d1.getDate());

src/diff/diff-unit.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,32 @@ import {MS, DiffUnitFn} from '../utils/basic-types';
22
import {isValidDate} from '../create/create-ts-date';
33
import {absFloor} from '../utils/utils';
44

5+
/**
6+
* The number of milliseconds between two dates
7+
* @example
8+
* diffMilliseconds(parseIso('2020-01-02'), parseIso('2020-01-01')) // 86400000 = milliseconds in one day
9+
*/
510
export function diffMilliseconds<T extends Date | null>(d1: T, d2: T): DiffUnitFn<T> {
611
if (!isValidDate(d1) || !isValidDate(d2)) return null as any;
712
return absFloor(d1.getTime() - d2.getTime());
813
}
914

15+
/**
16+
* The number of seconds between two dates
17+
* @example
18+
* diffSeconds(parseIso('2020-01-02'), parseIso('2020-01-01')) // 86400 = seconds in one day
19+
*/
1020
export function diffSeconds<T extends Date | null>(d1: T, d2: T): DiffUnitFn<T> {
1121
if (!isValidDate(d1) || !isValidDate(d2)) return null as any;
1222
return absFloor((d1.getTime() - d2.getTime()) / MS.Seconds);
1323
}
1424

25+
/**
26+
* The number of minutes between two dates
27+
* @example
28+
* diffMinutes(parseIso('2020-01-02'), parseIso('2020-01-01')) // 1440 = minutes in one day
29+
* diffMinutes(parseIso('2020-01-01T12:30:01'), parseIso('2020-01-01T12:31:00')) // 0 = only 59 seconds left
30+
*/
1531
export function diffMinutes<T extends Date | null>(d1: T, d2: T): DiffUnitFn<T> {
1632
if (!isValidDate(d1) || !isValidDate(d2)) return null as any;
1733
return absFloor((d1.getTime() - d2.getTime()) / MS.Minutes);

0 commit comments

Comments
 (0)