Skip to content

Commit

Permalink
more tests, more fixes. v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
williamkapke committed Oct 23, 2014
1 parent b2c4bc6 commit 06942c4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function ObjectID(arg) {
buf = Array.prototype.slice.call(arg);
}
else if(typeof arg === "string") {
if(arg.length!==12 && arg.length!==24)
if(arg.length!==12 && !ObjectID.isValid(arg))
throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");

buf = buffer(arg);
Expand Down Expand Up @@ -116,7 +116,7 @@ ObjectID.prototype = {
* @api public
*/
getTimestamp: function(){
return new Date(parseInt(this.str.substr(0,8), 16));
return new Date(parseInt(this.str.substr(0,8), 16) * 1000);
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bson-objectid",
"version": "0.1.1",
"version": "1.0.0",
"description": "Construct ObjectIDs without the mongodb driver or bson module",
"main": "objectid.js",
"directories": {
Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ describe("ObjectIDs", function() {
o.str.should.eql(hexString);
});

it("should correctly retrieve timestamp", function() {
var testDate = new Date();
var object1 = new ObjectID();
var seconds1 = Math.floor(testDate.getTime()/1000);
var seconds2 = Math.floor(object1.getTimestamp().getTime()/1000);
seconds1.should.eql(seconds2);
});

it("should validate valid hex strings", function() {
ObjectID.isValid("54495ad94c934721ede76d90").should.be.ok;
ObjectID.isValid("aaaaaaaaaaaaaaaaaaaaaaaa").should.be.ok;
Expand All @@ -86,6 +94,11 @@ describe("ObjectIDs", function() {

it("should invalidate bad strings", function() {
ObjectID.isValid().should.not.be.ok;
ObjectID.isValid(null).should.not.be.ok;
ObjectID.isValid({}).should.not.be.ok;
ObjectID.isValid([]).should.not.be.ok;
ObjectID.isValid(true).should.not.be.ok;
ObjectID.isValid("invalid").should.not.be.ok;
ObjectID.isValid("").should.not.be.ok;
ObjectID.isValid("zzzzzzzzzzzzzzzzzzzzzzzz").should.not.be.ok;
ObjectID.isValid("54495-ad94c934721ede76d9").should.not.be.ok;
Expand All @@ -109,5 +122,11 @@ describe("ObjectIDs", function() {
o.toString().should.eql("54495ad94c934721ede76d90");
});

it("should throw and error if constructing with an invalid string", function() {
(function(){
var o = new ObjectID("tttttttttttttttttttttttt");
}).should.throw();
});

});

0 comments on commit 06942c4

Please sign in to comment.