Created
May 9, 2017 21:20
-
-
Save jcarley/83860df6eadb2c2dbd46ba971c270fcf to your computer and use it in GitHub Desktop.
A sample query object that uses functions to filter a collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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