All files / mongoose/lib/helpers/update updatedPathsByArrayFilter.js

13.33% Statements 2/15
0% Branches 0/6
0% Functions 0/2
13.33% Lines 2/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    1x   1x                                              
'use strict';
 
const modifiedPaths = require('./modifiedPaths');
 
module.exports = function updatedPathsByArrayFilter(update) {
  if (update == null) {
    return {};
  }
  const updatedPaths = modifiedPaths(update);
 
  return Object.keys(updatedPaths).reduce((cur, path) => {
    const matches = path.match(/\$\[[^\]]+\]/g);
    if (matches == null) {
      return cur;
    }
    for (const match of matches) {
      const firstMatch = path.indexOf(match);
      if (firstMatch !== path.lastIndexOf(match)) {
        throw new Error(`Path '${path}' contains the same array filter multiple times`);
      }
      cur[match.substring(2, match.length - 1)] = path.
        substring(0, firstMatch - 1).
        replace(/\$\[[^\]]+\]/g, '0');
    }
    return cur;
  }, {});
};