Skip to content

Commit

Permalink
refactor: rewrite add methods using conditional types
Browse files Browse the repository at this point in the history
  • Loading branch information
standy committed Feb 12, 2020
1 parent c332ecc commit 2062172
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
72 changes: 43 additions & 29 deletions src/add/add-unit.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
import {AddUnitFn, MS} from '../utils/basic-types';
import {MS} from '../utils/basic-types';
import {ValidDate} from '../valid-date';

function addTimeFn(ms: number): AddUnitFn {
return (d: Date | null, n: number): /*ValidDate | Date | null*/ any => {
if (d === null) return null;
if (!isFinite(n)) return d;
return new Date(d.getTime() + ms * n);
};
export function addMilliseconds<T extends ValidDate | Date | null>(d: T, n: number): T {
if (d === null) return null as any;
if (!isFinite(n)) return d;
return new Date(d.getTime() + n) as T;
}

function addFn(keyGet: keyof Date, keySet: keyof Date): AddUnitFn {
return (d: Date | null, n: number): /*ValidDate | Date | null*/ any => {
if (d === null) return null;
if (!isFinite(n)) return d;
const result = new Date(d.getTime());
result[keySet as 'setDate'](result[keyGet as 'getDate']() + n);
return result;
};
export function addSeconds<T extends ValidDate | Date | null>(d: T, n: number): T {
if (d === null) return null as any;
if (!isFinite(n)) return d;
return new Date(d.getTime() + MS.Seconds * n) as T;
}

export const addMilliseconds = addTimeFn(MS.Milliseconds);
export const addSeconds = addTimeFn(MS.Seconds);
export const addMinutes = addTimeFn(MS.Minutes);
export const addHours = addTimeFn(MS.Hours);
export function addMinutes<T extends ValidDate | Date | null>(d: T, n: number): T {
if (d === null) return null as any;
if (!isFinite(n)) return d;
return new Date(d.getTime() + MS.Minutes * n) as T;
}

export function addHours<T extends ValidDate | Date | null>(d: T, n: number): T {
if (d === null) return null as any;
if (!isFinite(n)) return d;
return new Date(d.getTime() + MS.Hours * n) as T;
}

export const addDate = addFn('getDate', 'setDate');
export const addMonth = addFn('getMonth', 'setMonth');
export const addYear = addFn('getFullYear', 'setFullYear');
export function addDate<T extends ValidDate | Date | null>(d: T, n: number): T {
if (d === null) return null as any;
if (!isFinite(n)) return d;
const result = new Date(d.getTime());
result.setDate(result.getDate() + n);
return result as T;
}

export const addUTCMilliseconds = addFn('getUTCMilliseconds', 'setUTCMilliseconds');
export const addUTCSeconds = addFn('getUTCSeconds', 'setUTCSeconds');
export const addUTCMinutes = addFn('getUTCMinutes', 'setUTCMinutes');
export const addUTCHours = addFn('getUTCHours', 'setUTCHours');
export const addUTCDate = addFn('getUTCDate', 'setUTCDate');
export const addUTCMonth = addFn('getUTCMonth', 'setUTCMonth');
export const addUTCYear = addFn('getUTCFullYear', 'setUTCFullYear');
export function addMonth<T extends ValidDate | Date | null>(d: T, n: number): T {
if (d === null) return null as any;
if (!isFinite(n)) return d;
const result = new Date(d.getTime());
result.setMonth(result.getMonth() + n);
return result as T;
}

export function addYear<T extends ValidDate | Date | null>(d: T, n: number): T {
if (d === null) return null as any;
if (!isFinite(n)) return d;
const result = new Date(d.getTime());
result.setFullYear(result.getFullYear() + n);
return result as T;
}
4 changes: 0 additions & 4 deletions src/utils/basic-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ export const enum Month {

export type DiffUnitFn<T> = number | (T extends ValidDate ? number : null);

export interface AddUnitFn {
<T extends ValidDate | Date | null>(d: T, arg: number): T;
}

export interface FormatFn {
(d: ValidDate): string;
(d: null): null;
Expand Down

0 comments on commit 2062172

Please sign in to comment.