Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

filter-data

Package Overview
Dependencies
Maintainers
0
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

filter-data

simple, fast data filter

  • 0.6.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

filter-data

npm version dependencies Status CircleCI codecov

Description

Simple but fast data filter.

Examples

  1. Example In Browser
  2. Example In React

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 key10.947ms4.244ms1.827ms
no match, 1 key0.523ms2.385ms2.958ms
match partial, 1 key0.232ms0.318ms2.475ms
match all, 2 keys1.472ms0.465ms2.209ms
no match, 2 keys0.188ms0.513ms2.522ms
match partial, 2 keys0.191ms0.318ms2.475ms
match all, 1 key, slice(0,10)0.192ms0.206ms0.388ms
no match, 1 key, slice(0,10)0.101ms0.317ms0.079ms
match partial, 1 key, slice(0,10)0.107ms0.188ms2.807ms
input empty0.114ms0.095ms0.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 key21.439ms49.336ms16.884ms
no match, 1 key18.239ms33.312ms6.382ms
match partial, 1 key18.754ms22.56ms3.805ms
match all, 2 keys22.815ms22.524ms10.416ms
no match, 2 keys18.096ms33.232ms3.744ms
match partial, 2 keys16.821ms27.052ms3.094ms
match all, 1 key, slice(0,10)10.614ms12.692ms0.106ms
no match, 1 key, slice(0,10)9.808ms19.709ms0.111ms
match partial, 1 key, slice(0,10)9.593ms16.094ms0.393ms
input empty10.571ms6.985ms0.03ms

Install From Browser

<script src="https://cdn.jsdelivr.net/npm/filter-data@0.2.0/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

  1. 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 },
    ] -->
    
  2. 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 },
    ] -->
    
  3. 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 },
    ] -->
    
  4. 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
    ] -->
    
  5. 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.ParameterrequiredDefaultDescription
1dataarray of object for filtering
2searchConditionsarray of searchCondition; { key: 'search column', value: 'search value', type: 'search type' }
3options{ 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.

Keywords

FAQs

Package last updated on 01 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc