UNPKG

@goatlab/fluent

Version:

Readable query Interface & API generator for TS and Node

143 lines 4.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractConditions = void 0; const types_1 = require("../../types"); const isAnyObject_1 = require("./isAnyObject"); /** * Custom flatten function that preserves types */ function flattenWithTypes(obj, prefix = '', result = {}) { for (const key in obj) { if (Object.hasOwn(obj, key)) { const newKey = prefix ? `${prefix}.${key}` : key; if (obj[key] !== null && typeof obj[key] === 'object' && !Array.isArray(obj[key])) { flattenWithTypes(obj[key], newKey, result); } else { // Preserve the original type result[newKey] = obj[key]; } } } return result; } const extractConditions = (conditions) => { const accumulatedClauses = []; if (!conditions) { return accumulatedClauses; } for (const clause of conditions) { if (!clause) { continue; } const clauseConditions = extractClauseConditions(clause); accumulatedClauses.push(...clauseConditions); } return removeDuplicateConditions(accumulatedClauses); }; exports.extractConditions = extractConditions; function extractClauseConditions(clause) { const conditions = []; for (const element of Object.keys(clause)) { const value = clause[element]; if ((0, isAnyObject_1.isAnyObject)(value)) { const objectConditions = extractObjectConditions(element, value); conditions.push(...objectConditions); } else { conditions.push({ operator: types_1.LogicOperator.Equals, element, value, }); } } return conditions; } function extractObjectConditions(initialKey, value) { const conditions = []; const flatten = flattenWithTypes(value); for (const key of Object.keys(flatten)) { const transformedKey = key.replace(/.[0-9]/g, ''); const condition = createConditionFromKey(initialKey, key, transformedKey, value, flatten); if (condition) { conditions.push(condition); } } return conditions; } function createConditionFromKey(initialKey, key, transformedKey, originalValue, flatten) { // Handle direct operator // Check if transformedKey is a value in LogicOperator enum const operatorValue = Object.values(types_1.LogicOperator).find(val => val === transformedKey); if (operatorValue) { return createOperatorCondition(initialKey, transformedKey, key, originalValue, flatten); } // Handle nested operator if (transformedKey.includes('.')) { return createNestedCondition(initialKey, key, flatten); } // Default to equals operator return { operator: types_1.LogicOperator.Equals, element: `${initialKey}.${transformedKey}`, value: flatten[key], }; } function createOperatorCondition(initialKey, operatorKey, flatKey, originalValue, flatten) { // Find the operator by its value (e.g., 'in' -> LogicOperator.In) const operatorEntry = Object.entries(types_1.LogicOperator).find(([_key, value]) => value === operatorKey); if (!operatorEntry) { throw new Error(`Unknown operator: ${operatorKey}`); } const operator = operatorEntry[1]; // Special handling for IN and NOT IN operators if (operator === types_1.LogicOperator.In || operator === types_1.LogicOperator.NotIn) { return { operator, element: initialKey, value: originalValue[operatorKey], }; } return { operator, element: initialKey, value: flatten[flatKey], }; } function createNestedCondition(initialKey, key, flatten) { const parts = key.split('.'); const possibleOperator = parts[parts.length - 1]; if (!possibleOperator) { return null; } // Check if possibleOperator is a value in LogicOperator enum const operatorEntry = Object.entries(types_1.LogicOperator).find(([_key, value]) => value === possibleOperator); if (operatorEntry) { const elementPath = key.substring(0, key.length - possibleOperator.length - 1); return { operator: operatorEntry[1], element: `${initialKey}.${elementPath}`, value: flatten[key], }; } return { operator: types_1.LogicOperator.Equals, element: `${initialKey}.${key}`, value: flatten[key], }; } function removeDuplicateConditions(conditions) { const seen = new Set(); return conditions.filter(condition => { const key = JSON.stringify(condition); if (seen.has(key)) { return false; } seen.add(key); return true; }); } //# sourceMappingURL=extractConditions.js.map