filter-data
v0.6.1
Published
simple, fast data filter
Downloads
2,858
Maintainers
Readme
filter-data
Description
Simple but fast data filter.
Examples
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
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/filterdata.min.js"></script>
Installation
filter-data is available as an npm package.
npm install --save filter-data
Usage
From Browser
import from FilterData object. And others are the same with From npm
const { filterData, SearchType } = FilterData;
.
.
.
From npm
search single key only.
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: <!-- [ { firstName: 'Daniel', age: 14 }, { firstName: 'Dan', age: 18 }, ] -->
search multiple keys.
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: <!-- [ { firstName: 'Daniel', lastName: 'Johnson', age: 13 }, { firstName: 'Jack', lastName: 'Danny', age: 19 }, ] -->
caseSensitive.
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: <!-- [ { firstName: 'Jordan', age: 17 }, ] -->
offset & limit.
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: <!-- [ { firstName: 'Jordan', age: 17 }, . . . max 10 records ] -->
search nested object.
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: <!-- [ { firstName: 'Jordan', age: 17, father: { firstName: 'dan', age: 50 } }, ] -->
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
: equalSearchType.GT
: greater thanSearchType.GTE
: greater than or equalSearchType.LT
: less thanSearchType.LTE
: less than or equalSearchType.LK
: likeSearchType.NE
: not equalSearchType.NLK
: not like
License
This project is licensed under the terms of the MIT license.