Logic Query Parser
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
-
TypeError: parser.parse is not a function
cause Attempting to destructure `parse` directly from the `require` statement (e.g., `const { parse } = require('logic-query-parser');`). The `require` call returns the `parser` object itself, and `parse` is a method on that object.fixImport the entire `parser` object first: `const parser = require('logic-query-parser');` then call `parser.parse(...)`. -
ERR_REQUIRE_ESM: require() of ES Module ... from ... not supported.
cause Attempting to import `logic-query-parser` using ES module `import` syntax (`import parser from 'logic-query-parser';`) in a Node.js project configured for CommonJS or in an environment where direct ESM `require` is not enabled for CJS modules.fixUse CommonJS `require` syntax: `const parser = require('logic-query-parser');`. If modern ESM syntax is mandatory, consider a build step (e.g., Webpack, Rollup) to transpile or find an alternative library.
Warnings
- gotcha This package is a CommonJS module and does not natively support ES module (`import`) syntax. Attempting to use `import` statements directly will result in errors in modern Node.js environments unless transpiled or wrapped.
- breaking The package version (0.0.5) is extremely low and was last updated approximately 7 years ago. This indicates an abandoned status, meaning no further updates, bug fixes, or security patches are likely. Production use is risky.
- gotcha The `parser.parse` function expects either a single string argument or an options object as the first argument followed by the string. Passing the string as the first argument when an options object is expected will lead to incorrect parsing.
Install
-
npm install logic-query-parser -
yarn add logic-query-parser -
pnpm add logic-query-parser
Imports
- parser
import parser from 'logic-query-parser';
const parser = require('logic-query-parser'); - parser.parse
const { parse } = require('logic-query-parser'); const binaryTree = parse('hello AND world');const parser = require('logic-query-parser'); const binaryTree = parser.parse('hello AND world'); - parser.utils.binaryTreeToQueryJson
import { utils } from 'logic-query-parser'; const query = utils.binaryTreeToQueryJson(someBinaryTree);const parser = require('logic-query-parser'); const query = parser.utils.binaryTreeToQueryJson(someBinaryTree);
Quickstart
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));