Skip to content

Instantly share code, notes, and snippets.

@jcarley
Created May 9, 2017 21:20
Show Gist options
  • Save jcarley/83860df6eadb2c2dbd46ba971c270fcf to your computer and use it in GitHub Desktop.
Save jcarley/83860df6eadb2c2dbd46ba971c270fcf to your computer and use it in GitHub Desktop.
A sample query object that uses functions to filter a collection
class Query {
constructor(coll) {
this.coll = coll;
}
getCollection() {
return this.coll;
}
find(id) {
return new Query(_.each(this.coll, item => item.id === id));
}
select(predicate) {
return new Query(_.filter(this.coll, predicate));
}
groupBy(fn) {
return new Query(_.groupBy(this.coll, fn));
}
sortBy(valueFn) {
return new Query(_.sortBy(this.coll, valueFn));
}
}
let query = new Query(this.FileCache.getAll());
let results = query
.select((item) => item.title.matches(/regexhere/) && item.headline.empty())
.sortBy((item) => item.createDate)
.getCollection();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment