All files / mongoose/lib/helpers/document handleSpreadDoc.js

29.41% Statements 5/17
23.07% Branches 3/13
100% Functions 1/1
29.41% Lines 5/17

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 31 32 33 34 35 36    1x   1x               1x 53x                                       53x    
'use strict';
 
const utils = require('../../utils');
 
const keysToSkip = new Set(['__index', '__parentArray', '_doc']);
 
/**
 * Using spread operator on a Mongoose document gives you a
 * POJO that has a tendency to cause infinite recursion. So
 * we use this function on `set()` to prevent that.
 */
 
module.exports = function handleSpreadDoc(v, includeExtraKeys) {
  Iif (utils.isPOJO(v) && v.$__ != null && v._doc != null) {
    if (includeExtraKeys) {
      const extraKeys = {};
      for (const key of Object.keys(v)) {
        if (typeof key === 'symbol') {
          continue;
        }
        if (key[0] === '$') {
          continue;
        }
        if (keysToSkip.has(key)) {
          continue;
        }
        extraKeys[key] = v[key];
      }
      return { ...v._doc, ...extraKeys };
    }
    return v._doc;
  }
 
  return v;
};