https://github.com/thundermiracle/filter-data
filter data by transducer
https://github.com/thundermiracle/filter-data
filter-data partial-search
Last synced: 4 months ago
JSON representation
filter data by transducer
- Host: GitHub
- URL: https://github.com/thundermiracle/filter-data
- Owner: thundermiracle
- License: mit
- Created: 2019-12-07T09:27:51.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-03-27T06:51:50.000Z (11 months ago)
- Last Synced: 2025-03-29T15:05:20.885Z (11 months ago)
- Topics: filter-data, partial-search
- Language: TypeScript
- Size: 1.53 MB
- Stars: 8
- Watchers: 1
- Forks: 1
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# filter-data
[](https://badge.fury.io/js/filter-data)
[](https://david-dm.org/thundermiracle/filter-data)
[](https://circleci.com/gh/thundermiracle/filter-data)
[](https://codecov.io/gh/thundermiracle/filter-data)
## Description
Simple but fast data filter.
## Examples
1. [Example In Browser](https://codesandbox.io/s/filter-data-browser-stxj8?file=/index.html)
1. [Example In React](https://codesandbox.io/s/filter-data-react-iw534?file=/src/getData.js)
## Benchmark
### _100 Records_(ms)
_*The results are little different in partial search._
| | match-sorter (6.3.1) | fuse.js (6.6.2) | filter-data (0.2.0) |
| :--- | :--: | :-: | :--: |
| match all, 1 key | 10.947ms | 4.244ms | 1.827ms |
| no match, 1 key | 0.523ms | 2.385ms | 2.958ms |
| match partial, 1 key | 0.232ms | 0.318ms | 2.475ms |
| match all, 2 keys | 1.472ms | 0.465ms | 2.209ms |
| no match, 2 keys | 0.188ms | 0.513ms | 2.522ms |
| match partial, 2 keys | 0.191ms | 0.318ms | 2.475ms |
| match all, 1 key, slice(0,10) | 0.192ms | 0.206ms | 0.388ms |
| no match, 1 key, slice(0,10) | 0.101ms | 0.317ms | 0.079ms |
| match partial, 1 key, slice(0,10) | 0.107ms | 0.188ms | 2.807ms |
| input empty | 0.114ms | 0.095ms | 0.033ms |
### _10000 Records_(ms)
_*The results are little different in partial search._
| | match-sorter (4.0.2) | fuse.js (3.4.6) | filter-data (0.2.0) |
| :--- | :--: | :-: | :--: |
| match all, 1 key | 21.439ms | 49.336ms | 16.884ms |
| no match, 1 key | 18.239ms | 33.312ms | 6.382ms |
| match partial, 1 key | 18.754ms | 22.56ms | 3.805ms |
| match all, 2 keys | 22.815ms | 22.524ms | 10.416ms |
| no match, 2 keys | 18.096ms | 33.232ms | 3.744ms |
| match partial, 2 keys | 16.821ms | 27.052ms | 3.094ms |
| match all, 1 key, slice(0,10) | 10.614ms | 12.692ms | 0.106ms |
| no match, 1 key, slice(0,10) | 9.808ms | 19.709ms | 0.111ms |
| match partial, 1 key, slice(0,10) | 9.593ms | 16.094ms | 0.393ms |
| input empty | 10.571ms | 6.985ms | 0.03ms |
## Install From Browser
```html
```
## Installation
filter-data is available as an [npm package](https://www.npmjs.org/package/filter-data).
```sh
npm install --save filter-data
```
## Usage
### From Browser
import from FilterData object. And others are the same with [From npm](#from-npm)
```js
const { filterData, SearchType } = FilterData;
.
.
.
```
### From npm
1. search single key only.
```js
import { filterData, SearchType } from 'filter-data';
// search firstName contains 'dan' and age < 20
const searchConditions = [
{
key: 'firstName',
value: 'dan',
type: SearchType.LK,
},
{
key: 'age',
value: 20,
type: SearchType.LT,
},
];
const result = filterData(data, searchConditions);
// output:
```
1. search multiple keys.
```js
import { filterData, SearchType } from 'filter-data';
// search firstName&lastName contains 'dan' and age < 20
const searchConditions = [
{
key: ['firstName', 'lastName'],
value: 'dan',
type: SearchType.LK,
},
{
key: 'age',
value: 20,
type: SearchType.LT,
},
];
const result = filterData(data, searchConditions);
// output:
```
1. caseSensitive.
```js
import { filterData, SearchType } from 'filter-data';
// search firstName contains 'dan'
const searchConditions = [
{
key: 'firstName',
value: 'dan',
type: SearchType.LK,
},
];
const result = filterData(data, searchConditions, { caseSensitive: true });
// output:
```
1. offset & limit.
```js
import { filterData, SearchType } from 'filter-data';
// search firstName contains 'dan'
const searchConditions = [
{
key: 'firstName',
value: 'dan',
type: SearchType.LK,
},
];
const result = filterData(data, searchConditions, { caseSensitive: true, offset: 10, limit: 10 });
// output:
```
1. search nested object.
```js
import { filterData, SearchType } from 'filter-data';
// search firstName in father's sub object equals to 'dan'
const searchConditions = [
{
key: 'father.firstName', // or key: [['father', 'firstName']]
value: 'dan',
type: SearchType.EQ,
},
];
const result = filterData(data, searchConditions);
// output:
```
## Instructions
| No. | Parameter | required | Default | Description |
|:---|:-------------:|:---------:|:--------------|:-----------|
| 1 | data | 〇 | | array of object for filtering |
| 2 | searchConditions | 〇 | | array of searchCondition; ```{ key: 'search column', value: 'search value', type: 'search type' }``` |
| 3 | options | | ```{ caseSensitive: false, includeNull: false, offset: undefined, limit: undefined }``` | includeNull: include data even `key` is not exist or `value` is null |
### SearchType
* ```SearchType.EQ```: equal
* ```SearchType.GT```: greater than
* ```SearchType.GTE```: greater than or equal
* ```SearchType.LT```: less than
* ```SearchType.LTE```: less than or equal
* ```SearchType.LK```: like
* ```SearchType.NE```: not equal
* ```SearchType.NLK```: not like
## License
This project is licensed under the terms of the
[MIT license](/LICENSE).