UNPKG

@360-l/mongo-bulk-data-migration

Version:
36 lines (35 loc) 1.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isArrayFilterPath = isArrayFilterPath; exports.hasPathMatchingPositionalOperators = hasPathMatchingPositionalOperators; exports.buildArrayFiltersOptionToUnset = buildArrayFiltersOptionToUnset; const ARRAY_FILTER_OPERATION_PATTERN = /\$\[(\w+)]/g; function isArrayFilterPath(path) { return !!path.match(ARRAY_FILTER_OPERATION_PATTERN); } function hasPathMatchingPositionalOperators(path, pathsToMatch) { if (!isArrayFilterPath(path)) { return false; } const pathRegex = new RegExp(path.replace(ARRAY_FILTER_OPERATION_PATTERN, '\\w+'), 'g'); return pathsToMatch.some((pathToMatch) => pathRegex.test(pathToMatch)); } function buildArrayFiltersOptionToUnset(path) { let positionalElementNameMatch; const arrayFilters = []; do { positionalElementNameMatch = ARRAY_FILTER_OPERATION_PATTERN.exec(path); if (positionalElementNameMatch) { const subpathWithPositionalElementAndNextChild = _buildSubPathWithPositionalElementAndNextChild(positionalElementNameMatch[1], path); arrayFilters.push({ [subpathWithPositionalElementAndNextChild]: { $exists: true }, }); } } while (positionalElementNameMatch); return arrayFilters; } function _buildSubPathWithPositionalElementAndNextChild(positionalElementName, completePath) { const matchRegexp = new RegExp(`\\$\\[${positionalElementName}]\\.(\\w+)(?:\\.|$)`); const nextChildName = matchRegexp.exec(completePath)[1]; return `${positionalElementName}.${nextChildName}`; }