UNPKG

@comodinx/query-filters

Version:

@comodinx/query-filters is a module for parsing filters in string to object.

73 lines (72 loc) 2.68 kB
import { ParserMapMethod, ParserOperator, ParserOptions, ParserResult, ParserResultKey, ParserResultValue, ParserToken } from "./types"; export declare class Parser { private options; private regexpOperatorPrefix; private regexpOperatorSuffix; private regexpFilters; constructor(options?: ParserOptions); static get defaults(): ParserOptions; /** * Public parse * - Legacy: if no OR/group/word-ops, behaves exactly like old parser (split by comma => AND). * - Expr mode: tokenizes + shunting-yard + AST compile. */ parse(str: string): ParserResult | undefined; /** * Tokenizer for: * - AND: ',' OR ' AND ' (spaces required around word operator) * - OR: '|' OR ' OR ' (spaces required around word operator) * - Groups: '(' ')' * * Produces tokens: ATOM, AND, OR, LPAREN, RPAREN */ protected tokenizeExpression(input: string): ParserToken[]; /** * Shunting-yard: tokens -> RPN * precedence: AND (2) > OR (1) */ protected toRpn(tokens: ParserToken[]): ParserToken[]; /** * RPN -> AST * Node shapes: * - { type:'ATOM', obj } * - { type:'AND', items:[...] } * - { type:'OR', items:[...] } */ protected rpnToAst(rpn: ParserToken[]): ParserToken | undefined; /** * Parse atomic condition using existing regex+mappers. * "active eq 1" => { active: { eq: "1" } } */ protected parseAtom(text: string): ParserResult | undefined; /** * Compile AST to final JSON * - OR: { or: [ ... ] } * - AND: * - if possible returns plain merged object (legacy feel) * - if mix with logical nodes, returns { and: [...] } unless it can be flattened as { ...base, or:[...] } */ protected compileAst(node: ParserToken): ParserResult | undefined; /** * Merge plain filter objects preserving your operator-merge behavior: * { description:{li:"%casa"} } + { description:{eq:"depto"} } => { description:{li:"%casa", eq:"depto"} } */ protected mergePlainInto(target: ParserResult, incoming: ParserResult): void; /** * Format filters */ format(filters: ParserResult): string | undefined; /** * Map key */ mapKey(key: ParserResultKey, operator: ParserOperator, method: ParserMapMethod): ParserResultKey; /** * Map value */ mapValue(value: ParserResultValue, operator: ParserOperator, method: ParserMapMethod): ParserResultValue; /** * Map operator */ mapOperator(operator: ParserOperator, method: ParserMapMethod): ParserOperator; protected toStringOperator(operator: ParserOperator): string; }