Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#48898 update(date-arithmetic): version 4 u…
Browse files Browse the repository at this point in the history
…pdates by @peterblazejewicz

* update(date-arithmetic): version 4 updates

- simplify module definition
- add missing and new methods
- correct 'seconds' constant
- amend tests
- add maintainer

https://github.com/jquense/date-math/releases/tag/v4.1.0

Thanks!

* Update types/date-arithmetic/index.d.ts

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>

* Update types/date-arithmetic/index.d.ts

* Update types/date-arithmetic/index.d.ts

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
  • Loading branch information
peterblazejewicz and sandersn authored Nov 20, 2020
1 parent 09d10b6 commit 0a92f51
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 34 deletions.
41 changes: 38 additions & 3 deletions types/date-arithmetic/date-arithmetic-tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dateArithmetic = require('dateArithmetic');
import dateArithmetic = require('date-arithmetic');
import { add, subtract, eq, neq, gt, gte, lt, lte } from 'date-arithmetic';

dateArithmetic.add(new Date(2010, 7, 23), 2, 'second');
dateArithmetic.add(new Date(2010, 7, 23), 2, 'seconds');
dateArithmetic.add(new Date(2010, 7, 23), 2, 'minutes');
dateArithmetic.add(new Date(2010, 7, 23), 2, 'hours');
dateArithmetic.add(new Date(2010, 7, 23), 2, 'day');
Expand All @@ -10,7 +11,7 @@ dateArithmetic.add(new Date(2010, 7, 23), 2, 'year');
dateArithmetic.add(new Date(2010, 7, 23), 2, 'decade');
dateArithmetic.add(new Date(2010, 7, 23), 2, 'century');

dateArithmetic.subtract(new Date(2010, 7, 30), 1, 'second');
dateArithmetic.subtract(new Date(2010, 7, 30), 1, 'seconds');
dateArithmetic.subtract(new Date(2010, 7, 29), 2, 'minutes');
dateArithmetic.subtract(new Date(2010, 7, 28), 3, 'hours');
dateArithmetic.subtract(new Date(2010, 7, 27), 4, 'day');
Expand All @@ -26,3 +27,37 @@ dateArithmetic.gt(new Date(), new Date());
dateArithmetic.gte(new Date(), new Date());
dateArithmetic.lt(new Date(), new Date());
dateArithmetic.lte(new Date(), new Date());

add(new Date(2010, 7, 23), 2, 'seconds');
subtract(new Date(2010, 7, 30), 1, 'seconds');
eq(new Date(), new Date());
neq(new Date(), new Date());
gt(new Date(), new Date());
gte(new Date(), new Date());
lt(new Date(), new Date());
lte(new Date(), new Date());
dateArithmetic.startOf(new Date(), 'week', 0);
dateArithmetic.startOf(new Date(), 'century');

dateArithmetic.milliseconds(new Date());
dateArithmetic.milliseconds(new Date(), new Date().getMilliseconds());
dateArithmetic.seconds(new Date());
dateArithmetic.seconds(new Date(), new Date().getSeconds());
dateArithmetic.minutes(new Date());
dateArithmetic.minutes(new Date(), new Date().getMinutes());
dateArithmetic.hours(new Date());
dateArithmetic.hours(new Date(), new Date().getHours());
dateArithmetic.date(new Date());
dateArithmetic.date(new Date(), new Date().getDate());
dateArithmetic.day(new Date());
dateArithmetic.day(new Date(), new Date().getDay());
dateArithmetic.weekday(new Date(), new Date());
dateArithmetic.weekday(new Date(), new Date(), 0);
dateArithmetic.month(new Date());
dateArithmetic.month(new Date(), new Date().getMonth());
dateArithmetic.year(new Date());
dateArithmetic.year(new Date(), new Date().getFullYear());
dateArithmetic.decade(new Date());
dateArithmetic.decade(new Date(), 20);
dateArithmetic.century(new Date());
dateArithmetic.century(new Date(), 21);
89 changes: 64 additions & 25 deletions types/date-arithmetic/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,77 @@
// Type definitions for date-arithmetic v3.1.0
// Type definitions for date-arithmetic 4.1
// Project: https://github.com/jquense/date-math
// Definitions by: Sergii Paryzhskyi <https://github.com/HeeL>
// Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

