From f006ea42339bb953861a21b576ec99a3a89a6d52 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 7 Nov 2022 18:03:12 +0100 Subject: [PATCH 1/4] fix(date): don't allow parameters <= 0 --- src/modules/date/index.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index 111fc4bae6f..035a2993a07 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -47,10 +47,14 @@ export class DateModule { * @since 2.0.1 */ past(years?: number, refDate?: string | Date | number): Date { + if (years <= 0) { + throw new FakerError('Years should be greater than 0.'); + } + const date = toDate(refDate); const range = { min: 1000, - max: (years || 1) * 365 * 24 * 3600 * 1000, + max: (years ?? 1) * 365 * 24 * 3600 * 1000, }; let past = date.getTime(); @@ -76,10 +80,14 @@ export class DateModule { * @since 2.0.1 */ future(years?: number, refDate?: string | Date | number): Date { + if (years <= 0) { + throw new FakerError('Years should be greater than 0.'); + } + const date = toDate(refDate); const range = { min: 1000, - max: (years || 1) * 365 * 24 * 3600 * 1000, + max: (years ?? 1) * 365 * 24 * 3600 * 1000, }; let future = date.getTime(); @@ -157,10 +165,14 @@ export class DateModule { * @since 2.0.1 */ recent(days?: number, refDate?: string | Date | number): Date { + if (days <= 0) { + throw new FakerError('Days should be greater than 0.'); + } + const date = toDate(refDate); const range = { min: 1000, - max: (days || 1) * 24 * 3600 * 1000, + max: (days ?? 1) * 24 * 3600 * 1000, }; let future = date.getTime(); @@ -186,10 +198,14 @@ export class DateModule { * @since 5.0.0 */ soon(days?: number, refDate?: string | Date | number): Date { + if (days <= 0) { + throw new FakerError('Days should be greater than 0.'); + } + const date = toDate(refDate); const range = { min: 1000, - max: (days || 1) * 24 * 3600 * 1000, + max: (days ?? 1) * 24 * 3600 * 1000, }; let future = date.getTime(); From 0f2ed7ac3f5e01b30287127eac1026a84870551e Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 7 Nov 2022 18:14:45 +0100 Subject: [PATCH 2/4] test: update test expectations --- test/date.spec.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/date.spec.ts b/test/date.spec.ts index d5016e2dc7a..97c92594c0e 100644 --- a/test/date.spec.ts +++ b/test/date.spec.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it } from 'vitest'; -import { faker } from '../src'; +import { faker, FakerError } from '../src'; import { seededTests } from './support/seededRuns'; const converterMap = [ @@ -130,11 +130,11 @@ describe('date', () => { expect(date).greaterThanOrEqual(yearsAgo); }); - it('should return a past date when years 0', () => { + it('should throw an error when years = 0', () => { const refDate = new Date(); - const date = faker.date.past(0, refDate.toISOString()); - - expect(date).lessThan(refDate); + expect(() => faker.date.past(0, refDate.toISOString())).toThrow( + new FakerError('Years should be greater than 0.') + ); }); it.each(converterMap)( @@ -158,11 +158,11 @@ describe('date', () => { expect(date).greaterThan(new Date()); }); - it('should return a future date when years 0', () => { + it('should throw an error when years = 0', () => { const refDate = new Date(); - const date = faker.date.future(0, refDate.toISOString()); - - expect(date).greaterThan(refDate); // date should be after the date given + expect(() => faker.date.future(0, refDate.toISOString())).toThrow( + new FakerError('Years should be greater than 0.') + ); }); it.each(converterMap)( From bf815fe779ff58cc57066d4bf500845a234343c0 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 8 Nov 2022 17:26:04 +0100 Subject: [PATCH 3/4] refactor: reword --- src/modules/date/index.ts | 8 ++++---- test/date.spec.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index 035a2993a07..ea9d15d7768 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -48,7 +48,7 @@ export class DateModule { */ past(years?: number, refDate?: string | Date | number): Date { if (years <= 0) { - throw new FakerError('Years should be greater than 0.'); + throw new FakerError('Years must be greater than 0.'); } const date = toDate(refDate); @@ -81,7 +81,7 @@ export class DateModule { */ future(years?: number, refDate?: string | Date | number): Date { if (years <= 0) { - throw new FakerError('Years should be greater than 0.'); + throw new FakerError('Years must be greater than 0.'); } const date = toDate(refDate); @@ -166,7 +166,7 @@ export class DateModule { */ recent(days?: number, refDate?: string | Date | number): Date { if (days <= 0) { - throw new FakerError('Days should be greater than 0.'); + throw new FakerError('Days must be greater than 0.'); } const date = toDate(refDate); @@ -199,7 +199,7 @@ export class DateModule { */ soon(days?: number, refDate?: string | Date | number): Date { if (days <= 0) { - throw new FakerError('Days should be greater than 0.'); + throw new FakerError('Days must be greater than 0.'); } const date = toDate(refDate); diff --git a/test/date.spec.ts b/test/date.spec.ts index 97c92594c0e..958cc3bdad1 100644 --- a/test/date.spec.ts +++ b/test/date.spec.ts @@ -133,7 +133,7 @@ describe('date', () => { it('should throw an error when years = 0', () => { const refDate = new Date(); expect(() => faker.date.past(0, refDate.toISOString())).toThrow( - new FakerError('Years should be greater than 0.') + new FakerError('Years must be greater than 0.') ); }); @@ -161,7 +161,7 @@ describe('date', () => { it('should throw an error when years = 0', () => { const refDate = new Date(); expect(() => faker.date.future(0, refDate.toISOString())).toThrow( - new FakerError('Years should be greater than 0.') + new FakerError('Years must be greater than 0.') ); }); From bc5a242a73e70fff43abad109310cdd5ee93ab8e Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 8 Nov 2022 22:32:58 +0100 Subject: [PATCH 4/4] test: add missing tests --- test/date.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/date.spec.ts b/test/date.spec.ts index 958cc3bdad1..365e2cab7f5 100644 --- a/test/date.spec.ts +++ b/test/date.spec.ts @@ -218,6 +218,13 @@ describe('date', () => { expect(date).lessThanOrEqual(new Date()); }); + it('should throw an error when days = 0', () => { + const refDate = new Date(); + expect(() => faker.date.recent(0, refDate.toISOString())).toThrow( + new FakerError('Days must be greater than 0.') + ); + }); + it.each(converterMap)( 'should return a date N days from the recent past, starting from refDate', (converter) => { @@ -249,6 +256,13 @@ describe('date', () => { expect(date).greaterThanOrEqual(new Date()); }); + it('should throw an error when days = 0', () => { + const refDate = new Date(); + expect(() => faker.date.soon(0, refDate.toISOString())).toThrow( + new FakerError('Days must be greater than 0.') + ); + }); + it.each(converterMap)( 'should return a date N days from the recent future, starting from refDate', (converter) => {