Giter Club home page Giter Club logo

if-const's Introduction

if-const NPM version NPM monthly downloads NPM total downloads Linux Build Status Linux Build Status

Executes blocks of code depending on thruthness of the value, while also making the value accessible to the block

Install

Install with npm:

$ npm install --save if-const

or in any other way you like.

Usage

Imangine you have a faulty third-party function that can return a falsy value in some cases.
It's often needed to just get the "truthy" value from that function, quickly do something with it, and forget about it:

// returns either null or an object of some sort
import { nullOrObj } from './some-module';

if (nullOrObj()) {
  // how do we access the result?
}

// one might want to do this, but it's illegal in js
if (const obj = nullOrObj()) {
  obj
}

// And this is just tiring
// What if we want to enforce its immutability?
let obj;
if (obj = nullOrObj()) {}

That's where if-const comes in:

ifConst(nullOrObj(), obj => {
  // use the obj as you wish
});

It's that simple!

It works with any type of conditional that a normal if works with.
Allows to use the result of a conditional in a code block (similar to C# out var syntax).

import ifConst from 'if-const';

// a little function to simulate uncertanty of the result
// it returns either null or an object
const nullOrObj = () => Math.random() > 0.5 ? null : { foo: 'bar' };
const defaultObj = { foo: 'foo' };

const obj = ifConst(nullOrObj(), truthyObj => {
  console.log('obj is truthy', truthyObj);

  // returned value is then returned from the `ifConst` itself
  return truthyObj;
}, falsyObj => {
  console.log('obj is falsy', falsyObj);

  // returned value is then returned from the `ifConst` itself
  return defaultObj;
});

// logs either
// > obj is truthy { foo: 'bar' }
// or
// > obj is falsy { foo: 'foo' }

console.log(obj);
// > { foo: 'bar' }
// or
// > { foo: 'foo' }
// depending on which conditional block was executed

The ifConst function is also curried, and can be called with the first argument only:

const ifObj = ifConst(nullOrObj);

// Basically the same deal as earlier
const obj = ifObj(truthyObj => {
  console.log('obj is truthy', truthyObj);

  // returned value is then returned from the `ifObj` itself
  return truthyObj;
}, falsyObj => {
  console.log('obj is falsy', falsyObj);

  // returned value is then returned from the `ifObj` itself
  return defaultObj;
});

But if, for some reason, you have to set the blocks first,
you can use constIf:

import { constIf } from 'if-const';

const ifObj = constIf(truthyObj => {
  console.log('obj is truthy', truthyObj);

  // returned value is then returned from the resulting function
  return truthyObj;
}, falsyObj => {
  console.log('obj is falsy', falsyObj);

  // returned value is then returned from the resulting function
  return defaultObj;
});

// Basically the same deal as earlier
const obj = ifObj(nullOrObj);

This can be useful for piping and mapping different values in other functions.

Comparator

If, for some reason, you need to check for some different condition (not falsyness), you can use the .not and .compare methods:

// For example, we need to check if the value is 0 or null
const value = Math.random() > 0.5 ? null : 0;

// Since 0 is falsy, we need a custom comparator for this

// .not accepts a single value to `!==` against
// Note that the logic here is negated!
const ifNotNull = ifConst.not(null);

// .compare accepts a complete comparator function
const ifNotNull = ifConst.compare<null>(_ => _ !== null);

ifNotNull(value, v => {
  console.log('true', v, typeof v)
}, n => {
  console.log('false', n, typeof n)
});
// logs either
// > true 0 number
// or
// > false null object


// Or a shorter version:
ifConst.not(null)(value, v => {
  console.log('true', v, typeof v)
}, n => {
  console.log('false', n, typeof n)
});

Which reads almost like plain english!

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.