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

numericconditionals

v1.0.1

Published

A small utility which can conditionally return data based on predefined conditions

Downloads

2

Readme

numericconditionals

A small utility which can conditionally return data based on predefined conditions. This can be very useful to for example return different values based on a numeric value.

There are many possible uses - I for example use it for a Slack bot, which prefixes the current temperature with a fitting attribute (e. g. nice 20°C, cold -5°C etc.).

There are three types of objects, dependent on the each other:

  • A Condition tests a specific case (e. g. is the value bigger than 10? Is it smaller than or equal to 9?)
  • A Case tests multiple conditions and also holds data. You could for example say that a value should be bigger than 10 and less than or equal to 20, and store an array for when this case is true
  • A Conditional compares a value against multiple cases. You can either return the data of the first case that fits or return the data of all cases that fit.

Installation

npm install numericconditionals
yarn add numericconditionals

Usage

const {
  Condition,
  Case,
  Conditional
} = require('numericconditionals');

Conditions

Creation

Build up your conditions:

const conditions1 = [
  new Condition('>', 0),
  new Condition('<=', 11)
];

const conditions2 = [
  new Condition('>', 10),
  new Condition('<=', 20)
];

The first one would match any value bigger than 0 (not 0 itself!), the second one would match any value less than or equal to 10.
The numbers are run through Number(), so you can use everything that fits in there.

Usage

Use conditions by calling isTrue() on a Condition:

const c1 = new Condition('>', 10);
c1.isTrue(12); // returns true
c1.isTrue(8); // returns false

Cases

Creation

Build up your cases from your conditions:

const cases = [
  new Case(
    conditions1,
    ['freezing', 'cold', 'terrible']
  ),
  new Case(
    conditions2,
    ['nice', 'warm', 'refreshing']
  )
];

Usage

You can test a Case by calling areConditionsTrue:

const c2 = new Case(
  [
    new Condition('>', 10),
    new Condition('<=', 20)
  ],
  ['data', 'which', 'will', 'be', 'returned']
);

c2.areConditionsTrue(15); // returns true
c2.areConditionsTrue(9); // returns false
c2.getValue(); // returns ['data', 'which', 'will', 'be', 'returned']

Conditionals

Creation

Build up your conditional:

const conditional = new Conditional(cases);

Usage

Now you can get values from your conditional. There are two possibilities:

conditional.getFittingCases(10.5); // returns ['freezing', 'cold', 'terrible']
conditional.getFittingCases(10.5, true); // returns [['freezing', 'cold', 'terrible'], ['nice', 'warm', 'refreshing']]
conditional.getFittingCases(15); //returns ['nice', 'warm', 'refreshing']

Now you could use these however you'd like - for example get a random value for your chat bot!