(key, value, utils)
| 3 | |
| 4 | // loops over matching filters and passes options to each filter, returning the mapped results |
| 5 | export const applyFilterChain = (key, value, utils) => |
| 6 | new Promise((resolve, reject) => { |
| 7 | // find matching filters for this key |
| 8 | const matchingFilters = filters |
| 9 | .filter(f => f.key === key) |
| 10 | .map(f => f.cb); |
| 11 | |
| 12 | // resolve now |
| 13 | if (matchingFilters.length === 0) { |
| 14 | resolve(value); |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | // first filter to kick things of |
| 19 | const initialFilter = matchingFilters.shift(); |
| 20 | |
| 21 | // chain filters |
| 22 | matchingFilters |
| 23 | .reduce( |
| 24 | // loop over promises passing value to next promise |
| 25 | (current, next) => current.then(value => next(value, utils)), |
| 26 | |
| 27 | // call initial filter, will return a promise |
| 28 | initialFilter(value, utils) |
| 29 | |
| 30 | // all executed |
| 31 | ) |
| 32 | .then(value => resolve(value)) |
| 33 | .catch(error => reject(error)); |
| 34 | }); |
| 35 | |
| 36 | export const applyFilters = (key, value, utils) => |
| 37 | filters.filter(f => f.key === key).map(f => f.cb(value, utils)); |
no test coverage detected