File tree Expand file tree Collapse file tree 1 file changed +6
-9
lines changed Expand file tree Collapse file tree 1 file changed +6
-9
lines changed Original file line number Diff line number Diff line change @@ -285,22 +285,19 @@ var convertToHex = function(bytes) {
285
285
*/
286
286
ObjectID . createFromHexString = function createFromHexString ( string ) {
287
287
// Throw an error if it's not a valid setup
288
- if ( typeof string === 'undefined' || string != null && string . length != 24 )
288
+ if ( typeof string === 'undefined' || string != null && string . length != 24 ) {
289
289
throw new Error ( "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters" ) ;
290
-
291
- var length = string . length ;
292
-
293
- if ( length > 12 * 2 ) {
294
- throw new Error ( 'Id cannot be longer than 12 bytes' ) ;
295
290
}
296
291
292
+ // Use Buffer.from method if available
293
+ if ( hasBufferType ) return new ObjectID ( Buffer . from ( string , 'hex' ) ) ;
294
+
297
295
// Calculate lengths
298
- var sizeof = length >> 1 ;
299
- var array = new _Buffer ( sizeof ) ;
296
+ var array = new _Buffer ( 12 ) ;
300
297
var n = 0 ;
301
298
var i = 0 ;
302
299
303
- while ( i < length ) {
300
+ while ( i < 24 ) {
304
301
array [ n ++ ] = decodeLookup [ string . charCodeAt ( i ++ ) ] << 4 | decodeLookup [ string . charCodeAt ( i ++ ) ]
305
302
}
306
303
You can’t perform that action at this time.
0 commit comments