Skip to content

Instantly share code, notes, and snippets.

@tusbar
Last active March 7, 2019 14:07
Show Gist options
  • Save tusbar/dafc8af4a074b04d00e7f2d90c3d42f2 to your computer and use it in GitHub Desktop.
Save tusbar/dafc8af4a074b04d00e7f2d90c3d42f2 to your computer and use it in GitHub Desktop.
const {ObjectId} = require('mongodb')
function parse(input) {
if (!input) {
return
}
if (input._id) {
input = input._id
}
if (input instanceof ObjectId) {
return input
}
if (typeof input === 'string' && ObjectId.isValid(input)) {
return new ObjectId(input)
}
}
function equals(left, right) {
const ol = parse(left)
const or = parse(right)
return Boolean(ol) && ol.equals(or)
}
exports.parse = parse
exports.equals = equals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment