New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@timkit/conditions

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@timkit/conditions

A flexible TypeScript condition checker for evaluating complex conditions against objects

latest
Source
npmnpm
Version
1.1.1
Version published
Maintainers
1
Created
Source

conditions

A flexible and powerful condition checker for TypeScript that allows evaluating complex conditions against objects.

Installation

Install the package using npm:

npm install @timkit/conditions

Or using yarn:

yarn add @timkit/conditions

Usage

import { checkConditions } from '@timkit/conditions';

const user = {
  name: 'John Doe',
  age: 30,
  email: 'john.doe@example.com',
  address: {
    city: 'New York',
    state: 'NY'
  },
  tags: ['developer', 'javascript', 'typescript'],
  active: true
};

// Simple condition
const isUser30YearsOld = checkConditions(user, ['age == 30']); // true

// Negated condition
const isUserNot40YearsOld = checkConditions(user, ['age != 40']); // true 
const isUserNotJane = checkConditions(user, ['name !contains Jane']); // true

// Multiple conditions with OR logic (default)
const isActiveOrOver25 = checkConditions(user, [
  'active == true',
  'age > 25'
]); // true (both conditions are true)

// AND conditions (nested array)
const isActiveAndOver25 = checkConditions(user, [
  ['active == true', 'age > 25']
]); // true (both conditions are true)

// Complex nested conditions (AND and OR combined)
// (active == true AND age > 25) OR (name contains Smith AND city == Chicago)
const complexCondition = checkConditions(user, [
  ['active == true', 'age > 25'],
  ['name contains Smith', 'address.city == Chicago']
]); // true (first condition group is true)

Supported Operators

OperatorDescriptionExample
=Equality'age = 30'
==Equality'age == 30'
>Greater than'age > 25'
<Less than'age < 35'
>=Greater than or equal'age >= 30'
<=Less than or equal'age <= 30'
containsString contains or array includes'name contains John', 'tags contains typescript'
matchesString matches regex pattern'email matches .*@example\\.com'
inValue is in list'age in (25, 30, 35)'
isBlankField is empty, null, or undefined'address.zipcode isBlank'
isEmailField is a valid email address'email isEmail'
isUrlField is a valid URL'website isUrl'
isBoolField is a boolean'active isBool'
isNumberField is a number'age isNumber'
isIntegerField is an integer'count isInteger'
isFloatField is a floating-point number'price isFloat'
isDateField is a valid date'birthdate isDate'
isTimeField is a valid time (HH:MM, HH:MM:SS)'meetingTime isTime'
isDateTimeField is a valid date and time'createdAt isDateTime'
isRequiredField is not empty, null, or undefined'name isRequired'
isBetweenNumber is between min and max (inclusive)'age isBetween 25, 30'
startsWithString starts with a substring'name startsWith John'
endsWithString ends with a substring'email endsWith example.com'
isArrayField is an array'tags isArray'
isObjectField is an object'address isObject'
isCombined validation (see below)`'age is required
!operatorNegation of most operators'name !contains Smith', 'age !in (40, 50)', 'email !isEmail'

Negating Conditions

You can negate most operators by prefixing them with an exclamation mark (!). This reverses the result of the operator.

const user = { name: 'John Doe', age: 30, tags: ['a', 'b'], emptyField: '' };

// Standard contains
checkConditions(user, ['name contains John']); // true
// Negated contains
checkConditions(user, ['name !contains Jane']); // true

// Standard isBlank
checkConditions(user, ['emptyField isBlank']); // true
// Negated isBlank
checkConditions(user, ['name !isBlank']); // true

// Negation with lists
checkConditions(user, ['age !in (40, 50, 60)']); // true
checkConditions(user, ['age !in (20, 30, 40)']); // false

// Negation with type checks
checkConditions(user, ['name !isNumber']); // true
checkConditions(user, ['age !isNumber']); // false

// Negation works within AND/OR blocks
// (name !contains Jane) AND (age == 30)
checkConditions(user, [['name !contains Jane', 'age == 30']]); // true

Combined Validation with 'is' operator

The is operator allows you to apply multiple validation rules to a field in a single condition. Rules are separated by the pipe character (|).

// Check if age is required, a number, minimum 18, and maximum 100
checkConditions(user, ['age is required|number|min:18|max:100']);

// Check if email is required and a valid email format
checkConditions(user, ['email is required|email']);

// Check if a string has specific length constraints
checkConditions(user, ['username is required|string|minLength:3|maxLength:20']);

Supported Validations in 'is' operator

ValidationDescriptionExample
requiredField is not empty, null, or undefinedrequired
stringField is a stringstring
numberField is a numbernumber
integerField is an integerinteger
floatField is a floating-point numberfloat
boolField is a booleanbool
emailField is a valid emailemail
urlField is a valid URLurl
dateField is a valid datedate
timeField is a valid timetime
dateTimeField is a valid date and timedateTime
arrayField is an arrayarray
objectField is an objectobject
min:valueNumber is greater than or equal to valuemin:18
max:valueNumber is less than or equal to valuemax:100
minLength:valueString or array has minimum lengthminLength:5
maxLength:valueString or array has maximum lengthmaxLength:20
pattern:regexString matches regex patternpattern:^[a-z0-9_]+$
startsWith:valueString starts with substringstartsWith:http
endsWith:valueString ends with substringendsWith:.com
contains:valueString contains substringcontains:example

Logic Operations

  • OR Logic: By default, top-level conditions are combined with OR logic

    // name == John OR age == 30
    ['name == John', 'age == 30']
    
  • AND Logic: Nest conditions in an array for AND logic

    // name == John AND age == 30
    [['name == John', 'age == 30']]
    
  • Complex Logic: Combine AND and OR

    // (name == John AND age == 30) OR (email contains example AND active == true)
    [
      ['name == John', 'age == 30'],
      ['email contains example', 'active == true']
    ]
    

Accessing Nested Properties

You can access nested properties using dot notation:

// Check a nested property
checkConditions(user, ['address.city == New York']); // true

Running Tests

npm test

Keywords

conditions

FAQs

Package last updated on 24 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts