Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(date): don't allow parameters <= 0 #1536

Merged
merged 7 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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