Skip to content

Instantly share code, notes, and snippets.

@demiurg
Last active July 6, 2018 20:45
Show Gist options
  • Save demiurg/1d901ac7b0093147a5f7ccd7265f892b to your computer and use it in GitHub Desktop.
Save demiurg/1d901ac7b0093147a5f7ccd7265f892b to your computer and use it in GitHub Desktop.
JavaScript FUN
filterSites = (sites, terms) => {
var REDUCERS = {
'AND': (acc, cur) => acc && cur,
'OR': (acc, cur) => acc || cur
};
var filtered_sites = Object.values(sites).filter((s) => {
if (s && typeof s == 'object'){
var props = s.properties;
var truths = terms.map((term, idx) => {
var key = term[1];
var termFilter = this.typeFilter[term[0]];
var searchTerm = term[2];
var reducer = term[3];
return [(
typeof props == 'object' &&
props != null &&
props.hasOwnProperty(key) &&
termFilter(props[key], searchTerm)
), reducer];
});
return truths.reduce(
(acc, cur) => REDUCERS[cur[1]](acc, cur[0]),
[true, 'OR']
);
}
return true;
});
return filtered_sites;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment