Skip to content

Commit

Permalink
chore: refactor to make CVE date range user editable
Browse files Browse the repository at this point in the history
  • Loading branch information
thisislawatts committed Aug 9, 2022
1 parent ac78f58 commit d34d3b0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
14 changes: 14 additions & 0 deletions src/internal/toDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Converts date passed as a string, number or Date to a Date object.
* If nothing or a non parseable value is passed, takes current date.
*
* @param date Date
*/
export function toDate(date?: string | Date | number): Date {
date = new Date(date);
if (isNaN(date.valueOf())) {
date = new Date();
}

return date;
}
16 changes: 1 addition & 15 deletions src/modules/date/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
import type { Faker } from '../..';
import type { DateEntryDefinition } from '../../definitions';
import { FakerError } from '../../errors/faker-error';

/**
* Converts date passed as a string, number or Date to a Date object.
* If nothing or a non parseable value is passed, takes current date.
*
* @param date Date
*/
function toDate(date?: string | Date | number): Date {
date = new Date(date);
if (isNaN(date.valueOf())) {
date = new Date();
}

return date;
}
import { toDate } from '../../internal/toDate';

/**
* Module to generate dates.
Expand Down
20 changes: 15 additions & 5 deletions src/modules/security/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../..';
import { toDate } from '../../internal/toDate';

export interface Cvss {
score: number;
Expand All @@ -18,18 +19,27 @@ export class Security {
}

/**
* Generates a random CVE
* Generates a random CVE between the given boundaries
*
* @param options
* @param options.from The early date boundary
* @param options.to The late date boundary
*
* @example
* faker.security.cve() // 'CVE-2011-0762'
* faker.security.cve({from:'2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z') // 'CVE-2028-0762'
*/
cve(): string {
cve(options?: {
from: string | Date | number;
to: string | Date | number;
}): string {
const fromMs = toDate(options?.from || '1999-01-01T00:00:00.000Z');
const toMs = toDate(options?.to);

return [
'CVE',
// Year
this.faker.date
.between('1999-01-01T00:00:00.000Z', '2022-01-01T00:00:00.000Z')
.getFullYear(),
this.faker.date.between(fromMs, toMs).getFullYear(),
// Sequence in the year
this.faker.random.numeric(5, { allowLeadingZeros: true }),
].join('-');
Expand Down

0 comments on commit d34d3b0

Please sign in to comment.