Skip to content

Commit

Permalink
Merge pull request #46 from MrEmanuel/patch-2
Browse files Browse the repository at this point in the history
Update isValid method to match the bson node library
  • Loading branch information
niftylettuce authored Jan 20, 2022
2 parents 25a73b0 + 742268a commit 5006541
Showing 1 changed file with 4 additions and 23 deletions.
27 changes: 4 additions & 23 deletions objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,31 +132,12 @@ ObjectID.createFromHexString = function(hexString) {
ObjectID.isValid = function(id) {
if (id == null) return false;

if (typeof id === 'number') {
try {
new ObjectID(id);
return true;
} catch {
return false;
}

if (typeof id === 'string') {
return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
}

if (id instanceof ObjectID) {
return true;
}

if (isBuffer(id)) {
return true;
}

// Duck-Typing detection of ObjectId like objects
if (
typeof id.toHexString === 'function' &&
(id.id instanceof _Buffer || typeof id.id === 'string')
) {
return id.id.length === 12 || (id.id.length === 24 && checkForHexRegExp.test(id.id));
}

return false;
};

ObjectID.prototype = {
Expand Down

0 comments on commit 5006541

Please sign in to comment.