Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set machine #12

Merged
merged 3 commits into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -28,6 +28,7 @@
},
"homepage": "https://github.com/williamkapke/bson-objectid",
"devDependencies": {
"mocha": "^3.5.3",
"should": "^4.1.0"
}
}
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));
});
});