Skip to content

Commit

Permalink
Merge branch 'master' into typescript-typings
Browse files Browse the repository at this point in the history
  • Loading branch information
williamkapke authored Sep 28, 2017
2 parents 96b338d + 1bdbbe1 commit e17bfa0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ This diverges from [bson](https://github.com/mongodb/js-bson)'s implementation w

**returns** a valid 24 character `ObjectID` hex string.

### ObjectID.setMachineID(number)<br>ObjectID.setMachineID(string)
You can use this to set a custom machine-id.
It should be a 6 character hex string or a 3 byte integer and will truncate all larger values.

### ObjectID.getMachineID()
**returns** the machine-id as a 3 byte int.

## Test
mocha

Expand Down
41 changes: 41 additions & 0 deletions objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,47 @@ ObjectID.isValid = function(objectid) {
return /^[0-9A-F]{24}$/i.test(objectid.toString());
};

/**
* set a custom machineID
*
* @param {String|Number} machineid Can be a string, hex-string or a number
* @return {void}
* @api public
*/
ObjectID.setMachineID = function(arg) {
var machineID;

if(typeof arg === "string") {
// hex string
machineID = parseInt(arg, 16);

// any string
if(isNaN(machineID)) {
arg = ('000000' + arg).substr(-7,6);

machineID = "";
for(var i = 0;i<6; i++) {
machineID += (arg.charCodeAt(i));
}
}
}
else if(/number|undefined/.test(typeof arg)) {
machineID = arg | 0;
}

MACHINE_ID = (machineID & 0xFFFFFF);
}

/**
* get the machineID
*
* @return {number}
* @api public
*/
ObjectID.getMachineID = function() {
return MACHINE_ID;
}

ObjectID.prototype = {
_bsontype: 'ObjectID',
constructor: ObjectID,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
"@types/node": "^8.0.31",
"should": "^4.1.0",
"typescript": "^2.5.3"
"mocha": "^3.5.3"
}
}
31 changes: 31 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,36 @@ describe("ObjectIDs", function() {
}).should.throw();
});

it("should should set/get a custom machineID", function() {
var tests = [
{ machineID: 0x1, expected: 0x000001 }, // short
{ machineID: 0x123456, expected: 0x123456 }, // exact
{ machineID: 0x0987654321, expected: 0x654321 }, // long

{ machineID: 'TIZÙL“G!íçm', expected: 0xe41e05 }, // any string

{ machineID: '01', expected: 0x000001 }, // short hex string
{ machineID: '01a2b3', expected: 0x01a2b3 }, // exact hex string
{ machineID: '7f6e5d4c3b2a0', expected: 0xc3b2a0 }, // long hex string
];

tests.forEach(function(test) {
ObjectID.setMachineID(test.machineID);

ObjectID.getMachineID().should.eql(test.expected);
});
});

it("should work with a specific machineID", function() {
var testPattern = /^([0-9a-f]{8})([0-9a-f]{6})([0-9a-f]{4})([0-9a-f]{6})$/i;

var mid = parseInt(Math.random() * 0xFFFFFF, 10);
ObjectID.setMachineID(mid);

var o = ObjectID.generate();
var result = o.match(testPattern);

result[2].should.eql(mid.toString(16));
});
});

0 comments on commit e17bfa0

Please sign in to comment.