Logic Query Parser

0.0.5 · abandoned · verified Sun Apr 19

The `logic-query-parser` package provides a JavaScript utility for parsing logic search strings into a binary tree representation. It takes a query string, supports `AND`, `OR`, parentheses, and double quotes, and can be configured with custom space characters and a default operator. Additionally, it offers a utility function to convert the binary tree into a more usable JSON query form, simplifying programmatic interpretation. This package is currently at version 0.0.5 and was last published approximately 7 years ago, indicating it is no longer actively maintained. Its primary use case is to transform human-readable logical expressions into a structured data format suitable for backend search or filtering operations.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates parsing a complex logic query into a binary tree and then converting it to a simplified JSON query structure, also showing how to use custom parsing options.

const parser = require('logic-query-parser');

// Parse a complex logic query string into a binary tree
const binaryTree = parser.parse('(welcome OR bye) AND (hello OR ahoy)');

console.log('--- Binary Tree Structure ---');
console.log(JSON.stringify(binaryTree, null, 2));

// Convert the binary tree to a more usable JSON query form
const query = parser.utils.binaryTreeToQueryJson(binaryTree);

console.log('\n--- Converted Query Form ---');
console.log(JSON.stringify(query, null, 2));

// Example with options: custom spaces and default operator
const customParser = require('logic-query-parser');
const options = {
  spaces: [' ', '\t', '-'], // Allow hyphen as a space character
  defaultOperator: 'or' // Use OR by default
};
const customBinaryTree = customParser.parse(options, 'apple banana -orange');

console.log('\n--- Custom Options Example (Binary Tree) ---');
console.log(JSON.stringify(customBinaryTree, null, 2));

view raw JSON →