Skip to content

Commit d34d3b0

Browse files
committed
chore: refactor to make CVE date range user editable
1 parent ac78f58 commit d34d3b0

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

src/internal/toDate.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Converts date passed as a string, number or Date to a Date object.
3+
* If nothing or a non parseable value is passed, takes current date.
4+
*
5+
* @param date Date
6+
*/
7+
export function toDate(date?: string | Date | number): Date {
8+
date = new Date(date);
9+
if (isNaN(date.valueOf())) {
10+
date = new Date();
11+
}
12+
13+
return date;
14+
}

src/modules/date/index.ts

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
import type { Faker } from '../..';
22
import type { DateEntryDefinition } from '../../definitions';
33
import { FakerError } from '../../errors/faker-error';
4-
5-
/**
6-
* Converts date passed as a string, number or Date to a Date object.
7-
* If nothing or a non parseable value is passed, takes current date.
8-
*
9-
* @param date Date
10-
*/
11-
function toDate(date?: string | Date | number): Date {
12-
date = new Date(date);
13-
if (isNaN(date.valueOf())) {
14-
date = new Date();
15-
}
16-
17-
return date;
18-
}
4+
import { toDate } from '../../internal/toDate';
195

206
/**
217
* Module to generate dates.

src/modules/security/index.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '../..';
2+
import { toDate } from '../../internal/toDate';
23

34
export interface Cvss {
45
score: number;
@@ -18,18 +19,27 @@ export class Security {
1819
}
1920

2021
/**
21-
* Generates a random CVE
22+
* Generates a random CVE between the given boundaries
23+
*
24+
* @param options
25+
* @param options.from The early date boundary
26+
* @param options.to The late date boundary
2227
*
2328
* @example
2429
* faker.security.cve() // 'CVE-2011-0762'
30+
* faker.security.cve({from:'2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z') // 'CVE-2028-0762'
2531
*/
26-
cve(): string {
32+
cve(options?: {
33+
from: string | Date | number;
34+
to: string | Date | number;
35+
}): string {
36+
const fromMs = toDate(options?.from || '1999-01-01T00:00:00.000Z');
37+
const toMs = toDate(options?.to);
38+
2739
return [
2840
'CVE',
2941
// Year
30-
this.faker.date
31-
.between('1999-01-01T00:00:00.000Z', '2022-01-01T00:00:00.000Z')
32-
.getFullYear(),
42+
this.faker.date.between(fromMs, toMs).getFullYear(),
3343
// Sequence in the year
3444
this.faker.random.numeric(5, { allowLeadingZeros: true }),
3545
].join('-');

0 commit comments

Comments
 (0)