An abstraction to help you create a simple pattern matching API
interface Member extends Matchable<string> { name: string;}const members: Member[] = [ { matches: (p) => p.endsWith('.ts'), name: 'typescript' }, { matches: (p) => p.endsWith('.js'), name: 'javascript' },];const matcher = new Matcher(members);matcher.select('test.js')?.name; // javascript Copy
interface Member extends Matchable<string> { name: string;}const members: Member[] = [ { matches: (p) => p.endsWith('.ts'), name: 'typescript' }, { matches: (p) => p.endsWith('.js'), name: 'javascript' },];const matcher = new Matcher(members);matcher.select('test.js')?.name; // javascript
Readonly
Calls .select() to try and find a match for any of the members. If it can it calls matches otherwise it call noMatch
.select()
matches
noMatch
Called with the matching member if one exists
Called when no match was found
matcher.match( 'name', (member) => member.doSomething(), () => throw Error('Couldn't do anything!')) Copy
matcher.match( 'name', (member) => member.doSomething(), () => throw Error('Couldn't do anything!'))
Iterate over the members and return the first member that matches s.
s
const member = matcher.select('name');member.doSomething(); Copy
const member = matcher.select('name');member.doSomething();
Calls .match() to try and find a match if there is no match it returns the provided default value.
.match()
The value returned when the pattern provided doesn't match any member.
const member = matcher.select('', { matches: () => true, name: 'default' }); Copy
const member = matcher.select('', { matches: () => true, name: 'default' });
An abstraction to help you create a simple pattern matching API
Usage