Registry / testing / easy-compare

easy-compare

JSON →
library0.0.11jsnpmunverified

A lightweight JavaScript library (v0.0.11, stable, infrequent updates) for comparing values using MongoDB-style operators. It provides a simple `compare(value, operator)` function supporting comparison operators ($eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $size, $nsize, $regex, $nregex) and logical operators ($and, $or, $all, $not). Key differentiator: JSON-defined operator objects, making it suitable for rule engines or configurable validation. Minimal dependencies, tiny install size.

npm install easy-compare
INSTALL
IMPORT
SIG · EASY-COMPARE
E
easy-compare
testingjavascriptv0.0.11
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import easyCompare from 'easy-compare';
const easy = require('easy-compare'); // CJS require works for Node.js but not ESM
Uses a default export; also export { compare } is available? Actually only default export. Ensure bundler supports CJS.
compare
const { compare } = require('easy-compare');
import { compare } from 'easy-compare'; // Named import not supported
Library only has a default export function. Destructuring from default works in CJS but not ESM.
default (ESM)
import easy from 'easy-compare'; easy.compare(5, { $gte: 2 });
import easyCompare from 'easy-compare'; easyCompare(5, { $gte: 2 }); // Calling the default directly won't work; it's a function but compare is a method
Default export is an object with a `compare` method, not a function. Use `easy.compare(...)`.

Shows basic usage of compare() with various operators: $gte, $and, $in, $regex.

const easy = require('easy-compare'); // Single operator const result1 = easy.compare(15, { $gte: 10 }); // true // Multiple operators with $and const ageCheck = { $and: { $gte: 18, $lte: 65 } }; console.log(easy.compare(25, ageCheck)); // true console.log(easy.compare(70, ageCheck)); // false // Using $in const names = ['Alice', 'Bob', 'Charlie']; console.log(easy.compare(names, { $in: 'Bob' })); // true // Using $regex console.log(easy.compare('hello world', { $regex: /^hello/ })); // true
Debug
Known issues
gotchaOperator object must contain only one top-level key unless using $and/$or. Multiple keys cause unexpected behavior.
fix
Wrap multiple operators in $and or $or, e.g., { $and: { $gt: 5, $lt: 10 } }.
affects: >=0.0.0
deprecatedPackage has not been updated since 2019. May not support newer Node.js versions or modern JS features.
fix
Consider using alternatives like 'sift' or MongoDB's own query engine if acting as a dependency.
affects: >=0.0.0
gotcha$in and $nin operate on the value as an array; passing a string will iterate over characters.
fix
Ensure the value is an array for $in/$nin, or use $regex for string containment.
affects: >=0.0.0
gotchaDefault export is an object { compare }, not a function. Direct invocation like easy(5, {}) will fail.
fix
Use easy.compare(value, operator).
affects: >=0.0.0
gotchaThe library uses loose equality (==) for $eq. May cause type coercion surprises.
fix
Use strict equality checks separately if needed.
affects: >=0.0.1
Errors
Common errors & fixes
TypeError: easy is not a function
Calling the default export as a function instead of using the compare method.
fix
Use easy.compare(value, operator) instead of easy(value, operator).
Uncaught TypeError: Cannot destructure property 'compare' of '...' as it is undefined.
Attempting named import { compare } from 'easy-compare' in ESM contexts.
fix
Use default import: import easy from 'easy-compare'; then easy.compare().
SyntaxError: Unexpected token '$and'
Trying to use multiple top-level keys without wrapping in $and/$or.
fix
Wrap multiple operators: { $and: { $gt: 5, $lt: 10 } }.
ReferenceError: require is not defined
Using CJS require() in an ESM environment.
fix
Use import easy from 'easy-compare'; or switch to CommonJS.
Upgrade
Version history
0.0.11latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
easy-compare — npm install easy-compare · libregistry