complex-filter

Funcional way to filter array of object with complex association of AND, OR and NOT.

complex-filter


Filter out objects that don't match a series of filtering requirements.

TL;DR

Basic usage

const complexFilter = require('complex-filter')
const muffins = [ {}, {}, {}, ..., {}]

const queryStack = [
    {
        "type": "filter",
        "not": false,
        "searchFor": "chocolate",
        "searchOn": "flavor",
        "searchType": "contains"
    }
]

const myFilter = complexFilter(queryStack)
const chocolateMuffins = muffins.filter(myFilter)

How to build the queryStack.

complex-filteris a function built to use the .filter()method of arrays in javascript based on a series of filtering requirements passed as a json object.

A simple filter

[
    {
        "type": "filter",
        "not": false,
        "searchFor": "search string",
        "searchOn": "microscopist",
        "searchType": "contains"
    }
]

Notice that the filter objectis part of an array. That is because complex-filterlet you stack filters. Now let's go through the specifics.

type

The typefield can accept three values: filter, ANDand OR. We will talk extensively about it soon enough.

not

The notfield takes either falseor true. If not present it defaults to false.

searchFor

The searchForfield takes what should be expected to see in the object for filter in.

searchOn

The searchOnfield takes the fields of the object that will be filtered.

searchType

The searchTypefield is used to identify the type of filter it should be used. Currently, complex-filtersupports:

searchTypes for string
contains
exact
startsWith
endsWith
regex
searchTypes for numbers
exactValue
lessThan
greaterThan
between

Note: To use betweenwe need to pass two semicolonseparated values in searchFor. Also, when using between, NOTworks to invert the search intervals.

A complex filter.

Let's suppose that our objects to be filtered looks like this:

{
    "date": 1161302400,
    "NBCItaxID": 80880,
    "speciesName": "Hylemonella gracilis",
    "tiltSingleDual": 1,
    "defocus": -12,
    "dosage": 75,
    "tiltConstant": 1,
    "tiltMin": -63,
    "tiltMax": 60,
    "tiltStep": 0.9,
    "microscopist": "Gavin Murphy",
    "institution": "Caltech",
    "lab": "Jensen Lab",
    "sid": "gm2006-10-20-4"
}

and we would like to filter in all objects with NCBItaxIDas 80880ANDmicroscopistas Gavin Murphy.

To do that we need two filter requirements, let's see how to organize them:

[
    {
        "type": "filter",
        "searchFor": 80880,
        "searchOn": "NCBItaxID",
        "searchType": "exact",
    },
    {
        "type": "filter",
        "searchFor": "Gavin Murphy",
        "searchOn": "microscopist",
        "searchType": "exact"
    }
]

so... the default association in complex-filteris AND. As we add type:filterobjects to the stack, they will be considered in sequence as in ANDform.

Now, let's build a filter for objects with NCBItaxIDas 80880ANDmicroscopistas Gavin MurphyORMatt Swulius:

[
    {
        "type": "filter",
        "searchFor": 80880,
        "searchOn": "NCBItaxID",
        "searchType": "exact",
    },
    {
        "type": "OR",
        "args": [
            {
                "type": "filter",
                "searchFor": "Gavin Murphy",
                "searchOn": "microscopist",
                "searchType": "exact"
            },
            {    "type": "filter",
                "searchFor": "Matt Suwlius",
                "searchOn": "microscopist",
                "searchType": "exact",
            }
        ]
    }
]

As we can see complex-filteruses a different typeof filter object to listen to associations.

Now, let's build a filter for objects with NCBItaxIDas 80880ANDmicroscopistas Gavin MurphyORMatt Swulius, but from all the objects with Matt Swuliuslet's only filter in the ones with lab:"Jensen Lab":

[
    {
        "type": "filter",
        "searchFor": 80880,
        "searchOn": "NCBItaxID",
        "searchType": "exact",
    },
    {
        "type": "OR",
        "args": [
            {
                "type": "filter",
                "searchFor": "Gavin Murphy",
                "searchOn": "microscopist",
                "searchType": "exact"
            },
            {
                "type": "AND",
                "args": [
                    {    "type": "filter",
                        "searchFor": "Matt Suwlius",
                        "searchOn": "microscopist",
                        "searchType": "exact",
                    },
                    {    "type": "filter",
                        "searchFor": "Jensen Lab",
                        "searchOn": "lab",
                        "searchType": "exact",
                    },
                ]
            }
        ]
    }
]

