npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mozeyinedu/filter

v2.0.6

Published

@mozeyinedu/filter filters array of object with few lines of codes. It is scalable, light weight and simple to use with both simple and complex array of object of any size

Downloads

48

Readme

@mozeyinedu/filter

filter.js filters array of objects with few lines of codes. It is scalable, light weight and simple to use with both simple and complex array of objects of any size

Description

filter.js is a light weight, plain javascript package that can be used to filter large array of objects. Can be used for simple array of objects as well as complex ones

Installation

npm i @mozeyinedu/filter

Usage

...

import filter from "@mozeyinedu/filter";

...

...

const filter = require("@mozeyinedu/filter");

...

filter accept an object that has a total of 5 properties out of which only one is compulsary. It also returns the filtered array, you can assign it to a variable to them

  1. data:

    • data accepts an array of object or function that returns an array.
    • It is compulsary
  2. keys:

    • keys accepts an array or a function that returns an array of strings of properties you are searching for from the object
    • Only the properties specified can be searched for
    • If no keys specified, the function search through the paths (below) for the object path, if non found, all the keys will be up to flter through
    • It is not specific, if a "name" is specified as a key to filter, all other name in the object will be use to filter the object as well
    • Optioanl
  3. paths:

    • paths accepts an object of array(s) of the paths from the object you are searching or accepts a function that returns same thing
    • In this paths, you can customize exactly what you want to allow your client to filter
    • Optional.
  4. input:

    • accepts a string or a function that returns a string of the values you are searching from the array
  5. cb:

    • a callback that receives a parameter which holds the returned filtered array
    • not compulsary property but neccessary to view the filtered data,
    • the filter function itself returns the filtered array, you can assign it to a variable to get the filtered data also

Syntax

... // const filteredData = filter({})

const filterdData = filter({
    data: [],
    keys: ["", "", "", ...],
    paths: {
        path_1: ["", "", "", ...],
        path_2: ["", "", "", ...],
        path_3: ["", "", "", ...]
    },
    input: ""
})

console.log(filterdData)

OR

const filterdData2 = filter({
    data: ()=>[],
    keys: ()=>{
            return ["", "", "", ...]
        },
    paths: ()=>{
        return {
            path_1: ["", "", "", ...],
            path_2: ["", "", "", ...],
            path_3: ["", "", "", ...]
        },
    },
    input: ()=>{
        return ""
    }
})

console.log(filterdData2)

OR

// passing callback to get the filtered Data

filter({
    data: [],
    keys: ["", "", "", ...],
    paths: {
        path_1: ["", "", "", ...],
        path_2: ["", "", "", ...],
        path_3: ["", "", "", ...]
    },
    input: "",
    cb: (data)=>{
        console.log(data)
    }
})

...

Example codes

With Simple Array of Objects

...

import filter from "fine-filter"

const arrayObj = [
    {
        id: "1",
        name: "name1",
        product: "product1",
        email: "[email protected]"
    },
    {
        id: "2",
        name: "name2",
        product: "product2",
        email: "[email protected]"
    },
    {
        id: "3",
        name: "name3",
        product: "product3",
        email: "[email protected]"
    },
    {
        id: "3",
        name: "name3",
        product: "product3",
        email: "[email protected]"
    }
]

...

...

const newData = filter({
   data: arrayObj,
   keys: [ "name" ],
   input: "name2",

})

console.log(newData)

/*
    [
        {
            id: '2',
            name: 'name2',
            product: 'product2',
            email: '[email protected]'
        }
    ]
*/

// the object can be filtered using name or email. If input value is empty string or not specified at all, all the object will be returned

...

Using "paths"

...

filter({

    data: ()=>arrayObj,
    paths: {
        name: [ "name", ],
        email: [ "email" ]
    },
    input: "[email protected]",
    cb: data=> console.log(data)

})

// the object can be filtered using name or email

/*
    [
        {
            id: '1',
            name: 'name1',
            product: 'product1',
            email: '[email protected]'
        }
    ]
*/

...

With more complex array of Objects

paths is more usefull when dealing with more complex object

Global filtering

filtering the array with name using "keys"

When a particular object has same variable names as properties, for example the object above has "name" properties defined in several paths, (name, types[0].name and types[1].name). When a "keys" is used to define the key you want to filter the object with, and "name" is passed, all values assign to all the "name" properties can be used to filter the array.

...

