UNPKG

ow

Version:

Function argument validation for humans

33 lines (30 loc) 1.09 kB
import hasItems from '../utils/has-items.js'; import { Predicate } from './predicate.js'; export class WeakMapPredicate extends Predicate { /** @hidden */ constructor(options) { super('WeakMap', options); } /** Test a WeakMap to include all the provided keys. The keys are tested by identity, not structure. @param keys - The keys that should be a key in the WeakMap. */ hasKeys(...keys) { return this.addValidator({ message: (_, label, missingKeys) => `Expected ${label} to have keys \`${JSON.stringify(missingKeys)}\``, validator: map => hasItems(map, keys), }); } /** Test a WeakMap to include any of the provided keys. The keys are tested by identity, not structure. @param keys - The keys that could be a key in the WeakMap. */ hasAnyKeys(...keys) { return this.addValidator({ message: (_, label) => `Expected ${label} to have any key of \`${JSON.stringify(keys)}\``, validator: map => keys.some(key => map.has(key)), }); } }