Comparing version 1.0.1 to 1.0.2
@@ -0,1 +1,5 @@ | ||
1.0.2 2016-12-06 | ||
---------------- | ||
- Minor optimizations for ObjectID to use Buffer.from where available. | ||
1.0.1 2016-12-06 | ||
@@ -2,0 +6,0 @@ ---------------- |
@@ -13,3 +13,9 @@ /** | ||
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$"); | ||
var hasBufferType = false; | ||
// Check if buffer exists | ||
try { | ||
if(Buffer && Buffer.from) hasBufferType = true; | ||
} catch(err) {}; | ||
/** | ||
@@ -30,3 +36,13 @@ * Create a new ObjectID instance | ||
var __id = null; | ||
// The most common usecase (blank id, new objectId instance) | ||
if(id == null || typeof id == 'number') { | ||
// Generate a new id | ||
this.id = this.generate(id); | ||
// If we are caching the hex string | ||
if(ObjectID.cacheHexString) this.__id = this.toString('hex'); | ||
// Return the object | ||
return; | ||
} | ||
// Check if the passed in id is valid | ||
var valid = ObjectID.isValid(id); | ||
@@ -37,7 +53,6 @@ | ||
throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters"); | ||
} else if(valid && typeof id == 'string' && id.length == 24 && hasBufferType) { | ||
return new ObjectID(Buffer.from(id, 'hex')); | ||
} else if(valid && typeof id == 'string' && id.length == 24) { | ||
return ObjectID.createFromHexString(id); | ||
} else if(id == null || typeof id == 'number') { | ||
// convert to 12 byte binary string | ||
this.id = this.generate(id); | ||
} else if(id != null && id.length === 12) { | ||
@@ -53,3 +68,3 @@ // assume 12 byte string | ||
if(ObjectID.cacheHexString) this.__id = this.toHexString(); | ||
if(ObjectID.cacheHexString) this.__id = this.toString('hex'); | ||
}; | ||
@@ -156,6 +171,13 @@ | ||
* | ||
* @param {String} format The Buffer toString format parameter. | ||
* @return {String} return the 24 byte hex string representation. | ||
* @ignore | ||
*/ | ||
ObjectID.prototype.toString = function() { | ||
ObjectID.prototype.toString = function(format) { | ||
// Is the id a buffer then use the buffer toString method to return the format | ||
if(this.id && this.id.copy) { | ||
return this.id.toString(format || 'hex'); | ||
} | ||
// if(this.buffer ) | ||
return this.toHexString(); | ||
@@ -271,18 +293,15 @@ }; | ||
// Throw an error if it's not a valid setup | ||
if(typeof string === 'undefined' || string != null && string.length != 24) | ||
if(typeof string === 'undefined' || string != null && string.length != 24) { | ||
throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters"); | ||
} | ||
var length = string.length; | ||
// Use Buffer.from method if available | ||
if(hasBufferType) return new ObjectID(Buffer.from(string, 'hex')); | ||
if(length > 12*2) { | ||
throw new Error('Id cannot be longer than 12 bytes'); | ||
} | ||
// Calculate lengths | ||
var sizeof = length >> 1; | ||
var array = new _Buffer(sizeof); | ||
var array = new _Buffer(12); | ||
var n = 0; | ||
var i = 0; | ||
while (i < length) { | ||
while (i < 24) { | ||
array[n++] = decodeLookup[string.charCodeAt(i++)] << 4 | decodeLookup[string.charCodeAt(i++)] | ||
@@ -289,0 +308,0 @@ } |
@@ -15,3 +15,3 @@ { | ||
], | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": "Christian Amor Kvalheim <christkv@gmail.com>", | ||
@@ -18,0 +18,0 @@ "contributors": [], |
@@ -60,3 +60,3 @@ # BSON parser | ||
`npm install bson-ext` | ||
`npm install bson` | ||
@@ -63,0 +63,0 @@ ## API |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
724501
19904