consist
v2.0.2
Published
A library for serving well ordered conditional logic
Readme
Consist
Consist is a lightweight collection of pure JavaScript utility functions that help you organize and manage conditional logic in your applications. Whether you're working with form validations, filtering arrays, or checking the properties of objects, Consist provides a flexible and intuitive API to keep your logic clean, modular, and consistent.
Table of Contents
Features
- Lightweight & Pure JS: No external dependencies, easy to integrate into any JavaScript/TypeScript project.
- Extensive Operation Support: Over a dozen built-in comparison operations (e.g.,
=,!=,<,>,<=,>=,contains,startsWith, and more). - Reusable Logic: Makes complex condition-checking more readable and maintainable.
- Object Support: Check and validate object properties with ease.
- Array Filtering: Filter arrays of objects based on single or multiple conditions.
Installation
Install Consist using your favorite package manager:
npm install consistOr:
yarn add consistQuick Start
import { isEqualTo, checkCondition, checkConditions } from 'consist';
// Single check
console.log(isEqualTo('a', 'a')); // true
// Simple condition check
console.log(checkCondition([5, "=", 5])); // true
// Multiple conditions in series
const conditions = [
[5, "=", 5, true], // expected to be true
["hello", "startsWith", "h", true], // expected to be true
[10, ">", 5, true] // expected to be true
];
console.log(checkConditions(conditions)); // trueAPI
Below is a brief overview of the primary functions. For more examples, see the Examples section.
operationListAn array of all available operations and their associated functions.
import { operationList } from 'consist';
console.log(operationList);
/* [
{ operation: "=", function: [Function] },
{ operation: "!=", function: [Function] },
{ operation: ">", function: [Function] },
...
] */You can use this list to dynamically choose operations or integrate them into a UI for condition-building.
checkConditionEvaluates a single condition. A condition is usually in the format:
[value, operation, compareTo, expectedResult?]- value: The data to be checked.
- operation: A string representing the operation to perform (e.g.,
"=","!=",">","exists", etc.). - compareTo (optional): A value to compare with.
- expectedResult (optional, boolean): Indicates what the condition expects to be. If omitted, it simply returns whether the condition is
trueorfalse.
import { checkCondition } from 'consist';
console.log(checkCondition([5, "=", 5])); // true
console.log(checkCondition([5, "<", 4])); // false
// With an expected result:
console.log(checkCondition([5, "=", 5, true])); // true (condition is true, matching expected)
console.log(checkCondition([5, "=", 5, false])); // false (condition is true, not matching expected)checkConditionsEvaluates multiple conditions in series. It returnstrueonly if all conditions pass (i.e., they match their expected results).
import { checkConditions } from 'consist';
const conditions = [
[5, "=", 5, true], // expects true
["hello", "startsWith", "h", true], // expects true
[10, ">", 5, true], // expects true
];
console.log(checkConditions(conditions)); // trueIf any one of the conditions does not match its expected result, checkConditions returns false.
checkObjectConditionSimilar tocheckCondition, but it evaluates properties within an object.
import { checkObjectCondition } from 'consist';
const obj = { firstName: "sam", age: 30 };
console.log(checkObjectCondition(obj, ["firstName", "=", "sam"])); // true
console.log(checkObjectCondition(obj, ["age", ">", 18])); // true
console.log(checkObjectCondition(obj, ["firstName", "exists"])); // trueUsage:
checkObjectCondition(
objectToCheck,
[key, operation, compareTo, expectedResult?]
)checkObjectConditionsEvaluates multiple conditions against the same object. Returnstrueonly if all conditions pass.
import { checkObjectConditions } from 'consist';
const obj = { firstName: "sam", age: 30 };
console.log(
checkObjectConditions(obj, [
["firstName", "=", "sam"],
["age", "=", 30],
])
); // truefilterByObjectConditionFilters an array of objects based on a single condition.
import { filterByObjectCondition } from 'consist';
const sampleData = [
{ name: "Apple", category: "fruit" },
{ name: "Carrot", category: "vegetable" },
{ name: "Banana", category: "fruit" },
{ name: "Cherry", category: "fruit" },
];
const result = filterByObjectCondition(sampleData, ["category", "=", "fruit"]);
console.log(result);
// [
// { name: "Apple", category: "fruit" },
// { name: "Banana", category: "fruit" },
// { name: "Cherry", category: "fruit" }
// ]filterByObjectConditionsFilters an array of objects based on multiple conditions in series.
import { filterByObjectConditions } from 'consist';
const sampleData = [
{ name: "Apple", category: "fruit" },
{ name: "Carrot", category: "vegetable" },
{ name: "Banana", category: "fruit" },
{ name: "Cherry", category: "fruit" },
];
const result = filterByObjectConditions(sampleData, [
["category", "=", "fruit"],
["name", "startsWith", "C"],
]);
console.log(result);
// [{ name: "Cherry", category: "fruit" }]Examples
Dynamic Condition Checks
import { checkCondition } from 'consist';
// Single condition with logical operators
console.log(
checkCondition([[true, "=", false], "or", [1, "!=", 2]])
);
// Explanation:
// - [true, "=", false] -> false
// - [1, "!=", 2] -> true
// "false or true" = true
// Overall result: trueComplex Form Validations
You can use Consist for advanced form validations. For instance, you might have fields like age, country, acceptTerms, and so on. Simply build conditions and pass them to checkObjectConditions or checkConditions to validate the entire form in one go.
Filtering Data Sets
When you need to filter large arrays of objects by multiple properties (e.g., searching for products within a category, above a certain price, or matching a search term), filterByObjectConditions comes in handy.
Use Cases
- Form Validation: Dynamically show or hide form fields based on user input.
- Search & Filtering: Filter lists of data (e.g., e-commerce product lists) by user-selected criteria.
- Conditional Rendering: Toggle UI components by checking multiple properties or states.
- Data Processing Pipelines: Apply transformations or validations on streams of data.
License
This project is licensed under the MIT License. Feel free to use it in your personal or commercial projects.
Happy Coding! If you have any questions or run into any issues, feel free to open an issue on GitHub or reach out via a pull request.
