1.0.2 • Published 3 years ago

@amn31/filter-multiple-conditions v1.0.2

Weekly downloads
6
License
MIT
Repository
-
Last release
3 years ago

Like a SQL query, filter a array of objects

To simplify searching in a array of objects like a SQL query, you can use amn31@filter-multiple-conditions. If datas is stored in database, you can use package @amn31/convert2sequelize to complete your needs.

Query example

    // Example of request 
    let whereJSON = [ 
            [
                "Imei",
                "not like",
                "'%33680090%'"
            ],
            "and",
            [
                [
                    "EnrollmentStatus",
                    "=",
                    "Unnrolled"
                ],
                "or",
                [
                    "EnrollmentStatus",
                    "=",
                    "Enrolled"
                ],
            ]
        ]

Installation

$ npm install @amn31/filter-multiple-conditions

Launch queries

How use amn31@filter-multiple-conditions ?

  
   import { FilterConditions, MaFilter } from "@amn31/filter-multiple-conditions"

    // Create query
    let conditions :FilterConditions = [
        [
            "id",
            ">",
            // Number
            "13"
        ],
        "and",
        [
            [
                "isLeasing",
                "=",
                "1" // '1' => true, 
            ],
            "or",
            [
                [
                    "isLeasing",
                    "isnull",
                    ""
                ],
                "and",
                [
                    "Platform",
                    "like",
                    "%Apple%"
                ],
            ]
        ]
    ];

    // Data to filter
    let data = [
        ...
        {
            "id": 10,
            "Imei": "01326007739292",
            "Platform": "Apple",
            "Model": "iPad 3rd Gen LTE (GSM) (64 GB)",
            "OperatingSystem": "9.3.5",
            "EnrollmentStatus": "Enrolled",
            "lastseen": "2021-02-08",
            "isLeasing": true
        },
        ...
    ]

    // Result of filtering
    let dataFiltered = MaFilter.FilterByConditions(conditions, data);

    console.log(dataFiltered);

    // FilterByConditions can sort result if you add options 'field'
    let dataFilteredAndSorted = MaFilter.FilterByConditions(
        conditions, 
        data, 
        {
            field: 'id'
        });
    
    // FilterByConditions can   reverse the sorted result if you add options 'reverse'}
    let dataFilteredAndSorted = MaFilter.FilterByConditions(
        conditions, 
        data, 
        {
            field: 'id',
            reverse: true
        });
    

Operators

    and
    or
    =
    !=
    >
    <
    >=
    <=
    like
    not like
    regex
    notRegexp
    startswith
    endswith
    contains
    isnull
    isnotnull

License

MIT

Angular

Sequelize

@amn31/convert2sequelize

@amn31/filter-multiple-conditions