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

@flipbyte/when-condition

v0.6.0

Published

Check conditional statements and return true/false

Downloads

3,037

Readme

when-condition

Developed by Flipbyte

Build Status npm package Coverage Status license Codacy Badge

Check conditional statements and return true/false

Installation

npm i @flipbyte/when-condition

Usage

Define your conditionals using an array with logical rule ('and' or 'or') as the first element followed by an array of comparison conditions.

General representation of a logical rule is as follows:

['{your logical rule}', [{your comparison rule 1}], [{your comparison rule 2}], ...]

Comparison rule is an array with 3 elements:

  • The comparison rule.
  • The key of the object whose value needs to be compared with a condition.
  • The comparison value.

General representation of a comparison rule is as follows:

['{comparison rule}', '{key}', '{comparison value}']

Example

Consider an object as follows:

let data = {
    a: 1,
    b: 2,
    c: {
        d: 'string',
        e: [{
            f: 1,
            g: 2
        }]
    }
}

Single rule

To simply check one rule (say) data.a = 1

let rule = ['is', 'a', 1]

or

Check whether data.c.e[0].f = 1

let rule = ['is', 'c.e[0]`.f', 1]

Rules with multiple comparisons

Check whether both data.a = 1 and data.b = 2

let rule = ['and', ['is', 'a', 1], ['is', 'b', 2]]

Check whether either data.a = 1 or data.b = 2

let rule = ['or', ['is', 'a', 1], ['is', 'b', 2]]

Complex rules

Check whether (data.a = 1 and data.b = 2) or (data.b = 2 and data.c.d != 'string')

let rule = ['or', ['and', ['is', 'a', 1], ['is', 'b', 2]], ['and', ['is', 'b', 2], ['is', 'c.d', 'string']]]

Evaluating rules

import when from 'when-condition'

when(rule, data) // => returns true or false

Rules using callback

You can also pass a callback function to check the data. Which will be resolved to a promise. It can either be a plain function or a promise

when(function(data) {
    return data.a !== data.b
}, data).then((result) => {
    //...handle your result
})

Logical rules

There are 2 types of logical rules:

  • and - checks whether all the conditions evaluate to true.
  • or - checks whether atleast one condition evaluates to true.
  • not - returns the opposite of the evaluated comparison rule. This logical rule takes only one comparison rule.

Comparison rules

Following are the available comparison rules:

  • is - returns true if the object value strictly matches the comparison value.
  • isOfType - returns true if the object value matches the specified type (Ex: string, undefined, etc.).
  • anyOf - returns true if at least one of the comparison values matches the object key value. The comparison value needs to be an array.
  • allOf - returns true if all the object key value matches the comparison values. The comparison value needs to be an array.
  • gt - returns true if the object key value is greater than the comparison value
  • gte - returns true if the object key value is greater than or equal to the comparison value
  • lt - returns true if the object key value is lesser than the comparison value
  • lte - returns true if the object key value is lesser than or equal to the comparison value

License

The MIT License (MIT)