diff --git a/src/internal/toDate.ts b/src/internal/toDate.ts new file mode 100644 index 00000000000..ea91cc8fd0d --- /dev/null +++ b/src/internal/toDate.ts @@ -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; +} diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index 23cf6222bbf..49d8a2a04b9 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -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. diff --git a/src/modules/security/index.ts b/src/modules/security/index.ts index 18291066ffc..550e37c5362 100644 --- a/src/modules/security/index.ts +++ b/src/modules/security/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { toDate } from '../../internal/toDate'; export interface Cvss { score: number; @@ -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('-');