GithubHelp home page GithubHelp logo

manusquall / array-querier Goto Github PK

View Code? Open in Web Editor NEW

This project forked from orbitturner/array-querier

0.0 1.0 0.0 141 KB

A TS/JS NPM Package to Filter an array of objects with multiple match-criteria.

Home Page: https://www.npmjs.com/package/array-querier

License: MIT License

TypeScript 100.00%

array-querier's Introduction

๐ŸŸข Array Querier ๐Ÿ”Ž

Compatible Status Code Size Status Commit Status Issues Status npm version license

Array-Querier is a TS/JS NPM Package to Filter an Array of objects with multiple match-criteria.

array-querier COVER

INSTALLATION



๐Ÿ“š Table Of Contents ๐Ÿ“‘


๐Ÿ’จ What is this Library for? ๐Ÿค”

array-querier is a small library that is useful for filtering a One Level or Multi Level Depth array of objects with multiple match-criteria. The exposed methods receives an array as the first argument, and a plain object describing the fields to filter as the last argument.

Note: This library can only be used with typescript or js but you already know that ๐Ÿคฆ๐Ÿฟโ€โ™‚๏ธ.

โœจ Key Features ๐ŸŽฏ

  • Use it without Instanciation because all the methods are Static.
  • Multi Level Depth Filtering with complex filtering condition.
  • Optimized for Great Performance even with Big Fat @/@ Arrays of Objects.
  • โœ… TOO EASY TO USE !! ๐Ÿฅณ๐Ÿฅณ

๐Ÿ“ฅ Installation ๐Ÿ”ฐ

# installation with npm
npm install array-querier

# or you may prefer
npm i --save array-querier

# installation with yarn
yarn add array-querier

This HELPER relies on NOTHING SO YOU DON'T NEED ADDITIONNAL PACKAGES.


๐Ÿค” One-Level vs Multi-Level Depth JSON ? ๐Ÿค”

A JSON depth level is just an nesting of another object within a current JSON object. For example : If you have a User object as follows ->

// Nested Object Planet in User
User = {
    "name": "Orbit",
    "age": 21,
    "planet": {
        "id": 4,
        "codename" : "Shadow-Coders",
        "galaxyName" : "Turner"
    }   
}

Then User is an Array of two level Depth.

Of course if you don't have any nested object then you got an One level Depth.


โš™ Usage: One-Level Depth Arrays (Simple Arrays) ๐ŸŽš

โžค Querier.filterSimpleArray(yourData, filterObject); ๐ŸŸข

If you are only interested in filtering an simple array of JSON objects directly:

import {Querier} from 'array-querier/lib/orbiter';

/**
 * Your array of JSON Objects.
 * This can be pulled directly from your Backend Rest API.
 */
const products = [
  { name: 'A', color: 'Blue', size: 50 },
  { name: 'B', color: 'Blue', size: 60 },
  { name: 'C', color: 'Black', size: 70 },
  { name: 'D', color: 'Green', size: 50 },
];

// โš  You need a Filter Object to make your condition โš 
const filters = {
  color: ['BLUE', 'black'],
  size: [70, 50],
};

/**
 * Calling Simple Array Filterer In Query.
 * Filters an array of objects (one level-depth) with multiple criteria.
 * The function returns an array of the same type as the input array.
 */
const MyFilteredResult = Querier.filterSimpleArray(products, filters);

console.table(MyFilteredResult);
/* ๐ŸŸข The Result Will Be :๐ŸŸข
      { name: 'A', color: 'Blue', size: 50 },
      { name: 'C', color: 'Black', size: 70 },
 */

โš  Note: โš  The filterSimpleArray method IS NOT Case-Sensitive ๐Ÿšจ.


โš™ Usage: Multi-Level Depth Arrays (Complex Arrays) ๐ŸŽ›

โžค Querier.filterComplexArray(yourData, filterObject); ๐ŸŸข

In everyday life, as developers, our JSON arrays are often very complex because of foreign keys and / or the nesting of objects that allow us to better describe our entities.

In this case this Method is the most appropriate because it allows to apply very advanced filters to our Array regardless of the depth level.

import {Querier} from 'array-querier/lib/orbiter';

/**
 * Your Complex array of JSON Objects.
 * This can be pulled directly from your Backend Rest API.
 */
const products = [
  { name: 'Orbit', color: 'Blue', size: 50, locations: ['USA', 'Europe'], details: { length: 20, width: 70 } },
  { name: 'Galsen', color: 'Blue', size: 60, locations: [], details: { length: 20, width: 70 } },
  { name: 'DaoudaBa', color: 'Black', size: 70, locations: ['Japan'], details: { length: 20, width: 71 } },
  { name: 'Mmnl', color: 'Green', size: 50, locations: ['USA'], details: { length: 20, width: 71 } },
];

// โš  Filter Object with complex conditions โš 
const filters = {
  size: (size: number) => size === 50 || size === 70,
  color: (color: string) => ['blue', 'black'].includes(color.toLowerCase()),
  locations: (locations: any[]) => locations.find(x => ['JAPAN', 'USA'].includes(x.toUpperCase())),
  details: (details: { length: number; width: number; }) => details.length < 30 && details.width >= 70,
};

/**
 * Calling Simple Array Filterer In Query.
 * Filters an array of objects (one level-depth) with multiple criteria.
 * The function returns an array of the same type as the input array.
 */
const MyFilteredResult = Querier.filterComplexArray(products, filters);

console.table(MyFilteredResult);
/* ๐ŸŸข The Result Will Be :๐ŸŸข
      { name: 'A', color: 'Blue', size: 50, locations: ['USA', 'Europe'], details: { length: 20, width: 70 } },
      { name: 'C', color: 'Black', size: 70, locations: ['Japan'], details: { length: 20, width: 71 } },
 */

The Filter can be more complex and advance like the following use case case :

...

// โš  Filter Object with VERY Complex Conditions ๐Ÿƒ๐Ÿพโ€โ™‚๏ธ๐Ÿƒ๐Ÿพโ€โ™‚๏ธโš 
const filters = {
  size: (size: number) => size === 50 || size === 70,
  color: (color: string) => ['blue', 'black'].includes(color.toLowerCase()),
  details: (details: { length: number; width: number; }) => details.length < 30 && details.width >= 70,
  locations: (locations: string | string[]) => {
    if (locations.includes('USA')) { return true; } // case sensitive
    if (locations.includes('Japan')) { return true; } // case sensitive

    const url = window.location.pathname.toLowerCase();
    if (url.includes('/en-us/')) { return true; } // not case sensitive
    if (url.includes('/es/')) { return true; } // not case sensitive
    return false;
  }
};

const MyFilteredResult = Querier.filterComplexArray(products, filters);

โœจ๐Ÿค— AS I SAID BEFORE : EASYYY ๐Ÿค—โœจ

Configuration Options

Coming Soon !


Contributing โค

๐Ÿ‘‹๐Ÿพ Pull requests are welcome!


Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.


GREETINGS

โคโค Coming Soon ! โคโค


Author

Orbit Turner


License

This project is licensed under the MIT license. See the LICENSE file for more info.


โค MADE WITH LOVE โค

Image of OT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.