From 0a92f51bc262dc320c39e1ed3d99e5815c3e55b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20B=C5=82a=C5=BCejewicz=20=28Peter=20Blazejewicz=29?= Date: Fri, 20 Nov 2020 22:49:09 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#48898=20update(dat?= =?UTF-8?q?e-arithmetic):=20version=204=20updates=20by=20@peterblazejewicz?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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> --- .../date-arithmetic/date-arithmetic-tests.ts | 41 ++++++++- types/date-arithmetic/index.d.ts | 89 +++++++++++++------ types/date-arithmetic/tslint.json | 7 +- 3 files changed, 103 insertions(+), 34 deletions(-) diff --git a/types/date-arithmetic/date-arithmetic-tests.ts b/types/date-arithmetic/date-arithmetic-tests.ts index b0cf5d85cc6721..3b9c07cb026ffd 100644 --- a/types/date-arithmetic/date-arithmetic-tests.ts +++ b/types/date-arithmetic/date-arithmetic-tests.ts @@ -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'); @@ -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'); @@ -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); diff --git a/types/date-arithmetic/index.d.ts b/types/date-arithmetic/index.d.ts index f168838f325120..ca0c912d498736 100644 --- a/types/date-arithmetic/index.d.ts +++ b/types/date-arithmetic/index.d.ts @@ -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 +// Piotr Błażejewicz // 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): 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; diff --git a/types/date-arithmetic/tslint.json b/types/date-arithmetic/tslint.json index 557cc3f5c67e72..f93cf8562ad24d 100644 --- a/types/date-arithmetic/tslint.json +++ b/types/date-arithmetic/tslint.json @@ -1,8 +1,3 @@ { - "extends": "dtslint/dt.json", - "rules": { - "ban-types": false, - "dt-header": false, - "no-single-declare-module": false - } + "extends": "dtslint/dt.json" }