From e97c0a650506ea6f0d8f173f19c6dd052f53d735 Mon Sep 17 00:00:00 2001 From: Felipe Armoni Date: Mon, 14 Jun 2021 14:48:40 -0300 Subject: [PATCH 1/3] Fixed add function --- src/add/index.ts | 14 ++++++------ src/add/test.ts | 56 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/add/index.ts b/src/add/index.ts index 5a4f019e62..03403f03b3 100644 --- a/src/add/index.ts +++ b/src/add/index.ts @@ -52,13 +52,13 @@ export default function add( if (!duration || typeof duration !== 'object') return new Date(NaN) - const years = 'years' in duration ? toInteger(duration.years) : 0 - const months = 'months' in duration ? toInteger(duration.months) : 0 - const weeks = 'weeks' in duration ? toInteger(duration.weeks) : 0 - const days = 'days' in duration ? toInteger(duration.days) : 0 - const hours = 'hours' in duration ? toInteger(duration.hours) : 0 - const minutes = 'minutes' in duration ? toInteger(duration.minutes) : 0 - const seconds = 'seconds' in duration ? toInteger(duration.seconds) : 0 + const years = duration.years ? toInteger(duration.years) : 0 + const months = duration.months ? toInteger(duration.months) : 0 + const weeks = duration.weeks ? toInteger(duration.weeks) : 0 + const days = duration.days ? toInteger(duration.days) : 0 + const hours = duration.hours ? toInteger(duration.hours) : 0 + const minutes = duration.minutes ? toInteger(duration.minutes) : 0 + const seconds = duration.seconds ? toInteger(duration.seconds) : 0 // Add years and months const date = toDate(dirtyDate) diff --git a/src/add/test.ts b/src/add/test.ts index 8c8d7a91a8..850b0cb161 100644 --- a/src/add/test.ts +++ b/src/add/test.ts @@ -5,8 +5,8 @@ import assert from 'assert' import add from '.' import { getDstTransitions } from '../../test/dst/tzOffsetTransitions' -describe('add', function() { - it('adds the values from the given object', function() { +describe('add', function () { + it('adds the values from the given object', function () { const result = add(new Date(2014, 8 /* Sep */, 1, 10, 19, 50), { years: 2, months: 9, @@ -14,41 +14,67 @@ describe('add', function() { days: 7, hours: 5, minutes: 9, - seconds: 30 + seconds: 30, }) assert.deepStrictEqual(result, new Date(2017, 5 /* June */, 15, 15, 29, 20)) }) - it('returns same date object when passed empty duration values', function() { + it('supports an undefined value in the duration object', function () { + const result = add(new Date(2014, 8 /* Sep */, 1, 10, 19, 50), { + years: undefined, + months: 9, + weeks: 1, + days: 7, + hours: 5, + minutes: 9, + seconds: 30, + }) + assert.deepStrictEqual(result, new Date(2015, 5 /* June */, 15, 15, 29, 20)) + }) + + it('returns same date object when passed empty duration values', function () { + const result = add(new Date(2014, 8 /* Sep */, 1, 10).getTime(), { + years: undefined, + months: undefined, + weeks: undefined, + days: undefined, + hours: undefined, + minutes: undefined, + seconds: undefined, + }) + assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 1, 10)) + }) + + it('returns same date object when passed undefined duration values', function () { const result = add(new Date(2014, 8 /* Sep */, 1, 10).getTime(), {}) assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 1, 10)) }) - it('accepts a timestamp', function() { + it('accepts a timestamp', function () { const result = add(new Date(2014, 8 /* Sep */, 1, 10).getTime(), { - hours: 4 + hours: 4, }) assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 1, 14)) }) - it('converts a fractional number to an integer', function() { + it('converts a fractional number to an integer', function () { const result = add(new Date(2014, 8 /* Sep */, 1, 10), { hours: 4.2 }) assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 1, 14)) }) - it('implicitly converts number arguments', function() { + it('implicitly converts number arguments', function () { // @ts-expect-error const result = add(new Date(2014, 8 /* Sep */, 1, 10), { hours: '4.2' }) assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 1, 14)) }) - it('does not mutate the original date', function() { + it('does not mutate the original date', function () { const date = new Date(2014, 8 /* Sep */, 1, 10) add(date, { hours: 4 }) assert.deepStrictEqual(date, new Date(2014, 8 /* Sep */, 1, 10)) }) - it('works well if the desired month has fewer days and the provided date is in the last day of a month', function() { + it('works well if the desired month has fewer days and the provided date is in the last day of a month', function () { const date = new Date(2014, 11 /* Dec */, 31) const result = add(date, { months: 9 }) assert.deepStrictEqual(result, new Date(2015, 8 /* Sep */, 30)) @@ -61,14 +87,14 @@ describe('add', function() { dstOnly( `works at DST-end boundary in local timezone: ${tz || '(unknown)'}`, - function() { + function () { const date = dstTransitions.end const result = add(date!, { hours: 1 }) assert.deepStrictEqual(result, new Date(date!.getTime() + HOUR)) } ) - it('handles dates before 100 AD', function() { + it('handles dates before 100 AD', function () { const initialDate = new Date(0) initialDate.setFullYear(-1, 10 /* Nov */, 30) initialDate.setHours(0, 0, 0, 0) @@ -79,18 +105,18 @@ describe('add', function() { assert.deepStrictEqual(result, expectedResult) }) - it('returns `Invalid Date` if the given date is invalid', function() { + it('returns `Invalid Date` if the given date is invalid', function () { const result = add(new Date(NaN), { hours: 5 }) assert(result instanceof Date && isNaN(result.getTime())) }) - it('throws RangeError exception if passed Number as duration', function() { + it('throws RangeError exception if passed Number as duration', function () { // @ts-expect-error const result = add(new Date(2014, 8, 1), 'wut') assert(result instanceof Date && isNaN(result.getTime())) }) - it('throws TypeError exception if passed less than 2 arguments', function() { + it('throws TypeError exception if passed less than 2 arguments', function () { // @ts-expect-error assert.throws(add.bind(null), TypeError) // @ts-expect-error From 38bb1ce17b49f95d46c1de263ad222291a84a21a Mon Sep 17 00:00:00 2001 From: Felipe Armoni Date: Mon, 14 Jun 2021 14:53:53 -0300 Subject: [PATCH 2/3] Fixed sub function --- src/sub/index.js | 14 +++++++------- src/sub/test.js | 30 ++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/sub/index.js b/src/sub/index.js index fa5f30f7f1..af551eb501 100644 --- a/src/sub/index.js +++ b/src/sub/index.js @@ -48,13 +48,13 @@ export default function sub(dirtyDate, duration) { if (!duration || typeof duration !== 'object') return new Date(NaN) - const years = 'years' in duration ? toInteger(duration.years) : 0 - const months = 'months' in duration ? toInteger(duration.months) : 0 - const weeks = 'weeks' in duration ? toInteger(duration.weeks) : 0 - const days = 'days' in duration ? toInteger(duration.days) : 0 - const hours = 'hours' in duration ? toInteger(duration.hours) : 0 - const minutes = 'minutes' in duration ? toInteger(duration.minutes) : 0 - const seconds = 'seconds' in duration ? toInteger(duration.seconds) : 0 + const years = duration.years ? toInteger(duration.years) : 0 + const months = duration.months ? toInteger(duration.months) : 0 + const weeks = duration.weeks ? toInteger(duration.weeks) : 0 + const days = duration.days ? toInteger(duration.days) : 0 + const hours = duration.hours ? toInteger(duration.hours) : 0 + const minutes = duration.minutes ? toInteger(duration.minutes) : 0 + const seconds = duration.seconds ? toInteger(duration.seconds) : 0 // Subtract years and months const dateWithoutMonths = subMonths(toDate(dirtyDate), months + years * 12) diff --git a/src/sub/test.js b/src/sub/test.js index 016accc7f0..ae5949a50c 100644 --- a/src/sub/test.js +++ b/src/sub/test.js @@ -13,11 +13,37 @@ describe('sub', () => { days: 7, hours: 5, minutes: 9, - seconds: 30 + seconds: 30, }) assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 10, 19, 50)) }) + it('supports an undefined value in the duration object', function () { + const result = sub(new Date(2017, 5 /* June */, 15, 15, 29, 20), { + years: undefined, + months: 9, + weeks: 1, + days: 7, + hours: 5, + minutes: 9, + seconds: 30, + }) + assert.deepEqual(result, new Date(2016, 8 /* Sep */, 1, 10, 19, 50)) + }) + + it('returns same date object when passed empty duration values', function () { + const result = sub(new Date(2014, 8 /* Sep */, 1, 10).getTime(), { + years: undefined, + months: undefined, + weeks: undefined, + days: undefined, + hours: undefined, + minutes: undefined, + seconds: undefined, + }) + assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 1, 10)) + }) + it('returns same date object when passed empty duration', () => { const result = sub(new Date(2014, 8 /* Sep */, 1, 10).getTime(), {}) assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 10)) @@ -25,7 +51,7 @@ describe('sub', () => { it('accepts a timestamp', () => { const result = sub(new Date(2014, 8 /* Sep */, 1, 14).getTime(), { - hours: 4 + hours: 4, }) assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 10)) }) From 717056a75834d1b34d1b8f5d43d074e3f33275ff Mon Sep 17 00:00:00 2001 From: Tetiana Date: Fri, 18 Jun 2021 17:18:22 +0100 Subject: [PATCH 3/3] Fix table --- src/add/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/add/index.ts b/src/add/index.ts index 03403f03b3..bbad0367e6 100644 --- a/src/add/index.ts +++ b/src/add/index.ts @@ -20,7 +20,7 @@ import { Duration } from '../types' * |----------------|------------------------------------| * | years | Amount of years to be added | * | months | Amount of months to be added | - * | weeks | Amount of weeks to be added | + * | weeks | Amount of weeks to be added | * | days | Amount of days to be added | * | hours | Amount of hours to be added | * | minutes | Amount of minutes to be added |