@@ -31,7 +31,7 @@ export class ObjectId {
31
31
_bsontype ! : 'ObjectId' ;
32
32
33
33
/** @internal */
34
- static index = ~ ~ ( Math . random ( ) * 0xffffff ) ;
34
+ static index = Math . floor ( Math . random ( ) * 0xffffff ) ;
35
35
36
36
static cacheHexString : boolean ;
37
37
@@ -43,54 +43,54 @@ export class ObjectId {
43
43
/**
44
44
* Create an ObjectId type
45
45
*
46
- * @param id - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
46
+ * @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
47
47
*/
48
- constructor ( id ?: string | Buffer | number | ObjectIdLike | ObjectId ) {
49
- if ( ! ( this instanceof ObjectId ) ) return new ObjectId ( id ) ;
48
+ constructor ( inputId ?: string | Buffer | number | ObjectIdLike | ObjectId ) {
49
+ if ( ! ( this instanceof ObjectId ) ) return new ObjectId ( inputId ) ;
50
50
51
- // Duck-typing to support ObjectId from different npm packages
52
- if ( id instanceof ObjectId ) {
53
- this [ kId ] = id . id ;
54
- this . __id = id . __id ;
55
- }
56
-
57
- if ( typeof id === 'object' && id && 'id' in id ) {
58
- if ( 'toHexString' in id && typeof id . toHexString === 'function' ) {
59
- this [ kId ] = Buffer . from ( id . toHexString ( ) , 'hex' ) ;
51
+ // workingId is set based on type of input and whether valid id exists for the input
52
+ let workingId ;
53
+ if ( typeof inputId === 'object' && inputId && 'id' in inputId ) {
54
+ if ( typeof inputId . id !== 'string' && ! ArrayBuffer . isView ( inputId . id ) ) {
55
+ throw new BSONTypeError (
56
+ 'Argument passed in must have an id that is of type string or Buffer'
57
+ ) ;
58
+ }
59
+ if ( 'toHexString' in inputId && typeof inputId . toHexString === 'function' ) {
60
+ workingId = Buffer . from ( inputId . toHexString ( ) , 'hex' ) ;
60
61
} else {
61
- this [ kId ] = typeof id . id === 'string' ? Buffer . from ( id . id ) : id . id ;
62
+ workingId = inputId . id ;
62
63
}
64
+ } else {
65
+ workingId = inputId ;
63
66
}
64
67
65
- // The most common use case (blank id, new objectId instance)
66
- if ( id == null || typeof id === 'number' ) {
68
+ // the following cases use workingId to construct an ObjectId
69
+ if ( workingId == null || typeof workingId === 'number' ) {
70
+ // The most common use case (blank id, new objectId instance)
67
71
// Generate a new id
68
- this [ kId ] = ObjectId . generate ( typeof id === 'number' ? id : undefined ) ;
69
- // If we are caching the hex string
70
- if ( ObjectId . cacheHexString ) {
71
- this . __id = this . id . toString ( 'hex' ) ;
72
- }
73
- }
74
-
75
- if ( ArrayBuffer . isView ( id ) && id . byteLength === 12 ) {
76
- this [ kId ] = ensureBuffer ( id ) ;
77
- }
78
-
79
- if ( typeof id === 'string' ) {
80
- if ( id . length === 12 ) {
81
- const bytes = Buffer . from ( id ) ;
72
+ this [ kId ] = ObjectId . generate ( typeof workingId === 'number' ? workingId : undefined ) ;
73
+ } else if ( ArrayBuffer . isView ( workingId ) && workingId . byteLength === 12 ) {
74
+ this [ kId ] = ensureBuffer ( workingId ) ;
75
+ } else if ( typeof workingId === 'string' ) {
76
+ if ( workingId . length === 12 ) {
77
+ const bytes = Buffer . from ( workingId ) ;
82
78
if ( bytes . byteLength === 12 ) {
83
79
this [ kId ] = bytes ;
80
+ } else {
81
+ throw new BSONTypeError ( 'Argument passed in must be a string of 12 bytes' ) ;
84
82
}
85
- } else if ( id . length === 24 && checkForHexRegExp . test ( id ) ) {
86
- this [ kId ] = Buffer . from ( id , 'hex' ) ;
83
+ } else if ( workingId . length === 24 && checkForHexRegExp . test ( workingId ) ) {
84
+ this [ kId ] = Buffer . from ( workingId , 'hex' ) ;
87
85
} else {
88
86
throw new BSONTypeError (
89
- 'Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters'
87
+ 'Argument passed in must be a string of 12 bytes or a string of 24 hex characters'
90
88
) ;
91
89
}
90
+ } else {
91
+ throw new BSONTypeError ( 'Argument passed in does not match the accepted types' ) ;
92
92
}
93
-
93
+ // If we are caching the hex string
94
94
if ( ObjectId . cacheHexString ) {
95
95
this . __id = this . id . toString ( 'hex' ) ;
96
96
}
@@ -156,7 +156,7 @@ export class ObjectId {
156
156
*/
157
157
static generate ( time ?: number ) : Buffer {
158
158
if ( 'number' !== typeof time ) {
159
- time = ~ ~ ( Date . now ( ) / 1000 ) ;
159
+ time = Math . floor ( Date . now ( ) / 1000 ) ;
160
160
}
161
161
162
162
const inc = ObjectId . getInc ( ) ;
0 commit comments