.eslintrc.js | ||
.gitignore | ||
.travis.yml | ||
coverage.svg | ||
index.js | ||
index.test.js | ||
package.json | ||
README.md | ||
testdata.json |
json-query-chain
Chain queries onto arrays and arrays of objects to return precise results. See example usages in the tests running on this test data.
Usage
import Query from 'json-query-chain';
let query = new Query(someJsonData)
.search('isActiveUser', true)
.results;
console.log('Results: ', query);
Chainable Methods
Search
By Boolean
Acts as a smart filter returning only elements who's key matches the expected result.
.search(true, 'isActiveUser')
By String
Sorts and filters out any elements in the array not matching the requested value while attempting to raise the best results to the top (most frequent number of occurrences).
.search('steele', 'name')
Filter
Simpler version of search using a custom function in the chain.
.filter(a => a.age >= 21)
By Key
Like .filter()
, but narrowed down by key.
.filterBy('age', x => x >= 21)
Sort
A chainable version of javascript's built in array sort.
.sort((a, b) => a > b)
By Boolean
Abstracted sort by matching a key to a boolean.
.sortBy('isActiveUser')
By String
Sorts alphabetically based on key.
.sortBy('name')
By Number
Sorts by ascending numerical order based on key.
.sortBy('netWorth')
Pagination
Page 1 with 5 results per page.
.paginate(1, 5)
Page 2 with default of 10 results per page.
.paginate(2)
Tests
npm test
runs tests through Jest