summaryrefslogtreecommitdiffstats
path: root/node_modules/mongoose/lib/cast/objectid.js
blob: 67cffb543042373e9a964941f2cf67079db858c0 (plain) (blame)
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
'use strict';

const ObjectId = require('../driver').get().ObjectId;
const assert = require('assert');

module.exports = function castObjectId(value) {
  if (value == null) {
    return value;
  }

  if (value instanceof ObjectId) {
    return value;
  }

  if (value._id) {
    if (value._id instanceof 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());
  }

  assert.ok(false);
};