Pure Conditions Utility Functions
pure-conditions is a JavaScript utility library providing a collection of pure, isolated functions designed to simplify common conditional logic. It offers methods for tasks like checking for duplicates in arrays, determining if an object or array is empty, validating email formats, and string manipulations such as checking if a string ends with a specific substring. The current stable version is 1.2.3. As a small, focused utility, it doesn't adhere to a strict release cadence but updates as new common conditions are identified or minor improvements are made. Its key differentiators include its adherence to pure function principles (no side effects, predictable output) and the complete isolation of functions, which benefits bundlers and compilers by allowing for efficient tree-shaking and minimal bundle sizes. It focuses on functional programming paradigms for robustness and testability.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'hasDuplicates')
cause Attempting to call `hasDuplicates` on an `undefined` or incorrectly imported object, often due to a wrong import statement (e.g., default import or no destructuring).fixEnsure you are using named imports with destructuring: `import { hasDuplicates } from 'pure-conditions';` or `const { hasDuplicates } = require('pure-conditions');` -
SyntaxError: Unexpected token '{'cause Using `import { someFunction } from 'pure-conditions';` in a CommonJS module context (a file without `'type': 'module'` in `package.json` and `.js` extension, or a `.cjs` file).fixEither convert your file/project to an ESM module by setting `'type': 'module'` in `package.json` or rename the file to `.mjs`, or use CommonJS `require`: `const { someFunction } = require('pure-conditions');`
Warnings
- gotcha The library primarily uses named exports. Attempting to use a default import (e.g., `import PConditions from 'pure-conditions'`) will result in `undefined` or an error, as there is no default export.
- gotcha As a 'pure' utility, functions are designed for immutable operations. While convenient, ensure you understand that these functions do not modify their inputs, but rather return a boolean or new value based on the condition.
- gotcha Older Node.js environments or build setups might require specific configuration for ESM `import` statements. The README's examples use CommonJS `require`, which is universally supported but less modern.
Install
-
npm install pure-conditions -
yarn add pure-conditions -
pnpm add pure-conditions
Imports
- hasDuplicates
const { hasDuplicates } = require('pure-conditions');import { hasDuplicates } from 'pure-conditions'; - isEmpty
import isEmpty from 'pure-conditions'; // Incorrect default import
import { isEmpty } from 'pure-conditions'; - isEmail
require('pure-conditions').isEmail;import { isEmail } from 'pure-conditions';
Quickstart
import { hasDuplicates, isEmpty, endsWith, isEmail } from 'pure-conditions';
console.log('--- pure-conditions Quickstart ---');
// Check for duplicates in an array
const listWithDuplicates = ['apple', 'banana', 'apple'];
console.log(`Has duplicates (['apple', 'banana', 'apple']): ${hasDuplicates(listWithDuplicates)}`);
// Check if an object is empty
const emptyObject = {};
const filledObject = { id: 1 };
console.log(`Is {} empty? ${isEmpty(emptyObject)}`);
console.log(`Is { id: 1 } empty? ${isEmpty(filledObject)}`);
// Check if a string ends with another string
const sentence = 'Hello, world!';
console.log(`'Hello, world!' ends with 'world!'? ${endsWith(sentence, 'world!')}`);
console.log(`'Hello, world!' ends with 'universe'? ${endsWith(sentence, 'universe')}`);
// Validate an email address
const validEmail = 'test@example.com';
const invalidEmail = 'invalid-email';
console.log(`'test@example.com' is a valid email? ${isEmail(validEmail)}`);
console.log(`'invalid-email' is a valid email? ${isEmail(invalidEmail)}`);