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

recollect-array-js

v1.0.7

Published

A simple and lightweight way to filter array using JavaScript

Downloads

11

Readme

Motivation

Because in sometimes, we need filter array passing conditions. This library simplify this work.

Documentation

Version | Documentation ---------- | ------------- unreleased | https://github.com/thadeu/recollect-array-js/blob/main/README.md

Table of Contents

Compatibility

| kind | branch | javascript | | -------------- | ------- | ------------------ | | unreleased | main | >= 14.x, <= 18.x |

Installation

Use Yarn

yarn add recollect-array-js

or use NPM

npm i --save recollect-array-js

and then, enjoy!

import RecollectArray from 'recollect-array-js'

Configuration

Without configuration, because we use only JavaScript. ❤️

Availables Predicates for all values

| Type | Suffix | Value | | ----------- | ----------- | ----------- | | Equal | eq | Anywhere | | NotEqual | not_eq | Anywhere | | Contains | cont | Anywhere | | NotContains | not_cont | Anywhere | | Included | in | Anywhere | | NotIncluded | not_in | Anywhere | | LessThan | lt | Anywhere | | LessThanEqual | lte | Anywhere | | GreaterThan | gt | Anywhere | | GreaterThanEqual | gte | Anywhere | | GreaterThanEqual | gte | Anywhere |

Availables Predicates only when value is Object

💡 Below predicates works only when value is Object

| Type | Suffix | Value | | ----------- | ----------- | ----------- | | Exists | exists | Anywhere | | NotEqual | not_eq | Object | | NotContains | not_cont | Object | | NotIncluded | not_in | Object | | NotMatches | not_matches | Object |

Usage

data = [
  {
    id: 1,
    name: 'Test #1',
    email: '[email protected]',
    schedule: { all_day: true },
    numbers: [1, 2],
    active: true,
    count: 9
  },
  {
    id: 2,
    name: 'Test #2',
    email: '[email protected]',
    schedule: { all_day: false },
    numbers: [3, 4],
    active: true,
    count: 10
  },
  {
    id: 3,
    name: 'Test #3',
    email: '[email protected]',
    schedule: { all_day: false },
    numbers: [5, 6],
    active: false,
    count: 99,
    members: null
  }
]

You can use one or multiples predicates in your filter. We see some use cases.

Flexible Use Case (Hash)

Equal

filters = {
  active: { eq: true }
}

collection = RecollectArray.filter(data, filters)

NotEqual

filters = {
  active: {
    not_eq: true
  }
}

collection = RecollectArray.filter(data, filters)

Exists

Filter only if value be different of null or undefined

filters = {
  members: {
    exists: true
  }
}

collection = RecollectArray.filter(data, filters)

NotExists

filters = {
  members: {
    exists: false
  }
}

collection = RecollectArray.filter(data, filters)

Nested Hash Paths

filters = {
  'schedule.all_day': {
    eq: true
  }
}

collection = RecollectArray.filter(data, filters)

Nested Array Paths

Note the .0 🎉

filters = {
  'numbers.0': {
    eq: '3'
  }
}

collection = RecollectArray.filter(data, filters)
filters = {
  numbers: {
    in: '3' // or in: ['3']
  }
}

collection = RecollectArray.filter(data, filters)

Using default Equal predicate.

RecollectArray.filter(data, { numbers: 3 })

RecollectArray.filter(data, { active: true })

RecollectArray.filter(data, { id: 3 })

If array, you can navigate into self, using property.NUMBER.property

data = [
  {
    schedules: [
      {
        opened: true,
        all_day: true
      },
      {
        opened: false,
        all_day: true
      }
    ]
  },
  {
    schedules: [
      {
        opened: false,
        all_day: true
      },
      {
        opened: false,
        all_day: true
      }
    ]
  }
]

filters = {
  'schedules.0.opened': {
    eq: true
  }
}

collection = RecollectArray.filter(data, filters)

# [{ schedules: [{ opened: true, all_day: true }, { opened: false, all_day: true }] }]

Amazing, you can pass a Function value as value, like this.

filters = {
  'schedules.0.opened': { eq: () => true }
}

collection = Recollect::Array.filter(data, filters)

Combine conditions

Yes, you can combine one or multiple predicates to filter you array.

filters = {
  active: { eq: true },
  numbers: {
    in: [5],
    not_in: '10'
  },
  email: {
    cont: 'email1',
    not_cont: '@gmail'
  },
  'schedule.all_day': {
    in: [true, false]
  }
}

collection = RecollectArray.filter(data, filters)

⬆️  Back to Top

Development

After checking out the repo, install dependencies. Then, run yarn test to run the tests.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/thadeu/recollect-array-js. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.