In this case, we had to declareanother type of association (AND). So as a rule of thumb, everytime that there is a change in association type (ANDor OR), a new typeof association must be declared.

If we would like to make the labrequirement to all of them:

[
    {
        "type": "filter",
        "searchFor": 80880,
        "searchOn": "NCBItaxID",
        "searchType": "exact",
    },
    {    "type": "filter",
        "searchFor": "Jensen Lab",
        "searchOn": "lab",
        "searchType": "exact",
    },
    {
        "type": "OR",
        "args": [
            {
                "type": "filter",
                "searchFor": "Gavin Murphy",
                "searchOn": "microscopist",
                "searchType": "exact"
            },
            {    "type": "filter",
                "searchFor": "Matt Suwlius",
                "searchOn": "microscopist",
                "searchType": "exact",
            }
        ]
    }
]

HomePage

https://github.com/daviortega/complex-filter#readme

Repository

https+https://github.com/daviortega/complex-filter


上一篇:ipfs-api
下一篇:oip-js

相关推荐

  • 走进AngularJs之过滤器(filter)详解

    过滤器(filter)正如其名,作用就是接收一个输入,通过某个规则进行处理,然后返回处理后的结果。主要用在数据的格式化上,例如获取一个数组中的子集,对数组中的元素进行排序等。

    3 年前
  • 详解Angularjs在控制器(controller.js)中使用过滤器($filter)格式化日期/时间实例

    Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期、格式化数字精度、语言本地化、格式化货币等等。但这些过滤器一般都是在VIEW中使用的,比...

    3 年前
  • 详解AngularJS中$filter过滤器使用(自定义过滤器)

    1.内置过滤器 2.自定义过滤器     套用上面的格式定义两个简单的自定义过滤器一个带条件的,一个不带条件的。 (1)【不带条件】,功能:固定转换(有时候项目中会遇到角色代号,门店编码...

    3 年前
  • 记一个复杂组件(Filter)的从设计到开发

    此文前端框架使用 rax(https://github.com/alibaba/rax),全篇代码暂未开源(待开源) 原文链接地址:Nealyang/PersonalBlog(https://gi...

    1 年前
  • 移动端ios屏幕滚动时filter发生抖动闪烁bug

    做项目时候有些内容(一段文字)需要高斯模糊效果,就用了如下html和css: //html //css 安卓上一切正常,ios经测试下发现屏幕滚动时候出现抖动闪烁甚至空白的问题,百度谷歌...

    2 年前
  • 生动形象解释forEach、filter、map、some、every、find、reduce间的区别

    前言 从最开始学的for循环遍历方法,到后来层出不穷的各种遍历方法,其实最大的区别就是应用场景的不同。 我们最需要记住的就是,什么情况下用哪一种方法比较合适。 从挑土豆开始 图片描述(htt...

    1 年前
  • 每日 30 秒之 filterNonUnique

    简介 取出对象数组中唯一的数据集。 代码分析 代码使用了 来进行遍历数组并获取过滤,通过 和 来对数据项进行判断是否重复出现过。 使用场景 从后端或者数据库分别获取到参加不同...

    1 年前
  • 日拱一卒-如何注册全局filter

    实际上非常简单,只需要在Vue实例化之前注册即可。 当然,这种写法比较丑陋,还是要模块化地来做,也就是写成以下样子:filters.js: main.js: 之后创建了V...

    4 个月前
  • 数组方法find、filter、findIndex简介

    前言 ES6提供了很多新的API,数组对象的尤为实用,但是如果我们没有在相对应的开发环境下,很难对这些API有深入的了解,毕竟实践出真知。 find、filter、findIndex这三个方法都...

    1 年前
  • 提取wux-weapp的微信小程序筛选框:FilterBar

    本来是一个GitHub上wuxweapp组件的筛选框,但是整个项目过于系统化,想提取某个组件都是有点难度的,首先要看得懂某个组件的整个生命逻辑,所以自己也是摸索过来的,便简单的提取了wuxweapp组...

    2 年前

官方社区

扫码加入 JavaScript 社区