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'); // falseReference
function isLike(pattern: string | RegExp): (input: string) => booleanParameters
| Parameter | Type | Description |
|---|---|---|
pattern | string | RegExp | Pattern 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'); // trueExample: 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'); // falseExample: 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'); // falseExample: Using string patterns
import { isLike } from '@accelint/predicates';
// String patterns are converted to RegExp
const hasNumbers = isLike('[0-9]');
hasNumbers('abc123'); // true
hasNumbers('abcdef'); // falseGood to know: When passing strings as patterns, they are converted to
RegExpusingnew RegExp(pattern). UseRegExpobjects directly for better control over flags like case-insensitivity (/pattern/i).
Related
- isNotLike - Inverted pattern matching predicate
- doesStartWith - Check string prefix
- doesEndWith - Check string suffix
- isString - Type guard for strings
- isIn - Array membership check