UNPKG

earljs

Version:

Ergonomic, modern and type-safe assertion library

41 lines (40 loc) 1.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.contains = exports.ArrayWithMatcher = void 0; const format_1 = require("../format"); const isEqual_1 = require("../isEqual"); const Base_1 = require("./Base"); class ArrayWithMatcher extends Base_1.Matcher { constructor(expectedItems) { super(); this.expectedItems = expectedItems; } check(actualItems) { if (!Array.isArray(actualItems)) { return false; } return contains(this.expectedItems, actualItems); } toString() { return `[ArrayWith: ${(0, format_1.formatCompact)(this.expectedItems)}]`; } static make(...items) { return new ArrayWithMatcher(items); } } exports.ArrayWithMatcher = ArrayWithMatcher; /** @internal */ function contains(expectedItems, actualItems) { const matchedIndexes = {}; return expectedItems.every((expectedItem) => { const foundIndex = actualItems.findIndex((actualItem, index) => (0, isEqual_1.isEqual)(actualItem, expectedItem) && !matchedIndexes[index]); if (foundIndex !== -1) { matchedIndexes[foundIndex] = true; return true; } else { return false; } }); } exports.contains = contains;