Commit cb50091 1 parent 5e6519b commit cb50091 Copy full SHA for cb50091
File tree 1 file changed +16
-2
lines changed
1 file changed +16
-2
lines changed Original file line number Diff line number Diff line change 39
39
40
40
}
41
41
42
- const ObjectId = ( rnd = r16 => Math . floor ( r16 ) . toString ( 16 ) ) =>
43
- rnd ( Date . now ( ) / 1000 ) + ' ' . repeat ( 16 ) . replace ( / ./ g, ( ) => rnd ( Math . random ( ) * 16 ) ) ;
42
+ const ObjectId = ( id ) => {
43
+ // Define the rnd function
44
+ const rnd = ( r16 ) => Math . floor ( r16 ) . toString ( 16 ) ;
45
+
46
+ if ( id === undefined ) {
47
+ // If id is undefined, generate a new ObjectId
48
+ return rnd ( Date . now ( ) / 1000 ) + '0' . repeat ( 16 ) . replace ( / ./ g, ( ) => rnd ( Math . random ( ) * 16 ) ) ;
49
+ } else {
50
+ // Check if the provided id is a valid ObjectId
51
+ const validIdRegex = / ^ [ 0 - 9 a - f A - F ] { 24 } $ / ;
52
+ if ( ! validIdRegex . test ( id ) ) {
53
+ throw new Error ( 'Invalid ObjectId' ) ;
54
+ }
55
+ return id ; // Return the valid ObjectId as a string
56
+ }
57
+ } ;
44
58
45
59
function checkValue ( value ) {
46
60
if ( / { { \s * ( [ \w \W ] + ) \s * } } / g. test ( value ) )
You can’t perform that action at this time.
0 commit comments