@@ -11,22 +11,23 @@ var MACHINE_ID = parseInt(Math.random() * 0xffffff, 10);
11
11
12
12
// Regular expression that checks for hex value
13
13
var checkForHexRegExp = new RegExp ( '^[0-9a-fA-F]{24}$' ) ;
14
+ var hasBufferType = false ;
14
15
15
16
// Check if buffer exists
16
17
try {
17
- if ( Buffer && Buffer . from ) var hasBufferType = true ;
18
+ if ( Buffer && Buffer . from ) hasBufferType = true ;
18
19
} catch ( err ) {
19
20
hasBufferType = false ;
20
21
}
21
22
22
23
/**
23
- * Create a new ObjectID instance
24
- *
25
- * @class
26
- * @param {(string|number) } id Can be a 24 byte hex string, 12 byte binary string or a Number.
27
- * @property {number } generationTime The generation time of this ObjectId instance
28
- * @return {ObjectID } instance of ObjectID.
29
- */
24
+ * Create a new ObjectID instance
25
+ *
26
+ * @class
27
+ * @param {(string|number) } id Can be a 24 byte hex string, 12 byte binary string or a Number.
28
+ * @property {number } generationTime The generation time of this ObjectId instance
29
+ * @return {ObjectID } instance of ObjectID.
30
+ */
30
31
var ObjectID = function ObjectID ( id ) {
31
32
// Duck-typing to support ObjectId from different npm packages
32
33
if ( id instanceof ObjectID ) return id ;
@@ -81,11 +82,11 @@ for (var i = 0; i < 256; i++) {
81
82
}
82
83
83
84
/**
84
- * Return the ObjectID id as a 24 byte hex string representation
85
- *
86
- * @method
87
- * @return {string } return the 24 byte hex string representation.
88
- */
85
+ * Return the ObjectID id as a 24 byte hex string representation
86
+ *
87
+ * @method
88
+ * @return {string } return the 24 byte hex string representation.
89
+ */
89
90
ObjectID . prototype . toHexString = function ( ) {
90
91
if ( ObjectID . cacheHexString && this . __id ) return this . __id ;
91
92
@@ -113,34 +114,34 @@ ObjectID.prototype.toHexString = function() {
113
114
} ;
114
115
115
116
/**
116
- * Update the ObjectID index used in generating new ObjectID's on the driver
117
- *
118
- * @method
119
- * @return {number } returns next index value.
120
- * @ignore
121
- */
117
+ * Update the ObjectID index used in generating new ObjectID's on the driver
118
+ *
119
+ * @method
120
+ * @return {number } returns next index value.
121
+ * @ignore
122
+ */
122
123
ObjectID . prototype . get_inc = function ( ) {
123
124
return ( ObjectID . index = ( ObjectID . index + 1 ) % 0xffffff ) ;
124
125
} ;
125
126
126
127
/**
127
- * Update the ObjectID index used in generating new ObjectID's on the driver
128
- *
129
- * @method
130
- * @return {number } returns next index value.
131
- * @ignore
132
- */
128
+ * Update the ObjectID index used in generating new ObjectID's on the driver
129
+ *
130
+ * @method
131
+ * @return {number } returns next index value.
132
+ * @ignore
133
+ */
133
134
ObjectID . prototype . getInc = function ( ) {
134
135
return this . get_inc ( ) ;
135
136
} ;
136
137
137
138
/**
138
- * Generate a 12 byte id buffer used in ObjectID's
139
- *
140
- * @method
141
- * @param {number } [time] optional parameter allowing to pass in a second based timestamp.
142
- * @return {Buffer } return the 12 byte id buffer string.
143
- */
139
+ * Generate a 12 byte id buffer used in ObjectID's
140
+ *
141
+ * @method
142
+ * @param {number } [time] optional parameter allowing to pass in a second based timestamp.
143
+ * @return {Buffer } return the 12 byte id buffer string.
144
+ */
144
145
ObjectID . prototype . generate = function ( time ) {
145
146
if ( 'number' !== typeof time ) {
146
147
time = ~ ~ ( Date . now ( ) / 1000 ) ;
@@ -175,12 +176,12 @@ ObjectID.prototype.generate = function(time) {
175
176
} ;
176
177
177
178
/**
178
- * Converts the id into a 24 byte hex string for printing
179
- *
180
- * @param {String } format The Buffer toString format parameter.
181
- * @return {String } return the 24 byte hex string representation.
182
- * @ignore
183
- */
179
+ * Converts the id into a 24 byte hex string for printing
180
+ *
181
+ * @param {String } format The Buffer toString format parameter.
182
+ * @return {String } return the 24 byte hex string representation.
183
+ * @ignore
184
+ */
184
185
ObjectID . prototype . toString = function ( format ) {
185
186
// Is the id a buffer then use the buffer toString method to return the format
186
187
if ( this . id && this . id . copy ) {
@@ -192,33 +193,31 @@ ObjectID.prototype.toString = function(format) {
192
193
} ;
193
194
194
195
/**
195
- * Converts to a string representation of this Id.
196
- *
197
- * @return {String } return the 24 byte hex string representation.
198
- * @ignore
199
- */
196
+ * Converts to a string representation of this Id.
197
+ *
198
+ * @return {String } return the 24 byte hex string representation.
199
+ * @ignore
200
+ */
200
201
ObjectID . prototype . inspect = ObjectID . prototype . toString ;
201
202
202
203
/**
203
- * Converts to its JSON representation.
204
- *
205
- * @return {String } return the 24 byte hex string representation.
206
- * @ignore
207
- */
204
+ * Converts to its JSON representation.
205
+ *
206
+ * @return {String } return the 24 byte hex string representation.
207
+ * @ignore
208
+ */
208
209
ObjectID . prototype . toJSON = function ( ) {
209
210
return this . toHexString ( ) ;
210
211
} ;
211
212
212
213
/**
213
- * Compares the equality of this ObjectID with `otherID`.
214
- *
215
- * @method
216
- * @param {object } otherID ObjectID instance to compare against.
217
- * @return {boolean } the result of comparing two ObjectID's
218
- */
214
+ * Compares the equality of this ObjectID with `otherID`.
215
+ *
216
+ * @method
217
+ * @param {object } otherID ObjectID instance to compare against.
218
+ * @return {boolean } the result of comparing two ObjectID's
219
+ */
219
220
ObjectID . prototype . equals = function equals ( otherId ) {
220
- // var id;
221
-
222
221
if ( otherId instanceof ObjectID ) {
223
222
return this . toString ( ) === otherId . toString ( ) ;
224
223
} else if (
@@ -240,11 +239,11 @@ ObjectID.prototype.equals = function equals(otherId) {
240
239
} ;
241
240
242
241
/**
243
- * Returns the generation date (accurate up to the second) that this ID was generated.
244
- *
245
- * @method
246
- * @return {date } the generation date
247
- */
242
+ * Returns the generation date (accurate up to the second) that this ID was generated.
243
+ *
244
+ * @method
245
+ * @return {date } the generation date
246
+ */
248
247
ObjectID . prototype . getTimestamp = function ( ) {
249
248
var timestamp = new Date ( ) ;
250
249
var time = this . id [ 3 ] | ( this . id [ 2 ] << 8 ) | ( this . id [ 1 ] << 16 ) | ( this . id [ 0 ] << 24 ) ;
@@ -253,24 +252,24 @@ ObjectID.prototype.getTimestamp = function() {
253
252
} ;
254
253
255
254
/**
256
- * @ignore
257
- */
255
+ * @ignore
256
+ */
258
257
ObjectID . index = ~ ~ ( Math . random ( ) * 0xffffff ) ;
259
258
260
259
/**
261
- * @ignore
262
- */
260
+ * @ignore
261
+ */
263
262
ObjectID . createPk = function createPk ( ) {
264
263
return new ObjectID ( ) ;
265
264
} ;
266
265
267
266
/**
268
- * Creates an ObjectID from a second based number, with the rest of the ObjectID zeroed out. Used for comparisons or sorting the ObjectID.
269
- *
270
- * @method
271
- * @param {number } time an integer number representing a number of seconds.
272
- * @return {ObjectID } return the created ObjectID
273
- */
267
+ * Creates an ObjectID from a second based number, with the rest of the ObjectID zeroed out. Used for comparisons or sorting the ObjectID.
268
+ *
269
+ * @method
270
+ * @param {number } time an integer number representing a number of seconds.
271
+ * @return {ObjectID } return the created ObjectID
272
+ */
274
273
ObjectID . createFromTime = function createFromTime ( time ) {
275
274
var buffer = new Buffer ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ;
276
275
// Encode time into first 4 bytes
@@ -283,7 +282,6 @@ ObjectID.createFromTime = function createFromTime(time) {
283
282
} ;
284
283
285
284
// Lookup tables
286
- //var encodeLookup = '0123456789abcdef'.split('');
287
285
var decodeLookup = [ ] ;
288
286
i = 0 ;
289
287
while ( i < 10 ) decodeLookup [ 0x30 + i ] = i ++ ;
@@ -295,12 +293,12 @@ var convertToHex = function(bytes) {
295
293
} ;
296
294
297
295
/**
298
- * Creates an ObjectID from a hex string representation of an ObjectID.
299
- *
300
- * @method
301
- * @param {string } hexString create a ObjectID from a passed in 24 byte hexstring.
302
- * @return {ObjectID } return the created ObjectID
303
- */
296
+ * Creates an ObjectID from a hex string representation of an ObjectID.
297
+ *
298
+ * @method
299
+ * @param {string } hexString create a ObjectID from a passed in 24 byte hexstring.
300
+ * @return {ObjectID } return the created ObjectID
301
+ */
304
302
ObjectID . createFromHexString = function createFromHexString ( string ) {
305
303
// Throw an error if it's not a valid setup
306
304
if ( typeof string === 'undefined' || ( string != null && string . length !== 24 ) ) {
@@ -325,11 +323,11 @@ ObjectID.createFromHexString = function createFromHexString(string) {
325
323
} ;
326
324
327
325
/**
328
- * Checks if a value is a valid bson ObjectId
329
- *
330
- * @method
331
- * @return {boolean } return true if the value is a valid bson ObjectId, return false otherwise.
332
- */
326
+ * Checks if a value is a valid bson ObjectId
327
+ *
328
+ * @method
329
+ * @return {boolean } return true if the value is a valid bson ObjectId, return false otherwise.
330
+ */
333
331
ObjectID . isValid = function isValid ( id ) {
334
332
if ( id == null ) return false ;
335
333
@@ -358,8 +356,8 @@ ObjectID.isValid = function isValid(id) {
358
356
} ;
359
357
360
358
/**
361
- * @ignore
362
- */
359
+ * @ignore
360
+ */
363
361
Object . defineProperty ( ObjectID . prototype , 'generationTime' , {
364
362
enumerable : true ,
365
363
get : function ( ) {
0 commit comments