Skip to content

Commit

Permalink
fix(date): don't allow parameters <= 0 (#1536)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Nov 9, 2022
1 parent 5a09c89 commit dd58148
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
24 changes: 20 additions & 4 deletions src/modules/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 must 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();
Expand All @@ -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 must 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();
Expand Down Expand Up @@ -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 must 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();
Expand All @@ -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 must 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();
Expand Down
32 changes: 23 additions & 9 deletions test/date.spec.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -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 must be greater than 0.')
);
});

it.each(converterMap)(
Expand All @@ -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 must be greater than 0.')
);
});

it.each(converterMap)(
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit dd58148

Please sign in to comment.