Skip to main content

isLike()

isLike(pattern): (input) => boolean

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

Parameters

pattern

The string pattern or RegExp to use for testing

string | RegExp

Returns

(input): boolean

Parameters

input

string

Returns

boolean

Remarks

  • Pure function with no side effects
  • Accepts both string patterns and RegExp objects
  • String patterns are automatically converted to RegExp using new RegExp(pattern)
  • Uses RegExp.prototype.test() for pattern matching
  • Useful for string validation and array filtering

Example

const isJsFile = isLike(/\.(js|ts)$/);
isJsFile('app.js'); // true
isJsFile('style.css'); // false

const hasNumbers = isLike('[0-9]');
hasNumbers('abc123'); // true
hasNumbers('abcdef'); // false

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

// Useful with arrays
const files = ['app.js', 'style.css', 'main.ts', 'README.md'];
const scriptFiles = files.filter(isLike(/\.(js|ts)$/)); // ['app.js', 'main.ts']