Commit d34d3b0 1 parent ac78f58 commit d34d3b0 Copy full SHA for d34d3b0
File tree 3 files changed +30
-20
lines changed
3 files changed +30
-20
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
import type { Faker } from '../..' ;
2
2
import type { DateEntryDefinition } from '../../definitions' ;
3
3
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' ;
19
5
20
6
/**
21
7
* Module to generate dates.
Original file line number Diff line number Diff line change 1
1
import type { Faker } from '../..' ;
2
+ import { toDate } from '../../internal/toDate' ;
2
3
3
4
export interface Cvss {
4
5
score : number ;
@@ -18,18 +19,27 @@ export class Security {
18
19
}
19
20
20
21
/**
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
22
27
*
23
28
* @example
24
29
* 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'
25
31
*/
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
+
27
39
return [
28
40
'CVE' ,
29
41
// 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 ( ) ,
33
43
// Sequence in the year
34
44
this . faker . random . numeric ( 5 , { allowLeadingZeros : true } ) ,
35
45
] . join ( '-' ) ;
You can’t perform that action at this time.
0 commit comments