Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 9 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.')
);
});
Comment thread
Shinigami92 marked this conversation as resolved.

it.each(converterMap)(
Expand Down