filterhandler v1.1.0
Filter Handler
Changelog
You can find the changelog here: https://github.com/dhershman1/FilterHandler/blob/master/changelog.md
Setup
Simply require filterhandler and initialize it with some options
const filterhandler = require('filterhandler')(options)
Options
options include:
trackHistory
- keep track of filter and data search historydefault: false
searchableData
- the data filterhandler will be managingsend as an array
pageSize
- how many items should filterhandler limit the return to?default: 10
set tonull
or0
to disable
Usage
const filterhandler = require('filterhandler')({
trackHistory: true,
searchableData: [{name: 'Bob'}, {name: 'Andrew'}, {name: 'Carson'}]
});
//Click sort event happens
filterhandler.sortBy('name');
//Output: [{name: 'Andrew'}, {name: 'Bob'}, {name: 'Carson'}]
//History: {name: [{name: 'Andrew'}, {name: 'Bob'}, {name: 'Carson'}]}
Public Functions
getHistory()
Gets history data and returns it IF data is being tracked, else it will return an empty object
Usage
filterhandler.getHistory();
filterByTerm(term, key)
Filter the array by the given term
term
- The string term to search for
Usage
var mockData = [{
name: 'Billy',
position: 'Kool Kid',
grade: 'G'
}, {
name: 'Albert',
position: 'Alpaca',
grade: 'Grass'
}, {
name: 'Zeb',
position: 'House Guy',
grade: 'Woods'
}, {
name: 'Joey',
position: 'News Guy',
grade: 'Woods'
}];
filterhandler.filterByTerm('woods', 'grade');
/*Output: [ { name: 'Zeb', position: 'House Guy', grade: 'Woods' },
{ name: 'Joey', position: 'News Guy', grade: 'Woods' } ]
*/
search(term)
Searches data and returns an object of data that matches the search
term
- The string term to search for
Usage
//Data:[{ name: 'Billy', age: 17, status: 'Good' }, { name: 'George', age: 16, status: 'Good' }, { name: 'Samantha', age: 16, status: 'Fantastic' }]
filterhandler.search('Billy');
//Output: [ { name: 'Billy', age: 17, status: 'Good' } ]
sortBy(key, reverse)
Sorts data based on key given also supports sorting by dates
key
- A key you wish to sort by (it will take the key value of the object to sort with)reverse
- A boolean if you want the sorted array to be reversed before being sent back
Usage
//Data: [{ name: 'Billy', position: 'Kool Kid', grade: 'G' }, { name: 'Albert', position: 'Alpaca', grade: 'Grass' }]
filterhandler.sortBy('name');
// Ouput: [{ name: 'Albert', position: 'Alpaca', grade: 'Grass' }, { name: 'Billy', position: 'Kool Kid', grade: 'G' }]
filterhandler.sortBy('name', true);
// Ouput: [{ name: 'Billy', position: 'Kool Kid', grade: 'G' }, { name: 'Albert', position: 'Alpaca', grade: 'Grass' }]
sortByDate(dateKey, reverse)
Sorts data based on date values given
dateKey
- The key with which you want to find dates underreverse
- A boolean if you want the sorted array to be reversed before being sent back
Usage
var mockDates = [{
name: 'Kelly',
startDate: '09/15/2016'
}, {
name: 'Ben',
startDate: '01/05/2016'
}, {
name: 'Frank',
startDate: '10/14/2012'
}];
filterhandler.sortByDate('startDate');
/*Output: [ { name: 'Frank', startDate: '10/14/2012' },
{ name: 'Ben', startDate: '01/05/2016' },
{ name: 'Kelly', startDate: '09/15/2016' } ]
*/
highlight(term)
Goes through the given data and tries to find and match a provided term to data, it will then place a property of highlight
within it which will be a boolean
of true
term
- The string term to search for to add highlight onto
Usage
//Data: [{ name: 'Billy', position: 'Kool Kid', grade: 'G' }, { name: 'Albert', position: 'Alpaca', grade: 'Grass' }]
filterhandler.highlight('Grass');
// Ouput: [{ name: 'Billy', position: 'Kool Kid', grade: 'G' }, { name: 'Albert', position: 'Alpaca', grade: 'Grass', highlight: true }]
offset()
Modifies the count of data to give you on offset amount. This is by default called at the end of every function to make sure you get the correct data count back. To disable offset simple set the pageSize
option either to null or 0
Usage
//opts.pageSize: 2
let data = [{ name: 'Kelly', startDate: '09/15/16' }, { name: 'Ben', startDate: 'Janurary 5, 2016' }, { name: 'Frank', startDate: '10/14/12' }]
filterhandler.offset(data);
//Output: [ { name: 'Frank', startDate: '10/14/12' }, { name: 'Ben', startDate: 'Janurary 5, 2016' } ]
clear
Clears out history and returns both the default data and the empty history object back to the caller as an object
Usage
//History: {name: [{name: 'Andrew'}, {name: 'Bob'}, {name: 'Carson'}]}
filterhandler.clear();
//Output: {data: defaultData, history: {}}