dynamic-forms / Function

compareValues

Compares two values using the specified comparison operator.

Supports various comparison types including equality, numerical comparisons, string operations, and regular expression matching. Type coercion is applied for numerical and string operations as needed.

Presentation

function compareValues(
  actual: unknown,
  expected: unknown,
  operator: string,
): boolean;

Returns

boolean -

True if the comparison succeeds, false otherwise

Parameters

NameTypeDescription
actual
unknown

The value to compare

expected
unknown

The value to compare against

operator
string

Comparison operator to use

Example usage

// Equality checks
compareValues(10, 10, 'equals')        // true
compareValues('test', 'other', 'notEquals') // true

// Numerical comparisons
compareValues(15, 10, 'greater')       // true
compareValues('5', 3, 'greaterOrEqual') // true (coerced to numbers)

// String operations
compareValues('hello world', 'world', 'contains')   // true
compareValues('prefix-test', 'prefix', 'startsWith') // true
compareValues('test.jpg', '.jpg', 'endsWith')       // true

// Regular expression matching
compareValues('test123', '^test\d+$', 'matches')    // true
compareValues('invalid', '[invalid', 'matches')      // false (invalid regex)