beater-matcher
v0.2.0
Published
A matcher library for beater.
Downloads
7
Maintainers
Readme
beater-matcher
A matcher library for beater.
Installation
$ npm install --save-dev beater-matcher
Usage
import assert from 'assert';
import {
any,
anyArray,
anyNumber,
anyString,
anyObject,
match,
matchString,
partialObject,
} from 'beater-matcher';
const value = {
a: 123,
b: 'abc',
c: [1, 2, 3],
d: { a: 1, b: 2, c: 3 }
};
// (basic matchers)
// value as matcher
assert(match(value, value));
// (pre-defined matchers)
// any matcher
assert(match(value, any()));
// anyXXX matcher
assert(match(
value,
{
a: anyNumber(),
b: anyString(),
c: anyArray(),
d: anyObject()
}
));
// partialObject and matchString matcher
assert(match(value, partialObject({ b: matchString(/^ab/) })));
// (custom matcher)
const abcMatcher = defineMatcher(
(value: unknown): boolean => {
return typeof value === 'string' && value.startsWith('ABC');
}
);
assert(match('ABCDEF', abcMatcher));