# filterhandler

> Handles filtering data sets and getting you filtered information

Latest version **1.1.0** (published 2017-05-15) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install filterhandler
pnpm add filterhandler
yarn add filterhandler
bun add filterhandler
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2017-05-15 |
| First published | 2016-06-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dustin Hershman |
| Maintainers | brandonchartier, killparadise |
| Keywords | filters, sorting, easy filters, customizable |

## Links

- npm: https://www.npmjs.com/package/filterhandler
- Repository: https://github.com/dhershman1/FilterHandler
- Homepage: https://github.com/dhershman1/FilterHandler#readme
- Issues: https://github.com/dhershman1/FilterHandler/issues
- npm.io page: https://npm.io/package/filterhandler

## Recent versions

- 1.1.0 (latest) — 2017-05-15
- 1.0.1 — 2016-11-12
- 1.0.0 — 2016-11-12
- 0.1.0 — 2016-06-06

## README

[![Build Status](https://travis-ci.org/dhershman1/FilterHandler.svg?branch=master)](https://travis-ci.org/dhershman1/FilterHandler)

# 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
```js
const filterhandler = require('filterhandler')(options)
```

### Options
options include:

- `trackHistory` - keep track of filter and data search history `default: false`
- `searchableData` - the data filterhandler will be managing `send as an array`
- `pageSize` - how many items should filterhandler limit the return to? `default: 10` set to `null` or `0` to disable

### Usage
```js
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
```js
filterhandler.getHistory();
```

#### filterByTerm(term, key)
Filter the array by the given term

- `term` - The string term to search for

##### Usage
```js
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
```js
//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
```js
//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 under
- `reverse` - A boolean if you want the sorted array to be reversed before being sent back

##### Usage
```js
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
```js
//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
```js
//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
```js
//History: {name: [{name: 'Andrew'}, {name: 'Bob'}, {name: 'Carson'}]}
filterhandler.clear();
//Output: {data: defaultData, history: {}}
```

---
_Source: https://npm.io/package/filterhandler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
