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

whatif-js

v1.0.1

Published

WhatIf is an extremely simple and easy to use JavaScript extension for expressing a single if-else statement anywhere, anytime.

Downloads

1

Readme

WhatIfJS

Installation:

Install using yarn or npm

npm i --save whatif-js
yarn add whatif-js

Then in the entryPoint of your app/modulejust require``whatIf:

require("whatif-js");

And that's it, nothing more needed for setup. Everything is handled by the module itself.

Usage

WhatIf

WhatIf is an expression for invoking an action when the given value is not-null, not-undefined and true.

The whatIffunction has three parameters:

  1. given - the value

  2. action - the action to perform if the given value is not-null, not-undefined and true.

  3. actionWhatIfNot - (optional) - the action to perform in case the given value returns false.

Examples:

The most basic (and useless) example would be:

let obj = { name: "Awesome App", type: "App" };
obj = obj.whatIf(obj.type === "App", function() {
  this["description"] = "I am an awesome app.";
});
//output =
//{
//  name: 'Awesome App',
//  type: 'App',
//  description: 'I am an awesome app.'
//}

Now let's get a little bit more complex. Usage in a builder like an Array or Stringalso works.

let array = [0, 1, 2, 3, 4, 5];
array = array
  .whatIf(array.length === 6, ctx => {
    return ctx.concat(6, 7, 8, 9);
  })
  .slice(2, 4)
  .join(",");
//output = 2,3,4,5,6,7,8,9

WhatIf can also be used directly with booleanslike so:

function workingWithBoolean(stopWork) {
  stopWork.whatIf(function() {
    console.log("Stop all the work!");
  });
}

Now coming to the the more complex example. Using WhatIf with functions.

There are two ways of using WhatIf with functions, the first is more simple:

function calculateSimple(doMultiplication, doDivision) {
  //three methods for three mathematical operations
  function add(...params) {
    return params.reduce((p, c) => p + c);
  }
  function multiply(...params) {
    return params.reduce((p, c) => p * c);
  }
  function divide(...params) {
    return params.reduce((p, c) => p / c);
  }

  //the values to work with
  let paramaters = [21, 23, 41, 51, 5, 1];

  //the result
  let result = add //perform add if both parameters are false
    .whatIf(doMultiplication, multiply) //this will only be executed if doDivision=false & doMultiplication=true
    .whatIf(doDivision, divide) //if doDivision=true, it will always take precedence no matter the value of doMultiplication
    .call(this, ...paramaters); //the final function call
  return result;
}

We can use the above function calculateSimplein the following three ways:

calculateSimple(false, false); //142
calculateSimple(false, true); //0.0000873307965816231
calculateSimple(true, false); //5049765
calculateSimple(true, true); //0.0000873307965816231

More complex example includes performing DMAS (Division, Multiplication, Addition, Subtraction) on a stringusing only the power ofWhatIf. See the full example here

WhatIfNotNull

whatIfNotNull is an expression for invoking an action when the target object is not null.

let person = getPersonByUsername("John Doe");
person.whatIfNotNull(
    function() {
      console.log("person is not null.");
    },
    function() {
      console.log("person is null");
    }
);

WhatIfNotUndefined

whatIfNotUndefined is exactly like whatIfNotNullbut it checks whether the object is undefined or not.

WhatIf With Different Return Type

In cases where you want a different return type then the input you can use whatIf like this:

let stringLength = "i am a string".whatIf(getLength, function() {
  return this.length;
});

WhatIfNotNullOrEmpty

An expression for invoking an action when the given array is not null and not empty. If the array is null or empty, actionWhatIfNot will be invoked instead of the action.

anyArray.whatIfNotNullOrEmpty(
  function() {
    console.log("array is not null or empty.");
  },
  function() {
    console.log("array is null or empty");
  }
);

Using Arrow Functions

All functions of whatIfare usable with arrow functions like so:

anyArray.whatIfNotNullOrEmpty(
  ctx => console.log("array is not null or empty."),
  ctx => console.log("array is null or empty")
);

Find this library useful? :heart:

Support it by joining stargazers for this repository. :star:or buy me a cup of coffee And follow me for my next creations! 🤩

License

Copyright 2019 thecodrr (Abdullah Atta)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.