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

if-const

v1.2.0

Published

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

Downloads

12

Readme

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

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

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