UNPKG

beater-matcher

Version:
58 lines (43 loc) 1.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.match = exports.defineMatcher = void 0; var _helper = require("./helper"); const match = (value, matcher) => { switch (typeof matcher) { case "bigint": return matcher === value; case "boolean": return matcher === value; case "function": return isMatcherFn(matcher) ? matcher.call(null, value) : matcher === value; case "number": return matcher === value; case "object": if (matcher === null) { return matcher === value; } else if (Array.isArray(matcher)) { return Array.isArray(value) && matcher.every((m, i) => match(value[i], m)); } else { return (0, _helper.isObject)(value) && Object.entries(matcher).every(([k, v]) => match(value[k], v)); } case "undefined": return matcher === value; case "string": return matcher === value; case "symbol": return matcher === value; default: throw new Error(`unknown matcher type '${typeof matcher}'`); } }; exports.match = match; const isMatcherFn = f => f.__matcher__ === true; const defineMatcher = f => { Object.defineProperty(f, "__matcher__", { value: true }); return f; }; exports.defineMatcher = defineMatcher;