fql-toolkit
Version:
40 lines • 1.31 kB
JavaScript
import FQLError from './errors.js';
const VALID_OPERATORS = [
'=', '!=', '<>', '<', '>', '<=', '>=',
];
/**
* Builds an internal FQL condition array from a plain-string triple.
*
* @example
* buildCondition('age', '>', 18)
* → ['>', ['symb', 'age'], 18]
*
* buildCondition('owner.name', '=', 'Ana')
* → ['=', ['symb', 'owner', 'name'], 'Ana']
*/
export function buildCondition(field, operator, value) {
if (typeof field !== 'string' || field.trim() === '') {
throw new FQLError('Condition field must be a non-empty string');
}
if (!VALID_OPERATORS.includes(operator)) {
throw new FQLError(`Invalid operator "${operator}". Valid operators: ${VALID_OPERATORS.join(', ')}`);
}
const fieldExpr = ['symb', ...field.split('.')];
return [operator, fieldExpr, value];
}
/**
* Combines multiple condition arrays into a single AND expression.
*
* @example
* mergeConditions([cond1]) → cond1
* mergeConditions([cond1, cond2]) → ['and', cond1, cond2]
* mergeConditions([]) → null
*/
export function mergeConditions(conditions) {
if (conditions.length === 0)
return null;
if (conditions.length === 1)
return conditions[0];
return ['and', ...conditions];
}
//# sourceMappingURL=conditions.js.map