const data = [
    {
        id:1,
        name: "phones",
        location: "US",
        types:[
            {
                id: 1,
                name: "iPhone",
                colors: ["pink", "red"]
            },
            {
                id: 2,
                name: "Infinix",
                colors: ["brown", "black"]
            }
        ]
    },

    {
        id: 2,
        name: "cars",
        location: "France",
        types:[
            {
                id: 1,
                name: "Renault",
                colors: ["green", "gray"]
            },
            {
                id: 2,
                name: "Bugatti",
                colors: ["white", "black"]
            }
        ]
    },

    {
        id:3,
        name: "phones",
        location: "Nigeria",
        types:[
            {
                id: 1,
                name: "iPhone",
                colors: ["yellow", "orange"]
            },
            {
                id: 2,
                name: "Infinix",
                colors: ["gold", "dark blue"]
            }
        ]
    },
]

...

...

filter({
    data: data,
    keys: ()=> [ "name" ],
    input: "iPhone",
    cb: data = > console.log(data)
})


/* 
    [
        {
            id: 1,
            name: 'phones',
            location: 'US',
            types: [ [Object], [Object] ]
        },
        {
            id: 3,
            name: 'phones',
            location: 'Nigeria',
            types: [ [Object], [Object] ]
        }
    ]

*/

...

...

filter({
    data,
    keys: ()=>[ "id", "location" ],
    input: "nigeria",
    cb: data=>console.log(data)
})

/*
    [
        {
            id: 3,
            name: 'phones',
            location: 'Nigeria',
            types: [ [Object], [Object] ]
        }
    ]
*/

...

...

filter({
    data,
    keys: [ "name" ],
    input: "cars",
    cb: data =>console.log(data)
})

/*
    [
        {
            id: 2,
            name: "cars",
            location: "France",
            types:[
                {
                    id: 1,
                    name: "Renault",
                    colors: ["green", "gray"]
                },
                {
                    id: 2,
                    name: "Bugatti",
                    colors: ["white", "black"]
                }
            ]
        }
    ]

*/

...

Local filtering

using paths to filter the object with a specific key -

If you do not want the behavior of global filter above, paths is here just for you. You can narrow your search to any properties you want by defining the object path explicitly. See examples below

...

filter({
    data: data,
    paths: {
        phoneName: ["types", "0", "name"]
    },
    input: "iPhone",
    cb: data=>console.log(data)
    }

})

/*
    [
        {
            id:1,
            name: "phones",
            location: "US",
            types:[
                {
                    id: 1,
                    name: "iPhone",
                    colors: ["pink", "red"]
                },
                {
                    id: 2,
                    name: "Infinix",
                    colors: ["brown", "black"]
                }
            ]
        }, 
        {
            id:3,
            name: "phones",
            location: "Nigeria",
            types:[
                {
                    id: 1,
                    name: "iPhone",
                    colors: ["yellow", "orange"]
                },
                {
                    id: 2,
                    name: "Infinix",
                    colors: ["gold", "dark blue"]
                }
            ]
        }
    ]
*/

// the paths is directed to the 2nd ordered name, types[0].name, i.e. name at index 0 of types


// if you try to search for car, it will return empty array since car is not one of the values of types[0].name

filter({
    data: data,
    paths: {
        phoneName: ["types", "0", "name"]
    },
    input: "car",
    cb: data=> console.log(data)
})

/*
    []

*/

...

Global and Local Filtering

Using keys and paths concurrently

For examples

  • Global filtring with name and email in "keys"
  • At the same time, Local filtering with colors in "paths"

...

filter({
    data: data,
    keys: [ "name", "email" ],
    paths: {
        colors_0: ["types", "0", "colors"],
        colors_1: ["types", "1", "colors"]
    },
    input: "red",
    cb: data=>console.log(data)
})

// you can defined as many paths as you want. The properties can be named anything, not necessarily colors_0 or colors_1

...

...

filter({
    data: data,
    keys: [ "name", "id" ],
    paths: {
        colors: ["types", "0", "colors"]
    },
    input: "1",
    cb: data=>console.log(data)
})

...

...

filter({
    data: data,
    keys: [ "id", "location" ],
    paths: {
        colors_0: ["types", "0", "colors"],
        colors_1: ["types", "1", "colors"],
        name: ["types", "1", "name"]
    },
    input: "dark blue",
    cb: data=>console.log(data)
})

...

Reach me;