All files / mongoose/lib/cast objectid.js

40% Statements 6/15
16.66% Branches 2/12
100% Functions 1/1
40% Lines 6/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30    1x 1x   1x 11x       11x 11x                                    
'use strict';
 
const isBsonType = require('../helpers/isBsonType');
const ObjectId = require('../types/objectid');
 
module.exports = function castObjectId(value) {
  Iif (value == null) {
    return value;
  }
 
  Eif (isBsonType(value, 'ObjectId')) {
    return value;
  }
 
  if (value._id) {
    if (isBsonType(value._id, 'ObjectId')) {
      return value._id;
    }
    if (value._id.toString instanceof Function) {
      return new ObjectId(value._id.toString());
    }
  }
 
  if (value.toString instanceof Function) {
    return new ObjectId(value.toString());
  }
 
  return new ObjectId(value);
};