mirror of https://github.com/lukechilds/ow.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
7 years ago
|
import {Predicate, Context} from './predicate';
|
||
|
|
||
|
export class WeakSetPredicate extends Predicate<WeakSet<any>> {
|
||
|
constructor(context?: Context) {
|
||
|
super('weakSet', context);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test a WeakSet to include all the provided items. The items are tested by identity, not structure.
|
||
|
*
|
||
|
* @param items The items that should be a item in the WeakSet.
|
||
|
*/
|
||
|
has(...items: any[]) {
|
||
|
const missingItems: any[] = [];
|
||
|
|
||
|
return this.addValidator({
|
||
|
message: () => `Expected WeakSet to have items \`${JSON.stringify(missingItems)}\``,
|
||
|
validator: set => {
|
||
|
for (const key of items) {
|
||
|
if (set.has(key)) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
missingItems.push(key);
|
||
|
|
||
|
if (missingItems.length === 5) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return missingItems.length === 0;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test a WeakSet to include any of the provided items. The items are tested by identity, not structure.
|
||
|
*
|
||
|
* @param items The items that could be a item in the WeakSet.
|
||
|
*/
|
||
|
hasAny(...items: any[]) {
|
||
|
return this.addValidator({
|
||
|
message: () => `Expected WeakSet to have any item of \`${JSON.stringify(items)}\``,
|
||
|
validator: set => items.some(item => set.has(item))
|
||
|
});
|
||
|
}
|
||
|
}
|