-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
27 lines (21 loc) · 965 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const { PASS, UNKNOWN, FAIL } = require('./status');
const Filter = require('./filter');
const defaultMap = e => e;
const Matcher = (filterString, collection, filterBy = defaultMap) => {
const filters = filterString.split(' ');
const requirements = filters.reduce((r, f) => { r[f] = UNKNOWN; return r; }, {}); // eslint-disable-line no-param-reassign
const requirementsKeys = Object.keys(requirements);
return collection.filter((element) => {
requirementsKeys.forEach((key) => { requirements[key] = UNKNOWN; });
const targets = filterBy(element).split(' ');
targets.forEach(target =>
requirementsKeys.forEach((key) => {
if (requirements[key] === FAIL) return;
const test = Filter(key).matches(target);
if (test !== UNKNOWN) requirements[key] = test;
}));
// Assert all the requirements are met
return requirementsKeys.every(key => requirements[key] === PASS);
});
};
module.exports = Matcher;