Standard Toolkit
Packages@accelint/predicates

isLike

Curried predicate for testing strings against patterns and regular expressions

Creates a predicate that determines if a string matches a given pattern or regular expression.

Usage

import { isLike } from '@accelint/predicates';

const isEmail = isLike(/^[^\s@]+@[^\s@]+\.[^\s@]+$/);
isEmail('user@example.com'); // true
isEmail('invalid-email');    // false

Reference

function isLike(pattern: string | RegExp): (input: string) => boolean

Parameters

ParameterTypeDescription
patternstring | RegExpPattern to match (strings are converted to RegExp)

Returns

Curried function that accepts a string and returns true if it matches the pattern.

Examples

Example: Email validation

import { isLike } from '@accelint/predicates';

const isEmail = isLike(/^[^\s@]+@[^\s@]+\.[^\s@]+$/);

isEmail('user@example.com'); // true
isEmail('invalid-email');    // false
isEmail('user@domain.co.uk'); // true

Example: File extension filtering

import { isLike } from '@accelint/predicates';

const isScriptFile = isLike(/\.(js|ts|jsx|tsx)$/);

const files = ['app.js', 'style.css', 'main.ts', 'README.md'];
const scripts = files.filter(isScriptFile);
// ['app.js', 'main.ts']

Example: URL validation

import { isLike } from '@accelint/predicates';

const isHttpUrl = isLike(/^https?:\/\/.+/);

isHttpUrl('https://example.com'); // true
isHttpUrl('http://example.com');  // true
isHttpUrl('ftp://example.com');   // false

Example: Phone number pattern

import { isLike } from '@accelint/predicates';

const isPhoneNumber = isLike(/^\d{3}-\d{3}-\d{4}$/);

isPhoneNumber('555-123-4567'); // true
isPhoneNumber('555.123.4567'); // false
isPhoneNumber('5551234567');   // false

Example: Using string patterns

import { isLike } from '@accelint/predicates';

// String patterns are converted to RegExp
const hasNumbers = isLike('[0-9]');

hasNumbers('abc123'); // true
hasNumbers('abcdef'); // false

Good to know: When passing strings as patterns, they are converted to RegExp using new RegExp(pattern). Use RegExp objects directly for better control over flags like case-insensitivity (/pattern/i).

On this page