npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

consist

v2.0.2

Published

A library for serving well ordered conditional logic

Readme

Consist

npm version

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 consist

Or:

yarn add consist

Quick 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)); // true

API

Below is a brief overview of the primary functions. For more examples, see the Examples section.

  1. operationList An 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.


  1. checkCondition Evaluates 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 true or false.
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)

  1. checkConditions Evaluates multiple conditions in series. It returns true only 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)); // true

If any one of the conditions does not match its expected result, checkConditions returns false.


  1. checkObjectCondition Similar to checkCondition, 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"]));    // true

Usage:

checkObjectCondition(
  objectToCheck,
  [key, operation, compareTo, expectedResult?]
)

  1. checkObjectConditions Evaluates multiple conditions against the same object. Returns true only if all conditions pass.
import { checkObjectConditions } from 'consist';

const obj = { firstName: "sam", age: 30 };

console.log(
  checkObjectConditions(obj, [
    ["firstName", "=", "sam"],
    ["age", "=", 30],
  ])
); // true

  1. filterByObjectCondition Filters 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" }
// ]

  1. filterByObjectConditions Filters 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: true

Complex 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

  1. Form Validation: Dynamically show or hide form fields based on user input.
  2. Search & Filtering: Filter lists of data (e.g., e-commerce product lists) by user-selected criteria.
  3. Conditional Rendering: Toggle UI components by checking multiple properties or states.
  4. 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.