json-query-chain/index.js
sharpshark28 7c75924c32 v1.0.0
2017-05-26 06:19:13 -05:00

47 lines
1.1 KiB
JavaScript

module.exports = class Query {
constructor (data) {
this.data = data.map(item => {
item.sortScore = 0;
return item;
});
};
get results () {
return this.data;
};
search (key, term, score = 0) {
switch (typeof term) {
case 'boolean':
this.data = this.data.filter(item => item[key] === term);
break;
case 'string':
if (term.length >= 3) {
this.data = this.data.filter(item => {
let regFind = new RegExp(term, 'gi');
let termMatches = (item[key].match(regFind) || []).length;
item.sortScore += termMatches;
return termMatches;
});
}
break;
}
return this;
};
sort (key = 'sortScore') {
this.data = this.data.sort((a, b) => {
if (a[key] < b[key]) return -1;
if (a[key] > b[key]) return 1;
return 0;
});
return this;
};
paginate (page = 1, perPage = 10) {
let min = page * perPage - perPage;
let max = min + perPage;
this.data = this.data.slice(min, max);
return this;
};
};