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

Update isValid method to match the bson node library #46

Merged
merged 2 commits into from
Jan 20, 2022
Merged
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
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