1.0.3 • Published 7 months ago

recollect-array-js v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Motivation

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

Documentation

VersionDocumentation
unreleasedhttps://github.com/thadeu/recollect-array-js/blob/main/README.md

Table of Contents

Compatibility

kindbranchjavascript
unreleasedmain>= 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

TypeSuffixValue
EqualeqAnywhere
NotEqualnot_eqAnywhere
ContainscontAnywhere
NotContainsnot_contAnywhere
IncludedinAnywhere
NotIncludednot_inAnywhere
LessThanltAnywhere
LessThanEquallteAnywhere
GreaterThangtAnywhere
GreaterThanEqualgteAnywhere
GreaterThanEqualgteAnywhere

Availables Predicates only when value is Object

💡 Below predicates works only when value is Object

TypeSuffixValue
ExistsexistsAnywhere
NotEqualnot_eqObject
NotContainsnot_contObject
NotIncludednot_inObject
NotMatchesnot_matchesObject

Usage

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.