Skip to content

Commit

Permalink
fix isValid case where isValid(Array<ObjectId>) => true to => false
Browse files Browse the repository at this point in the history
  • Loading branch information
junosuarez committed Jul 21, 2015
1 parent b5d34e7 commit 85566ac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
var inherits = require('util').inherits
var ObjectId = require('bson').ObjectId
var toString = require('to-string')

ObjectId.prototype.equals = function (oidB) {
return equals(this, oidB)
}

var objIdPattern = /^[0-9a-fA-F]{24}$/;
var isValid = function (alleged) {
return (!!alleged && objIdPattern.test(toString(alleged)))
return (Boolean(alleged) && !Array.isArray(alleged) && objIdPattern.test(String(alleged)))
}

var equals = function (oidA, oidB) {
Expand All @@ -20,8 +18,8 @@ var equals = function (oidA, oidB) {
}

if (oidA === oidB) { return true; }
if (!oidA || !oidB) { return false }
return (oidA.toString() === oidB.toString())
if (!isValid(oidA) || !isValid(oidB)) { return false }
return (String(oidA) === String(oidB))
return false;
}

Expand All @@ -42,7 +40,7 @@ function Id(id) {
return new ObjectId()
}

id = toString(id)
id = String(id)

if (isValid(id)) {
return new ObjectId(id)
Expand All @@ -57,4 +55,4 @@ module.exports = Id;
module.exports.constructor = ObjectId;
module.exports.tryParse = tryParse;
module.exports.equals = equals;
module.exports.isValid = isValid;
module.exports.isValid = isValid;
7 changes: 6 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ describe('objectid', function () {
ObjectId.isValid(new NativeObjectId).should.equal(true)
})

it('false for 1-element array of ObjectId', function () {
ObjectId.isValid([ObjectId()]).should.equal(false)
})

})

describe('.equals', function () {
Expand All @@ -142,8 +146,9 @@ describe('objectid', function () {
var oidB = '511083bb08ce6b1b00000003'
ObjectId.equals(oidA, oidB).should.equal(true)
})
it('returns false otherwise', function () {
it('returns false if A or B are not ObjectIds', function () {
ObjectId.equals('sdfsdfsdfsdf', testOid).should.equal(false)
ObjectId.equals('511083bb08ce6b1b00000003', ['511083bb08ce6b1b00000003']).should.equal(false)
})
it('is also on ObjectId.prototype', function () {
var oidA = ObjectId()
Expand Down

0 comments on commit 85566ac

Please sign in to comment.