Skip to content

Commit f858cd3

Browse files
committed
refactor(types): ObjectID => ObjectId
1 parent ad6f5bf commit f858cd3

12 files changed

+115
-114
lines changed

lib/bson.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Map = require('./map');
55
const Long = require('./long');
66
const Double = require('./double');
77
const Timestamp = require('./timestamp');
8-
const ObjectID = require('./objectid');
8+
const ObjectId = require('./objectid');
99
const BSONRegExp = require('./regexp');
1010
const Symbol = require('./symbol');
1111
const Int32 = require('./int_32');
@@ -221,8 +221,7 @@ module.exports = Object.assign({}, constants, {
221221
Symbol,
222222
DBRef,
223223
Binary,
224-
ObjectID,
225-
ObjectId: ObjectID,
224+
ObjectId,
226225
Long,
227226
Timestamp,
228227
Double,
@@ -238,5 +237,8 @@ module.exports = Object.assign({}, constants, {
238237
deserialize,
239238
calculateObjectSize,
240239
deserializeStream,
241-
setInternalBufferSize
240+
setInternalBufferSize,
241+
242+
// legacy support
243+
ObjectID: ObjectId
242244
});

lib/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = {
5555
BSON_DATA_UNDEFINED: 6,
5656

5757
/**
58-
* ObjectID BSON Type
58+
* ObjectId BSON Type
5959
*
6060
* @classconstant BSON_DATA_OID
6161
**/

lib/db_ref.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class DBRef {
88
* Create a DBRef type
99
*
1010
* @param {string} collection the collection name.
11-
* @param {ObjectID} oid the reference ObjectID.
11+
* @param {ObjectId} oid the reference ObjectId.
1212
* @param {string} [db] optional db name, if omitted the reference is local to the current db.
1313
* @return {DBRef}
1414
*/

lib/objectid.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,40 +45,40 @@ function convertToHex(bytes) {
4545
/**
4646
* A class representation of the BSON ObjectId type.
4747
*/
48-
class ObjectID {
48+
class ObjectId {
4949
/**
5050
* Create an ObjectId type
5151
*
5252
* @param {(string|number)} id Can be a 24 byte hex string, 12 byte binary string or a Number.
5353
* @property {number} generationTime The generation time of this ObjectId instance
54-
* @return {ObjectID} instance of ObjectID.
54+
* @return {ObjectId} instance of ObjectId.
5555
*/
5656
constructor(id) {
5757
// Duck-typing to support ObjectId from different npm packages
58-
if (id instanceof ObjectID) return id;
58+
if (id instanceof ObjectId) return id;
5959

6060
// The most common usecase (blank id, new objectId instance)
6161
if (id == null || typeof id === 'number') {
6262
// Generate a new id
6363
this.id = this.generate(id);
6464
// If we are caching the hex string
65-
if (ObjectID.cacheHexString) this.__id = this.toString('hex');
65+
if (ObjectId.cacheHexString) this.__id = this.toString('hex');
6666
// Return the object
6767
return;
6868
}
6969

7070
// Check if the passed in id is valid
71-
const valid = ObjectID.isValid(id);
71+
const valid = ObjectId.isValid(id);
7272

7373
// Throw an error if it's not a valid setup
7474
if (!valid && id != null) {
7575
throw new TypeError(
7676
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
7777
);
7878
} else if (valid && typeof id === 'string' && id.length === 24 && hasBufferType) {
79-
return new ObjectID(new Buffer(id, 'hex'));
79+
return new ObjectId(new Buffer(id, 'hex'));
8080
} else if (valid && typeof id === 'string' && id.length === 24) {
81-
return ObjectID.createFromHexString(id);
81+
return ObjectId.createFromHexString(id);
8282
} else if (id != null && id.length === 12) {
8383
// assume 12 byte string
8484
this.id = id;
@@ -91,17 +91,17 @@ class ObjectID {
9191
);
9292
}
9393

94-
if (ObjectID.cacheHexString) this.__id = this.toString('hex');
94+
if (ObjectId.cacheHexString) this.__id = this.toString('hex');
9595
}
9696

9797
/**
98-
* Return the ObjectID id as a 24 byte hex string representation
98+
* Return the ObjectId id as a 24 byte hex string representation
9999
*
100100
* @method
101101
* @return {string} return the 24 byte hex string representation.
102102
*/
103103
toHexString() {
104-
if (ObjectID.cacheHexString && this.__id) return this.__id;
104+
if (ObjectId.cacheHexString && this.__id) return this.__id;
105105

106106
let hexString = '';
107107
if (!this.id || !this.id.length) {
@@ -114,31 +114,31 @@ class ObjectID {
114114

115115
if (this.id instanceof _Buffer) {
116116
hexString = convertToHex(this.id);
117-
if (ObjectID.cacheHexString) this.__id = hexString;
117+
if (ObjectId.cacheHexString) this.__id = hexString;
118118
return hexString;
119119
}
120120

121121
for (let i = 0; i < this.id.length; i++) {
122122
hexString += hexTable[this.id.charCodeAt(i)];
123123
}
124124

125-
if (ObjectID.cacheHexString) this.__id = hexString;
125+
if (ObjectId.cacheHexString) this.__id = hexString;
126126
return hexString;
127127
}
128128

129129
/**
130-
* Update the ObjectID index used in generating new ObjectID's on the driver
130+
* Update the ObjectId index used in generating new ObjectId's on the driver
131131
*
132132
* @method
133133
* @return {number} returns next index value.
134134
* @ignore
135135
*/
136136
get_inc() {
137-
return (ObjectID.index = (ObjectID.index + 1) % 0xffffff);
137+
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
138138
}
139139

140140
/**
141-
* Update the ObjectID index used in generating new ObjectID's on the driver
141+
* Update the ObjectId index used in generating new ObjectId's on the driver
142142
*
143143
* @method
144144
* @return {number} returns next index value.
@@ -149,7 +149,7 @@ class ObjectID {
149149
}
150150

151151
/**
152-
* Generate a 12 byte id buffer used in ObjectID's
152+
* Generate a 12 byte id buffer used in ObjectId's
153153
*
154154
* @method
155155
* @param {number} [time] optional parameter allowing to pass in a second based timestamp.
@@ -215,35 +215,35 @@ class ObjectID {
215215
}
216216

217217
/**
218-
* Compares the equality of this ObjectID with `otherID`.
218+
* Compares the equality of this ObjectId with `otherID`.
219219
*
220220
* @method
221-
* @param {object} otherID ObjectID instance to compare against.
222-
* @return {boolean} the result of comparing two ObjectID's
221+
* @param {object} otherID ObjectId instance to compare against.
222+
* @return {boolean} the result of comparing two ObjectId's
223223
*/
224224
equals(otherId) {
225-
if (otherId instanceof ObjectID) {
225+
if (otherId instanceof ObjectId) {
226226
return this.toString() === otherId.toString();
227227
}
228228

229229
if (
230230
typeof otherId === 'string' &&
231-
ObjectID.isValid(otherId) &&
231+
ObjectId.isValid(otherId) &&
232232
otherId.length === 12 &&
233233
this.id instanceof _Buffer
234234
) {
235235
return otherId === this.id.toString('binary');
236236
}
237237

238-
if (typeof otherId === 'string' && ObjectID.isValid(otherId) && otherId.length === 24) {
238+
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
239239
return otherId.toLowerCase() === this.toHexString();
240240
}
241241

242-
if (typeof otherId === 'string' && ObjectID.isValid(otherId) && otherId.length === 12) {
242+
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
243243
return otherId === this.id;
244244
}
245245

246-
if (otherId != null && (otherId instanceof ObjectID || otherId.toHexString)) {
246+
if (otherId != null && (otherId instanceof ObjectId || otherId.toHexString)) {
247247
return otherId.toHexString() === this.toHexString();
248248
}
249249

@@ -267,15 +267,15 @@ class ObjectID {
267267
* @ignore
268268
*/
269269
static createPk() {
270-
return new ObjectID();
270+
return new ObjectId();
271271
}
272272

273273
/**
274-
* Creates an ObjectID from a second based number, with the rest of the ObjectID zeroed out. Used for comparisons or sorting the ObjectID.
274+
* Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
275275
*
276276
* @method
277277
* @param {number} time an integer number representing a number of seconds.
278-
* @return {ObjectID} return the created ObjectID
278+
* @return {ObjectId} return the created ObjectId
279279
*/
280280
static createFromTime(time) {
281281
const buffer = new Buffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
@@ -285,15 +285,15 @@ class ObjectID {
285285
buffer[1] = (time >> 16) & 0xff;
286286
buffer[0] = (time >> 24) & 0xff;
287287
// Return the new objectId
288-
return new ObjectID(buffer);
288+
return new ObjectId(buffer);
289289
}
290290

291291
/**
292-
* Creates an ObjectID from a hex string representation of an ObjectID.
292+
* Creates an ObjectId from a hex string representation of an ObjectId.
293293
*
294294
* @method
295-
* @param {string} hexString create a ObjectID from a passed in 24 byte hexstring.
296-
* @return {ObjectID} return the created ObjectID
295+
* @param {string} hexString create a ObjectId from a passed in 24 byte hexstring.
296+
* @return {ObjectId} return the created ObjectId
297297
*/
298298
static createFromHexString(string) {
299299
// Throw an error if it's not a valid setup
@@ -304,7 +304,7 @@ class ObjectID {
304304
}
305305

306306
// Use Buffer.from method if available
307-
if (hasBufferType) return new ObjectID(new Buffer(string, 'hex'));
307+
if (hasBufferType) return new ObjectId(new Buffer(string, 'hex'));
308308

309309
// Calculate lengths
310310
const array = new _Buffer(12);
@@ -316,7 +316,7 @@ class ObjectID {
316316
(decodeLookup[string.charCodeAt(i++)] << 4) | decodeLookup[string.charCodeAt(i++)];
317317
}
318318

319-
return new ObjectID(array);
319+
return new ObjectId(array);
320320
}
321321

322322
/**
@@ -336,7 +336,7 @@ class ObjectID {
336336
return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
337337
}
338338

339-
if (id instanceof ObjectID) {
339+
if (id instanceof ObjectId) {
340340
return true;
341341
}
342342

@@ -356,7 +356,7 @@ class ObjectID {
356356
/**
357357
* @ignore
358358
*/
359-
Object.defineProperty(ObjectID.prototype, 'generationTime', {
359+
Object.defineProperty(ObjectId.prototype, 'generationTime', {
360360
enumerable: true,
361361
get: function() {
362362
return this.id[3] | (this.id[2] << 8) | (this.id[1] << 16) | (this.id[0] << 24);
@@ -376,12 +376,12 @@ Object.defineProperty(ObjectID.prototype, 'generationTime', {
376376
* @return {String} return the 24 byte hex string representation.
377377
* @ignore
378378
*/
379-
ObjectID.prototype.inspect = ObjectID.prototype.toString;
379+
ObjectId.prototype.inspect = ObjectId.prototype.toString;
380380

381381
/**
382382
* @ignore
383383
*/
384-
ObjectID.index = ~~(Math.random() * 0xffffff);
384+
ObjectId.index = ~~(Math.random() * 0xffffff);
385385

386-
Object.defineProperty(ObjectID.prototype, '_bsontype', { value: 'ObjectID' });
387-
module.exports = ObjectID;
386+
Object.defineProperty(ObjectId.prototype, '_bsontype', { value: 'ObjectId' });
387+
module.exports = ObjectId;

lib/parser/calculate_size.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Buffer = require('buffer').Buffer;
44
const Long = require('../long');
55
const Double = require('../double');
66
const Timestamp = require('../timestamp');
7-
const ObjectID = require('../objectid');
7+
const ObjectId = require('../objectid');
88
const Symbol = require('../symbol');
99
const BSONRegExp = require('../regexp');
1010
const Code = require('../code');
@@ -94,7 +94,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
9494
value['_bsontype'] === 'MaxKey'
9595
) {
9696
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
97-
} else if (value instanceof ObjectID || value['_bsontype'] === 'ObjectID') {
97+
} else if (value instanceof ObjectId || value['_bsontype'] === 'ObjectId') {
9898
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1);
9999
} else if (value instanceof Date || isDate(value)) {
100100
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);

lib/parser/deserializer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Buffer = require('buffer').Buffer;
44
const Long = require('../long');
55
const Double = require('../double');
66
const Timestamp = require('../timestamp');
7-
const ObjectID = require('../objectid');
7+
const ObjectId = require('../objectid');
88
const Code = require('../code');
99
const MinKey = require('../min_key');
1010
const MaxKey = require('../max_key');
@@ -146,7 +146,7 @@ function deserializeObject(buffer, index, options, isArray) {
146146
} else if (elementType === constants.BSON_DATA_OID) {
147147
const oid = new Buffer(12);
148148
buffer.copy(oid, 0, index, index + 12);
149-
object[name] = new ObjectID(oid);
149+
object[name] = new ObjectId(oid);
150150
index = index + 12;
151151
} else if (elementType === constants.BSON_DATA_INT && promoteValues === false) {
152152
object[name] = new Int32(
@@ -554,7 +554,7 @@ function deserializeObject(buffer, index, options, isArray) {
554554
// Read the oid
555555
const oidBuffer = new Buffer(12);
556556
buffer.copy(oidBuffer, 0, index, index + 12);
557-
const oid = new ObjectID(oidBuffer);
557+
const oid = new ObjectId(oidBuffer);
558558

559559
// Update the index
560560
index = index + 12;

lib/parser/serializer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ function serializeInto(
704704
index = serializeNull(buffer, key, value, index, true);
705705
} else if (value === null) {
706706
index = serializeNull(buffer, key, value, index, true);
707-
} else if (value['_bsontype'] === 'ObjectID') {
707+
} else if (value['_bsontype'] === 'ObjectId') {
708708
index = serializeObjectId(buffer, key, value, index, true);
709709
} else if (Buffer.isBuffer(value)) {
710710
index = serializeBuffer(buffer, key, value, index, true);
@@ -811,7 +811,7 @@ function serializeInto(
811811
index = serializeDate(buffer, key, value, index);
812812
} else if (value === null || (value === undefined && ignoreUndefined === false)) {
813813
index = serializeNull(buffer, key, value, index);
814-
} else if (value['_bsontype'] === 'ObjectID') {
814+
} else if (value['_bsontype'] === 'ObjectId') {
815815
index = serializeObjectId(buffer, key, value, index);
816816
} else if (Buffer.isBuffer(value)) {
817817
index = serializeBuffer(buffer, key, value, index);
@@ -913,7 +913,7 @@ function serializeInto(
913913
if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
914914
} else if (value === null) {
915915
index = serializeNull(buffer, key, value, index);
916-
} else if (value['_bsontype'] === 'ObjectID') {
916+
} else if (value['_bsontype'] === 'ObjectId') {
917917
index = serializeObjectId(buffer, key, value, index);
918918
} else if (Buffer.isBuffer(value)) {
919919
index = serializeBuffer(buffer, key, value, index);

test/node/bson_compliance_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Code = BSON.Code;
66
const Binary = BSON.Binary;
77
const Timestamp = BSON.Timestamp;
88
const Long = BSON.Long;
9-
const ObjectID = BSON.ObjectID;
9+
const ObjectId = BSON.ObjectId;
1010
const DBRef = BSON.DBRef;
1111
const MinKey = BSON.MinKey;
1212
const MaxKey = BSON.MaxKey;
@@ -67,7 +67,7 @@ describe('BSON Compliance', function() {
6767
} else if (doc[name]['$regexp']) {
6868
object[name] = new RegExp(doc[name]['$regexp'], doc[name]['$options'] || '');
6969
} else if (doc[name]['$oid']) {
70-
object[name] = new ObjectID(doc[name]['$oid']);
70+
object[name] = new ObjectId(doc[name]['$oid']);
7171
} else if (doc[name]['$binary']) {
7272
object[name] = new Binary(doc[name]['$binary'], doc[name]['$type'] || 1);
7373
} else if (doc[name]['$timestamp']) {

0 commit comments

Comments
 (0)