type Unit = 'second' | 'minutes' | 'hours' | 'day' | 'week' | 'month' | 'year' | 'decade' | 'century';
export type Unit =
| 'milliseconds'
| 'seconds'
| 'minutes'
| 'hours'
| 'day'
| 'week'
| 'month'
| 'year'
| 'decade'
| 'century';

/** dateArithmetic Public Instance Methods */
interface dateArithmeticStatic {
/** Add specified amount of units to a provided date and return new date as a result */
add(date: Date, num: number, unit: Unit): Date;
export function milliseconds(date: Date): number;
export function milliseconds(date: Date, value: number): Date;
export function seconds(date: Date): number;
export function seconds(date: Date, value: number): Date;
export function minutes(date: Date): number;
export function minutes(date: Date, value: number): Date;
export function hours(date: Date): number;
export function hours(date: Date, value: number): Date;
export function date(date: Date): number;
export function date(date: Date, value: number): Date;
export function day(date: Date): number;
export function day(date: Date, value: number): Date;
export function weekday(date: Date, value: Date): number;
export function weekday(date: Date, value: Date, firstOfWeek: StartOfWeek): Date;
export function month(date: Date): number;
export function month(date: Date, value: number): Date;
export function year(date: Date): number;
export function year(date: Date, value: number): Date;
export function decade(date: Date): number;
export function decade(date: Date, value: number): Date;
export function century(date: Date): number;
export function century(date: Date, value: number): Date;

/** Subtract specified amount of units from a provided date and return new date as a result */
subtract(date: Date, num: number, unit: Unit): Date;
/** Add specified amount of units to a provided date and return new date as a result */
export function add(date: Date, num: number, unit: Unit): Date;

/** Compare two dates and return true if they are equal */
eq(date: Date, date2: Date): Boolean;
/**
* The opposite of `startOf`
*/
export function endOf(date: Date, unit: Unit): Date;
/**
* Return a new date with the relevent date parts zero'd out.
* You only need to provide a firstOfWeek when the unit is 'week'
*/
export function startOf(date: Date, unit: 'week', firtOfWeek: StartOfWeek): Date;
export function startOf(date: Date, unit: Exclude<Unit, 'week'>): Date;

/** Compare two dates and return false if they are equal */
neq(date: Date, date2: Date): Boolean;
/** Subtract specified amount of units from a provided date and return new date as a result */
export function subtract(date: Date, num: number, unit: Unit): Date;

/** Compare two dates and return true if date is greater than date2 */
gt(date: Date, date2: Date): Boolean;
/** Compare two dates and return true if they are equal */
export function eq(date: Date, date2: Date): boolean;

/** Compare two dates and return true if date is greater or equal than date2 */
gte(date: Date, date2: Date): Boolean;
/** Compare two dates and return false if they are equal */
export function neq(date: Date, date2: Date): boolean;

/** Compare two dates and return true if date is less than date2 */
lt(date: Date, date2: Date): Boolean;
/** Compare two dates and return true if date is greater than date2 */
export function gt(date: Date, date2: Date): boolean;

/** Compare two dates and return true if date is less or equal than date2 */
lte(date: Date, date2: Date): Boolean;
}
/** Compare two dates and return true if date is greater or equal than date2 */
export function gte(date: Date, date2: Date): boolean;

declare module 'dateArithmetic' {
const dateArithmetic: dateArithmeticStatic;
export = dateArithmetic;
}
/** Compare two dates and return true if date is less than date2 */
export function lt(date: Date, date2: Date): boolean;

/** Compare two dates and return true if date is less or equal than date2 */
export function lte(date: Date, date2: Date): boolean;

export type StartOfWeek = 0 | 1 | 2 | 3 | 4 | 5 | 6;
7 changes: 1 addition & 6 deletions types/date-arithmetic/tslint.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
{
"extends": "dtslint/dt.json",
"rules": {
"ban-types": false,
"dt-header": false,
"no-single-declare-module": false
}
"extends": "dtslint/dt.json"
}

0 comments on commit 0a92f51

Please sign in to comment.