From 6613a9955d42768ff75b42ddebcdd0051511730f Mon Sep 17 00:00:00 2001 From: Liam Mitchell Date: Sat, 8 Aug 2015 21:49:44 +1200 Subject: [PATCH 1/2] Allowing passing a string to get ObjectID parsed. Returns an ObjectId rather than a string. Also using !== or === where possible rather than just == or !=. Sorry I could not find a way to run the tests. --- src/main/javascript/Objectid.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/javascript/Objectid.js b/src/main/javascript/Objectid.js index 7f32db8..1f02e52 100644 --- a/src/main/javascript/Objectid.js +++ b/src/main/javascript/Objectid.js @@ -5,8 +5,8 @@ * and GPL (http://www.opensource.org/licenses/gpl-license.php) version 2 licenses. * This software is not distributed under version 3 or later of the GPL. * -* Version 1.0.2 -* +* Version 1.0.2 (Liams Modifications) +* Source: https://github.com/justaprogrammer/ObjectId.js */ if (!document) var document = { cookie: '' }; // fix crashes on node @@ -25,14 +25,14 @@ var ObjectId = (function () { for (var i in cookieList) { var cookie = cookieList[i].split('='); var cookieMachineId = parseInt(cookie[1], 10); - if (cookie[0] == 'mongoMachineId' && cookieMachineId && cookieMachineId >= 0 && cookieMachineId <= 16777215) { + if (cookie[0] === 'mongoMachineId' && cookieMachineId && cookieMachineId >= 0 && cookieMachineId <= 16777215) { machine = cookieMachineId; break; } } document.cookie = 'mongoMachineId=' + machine + ';expires=Tue, 19 Jan 2038 05:00:00 GMT;path=/'; }; - if (typeof (localStorage) != 'undefined') { + if (typeof (localStorage) !== 'undefined') { try { var mongoMachineId = parseInt(localStorage['mongoMachineId']); if (mongoMachineId >= 0 && mongoMachineId <= 16777215) { @@ -50,22 +50,23 @@ var ObjectId = (function () { function ObjId() { if (!(this instanceof ObjectId)) { - return new ObjectId(arguments[0], arguments[1], arguments[2], arguments[3]).toString(); + return new ObjectId(arguments[0], arguments[1], arguments[2], arguments[3]); } - if (typeof (arguments[0]) == 'object') { + if (typeof (arguments[0]) === 'object') { this.timestamp = arguments[0].timestamp; this.machine = arguments[0].machine; this.pid = arguments[0].pid; this.increment = arguments[0].increment; } - else if (typeof (arguments[0]) == 'string' && arguments[0].length == 24) { - this.timestamp = Number('0x' + arguments[0].substr(0, 8)), - this.machine = Number('0x' + arguments[0].substr(8, 6)), - this.pid = Number('0x' + arguments[0].substr(14, 4)), - this.increment = Number('0x' + arguments[0].substr(18, 6)) + else if (typeof (arguments[0]) === 'string' && arguments[0].length === 24) { + debugger; + this.timestamp = Number('0x' + arguments[0].substr(0, 8)); + this.machine = Number('0x' + arguments[0].substr(8, 6)); + this.pid = Number('0x' + arguments[0].substr(14, 4)); + this.increment = Number('0x' + arguments[0].substr(18, 6)); } - else if (arguments.length == 4 && arguments[0] != null) { + else if (arguments.length === 4 && arguments[0] !== null) { this.timestamp = arguments[0]; this.machine = arguments[1]; this.pid = arguments[2]; @@ -117,4 +118,4 @@ ObjectId.prototype.toString = function () { '000000'.substr(0, 6 - machine.length) + machine + '0000'.substr(0, 4 - pid.length) + pid + '000000'.substr(0, 6 - increment.length) + increment; -}; +}; \ No newline at end of file From acf03ff0fc493715c3b34492fab1e3a365f18b5e Mon Sep 17 00:00:00 2001 From: Liam Mitchell Date: Sun, 9 Aug 2015 03:43:05 +1200 Subject: [PATCH 2/2] Removed debugger statement Removed debugger statement --- src/main/javascript/Objectid.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/javascript/Objectid.js b/src/main/javascript/Objectid.js index 1f02e52..51e203d 100644 --- a/src/main/javascript/Objectid.js +++ b/src/main/javascript/Objectid.js @@ -60,7 +60,6 @@ var ObjectId = (function () { this.increment = arguments[0].increment; } else if (typeof (arguments[0]) === 'string' && arguments[0].length === 24) { - debugger; this.timestamp = Number('0x' + arguments[0].substr(0, 8)); this.machine = Number('0x' + arguments[0].substr(8, 6)); this.pid = Number('0x' + arguments[0].substr(14, 4));