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

check-is-truthy

v0.0.6

Published

Utilities and types to help with simple Boolean checks

Downloads

9

Readme

check-is-truthy

Utilities and types to help with simple Boolean checks

This is intended to be used with TypeScript, but it should work with JavaScript as well.

This package is side-effect free and can be fully tree-shaken.

Installation

npm install check-is-truthy

or

yarn add check-is-truthy

Why is this useful?

By and large, the primary useful thing about this package is the isTruthy function which can be used within standard JavaScript functions like Array.prototype.filter or within other utilities which take a predicate function with a type guard, such as Lodash's _.find.

Though the same behavior can be achieved at runtime by calling the Boolean constructor as a function, TypeScript makes no special handling of this with regards to type guards as mentioned above.

isTruthy

isTruthy is a function that takes a value and returns a boolean indicating whether it is "truthy" within JavaScript.

It is particularly useful for filtering collections, and can be used in most cases where predicates are used, such as Array.prototype.filter.

import { isTruthy } from "check-is-truthy";

doSomethingComplex(["foo", "bar", doSomething && expensive()].filter(isTruthy));

isFalsy

isFalsy is a function that takes a value and returns a boolean indicating whether it is "falsy" within JavaScript.

toggle

toggle is a function that takes a value and returns its logical opposite. This has the effect of cycling between true and false.

function MyComponent() {
  const [isOpen, setIsOpen] = useState(false);
  const toggleIsOpen = useCallback(() => setIsOpen(toggle), []);
  return (
    <Modal isOpen={isOpen}>
      <button onClick={toggleIsOpen} />
    </Modal>
  );
}

type FalsyValue

FalsyValue is a type that represents all values that are falsy in JavaScript.

One can use TypeScript's built-in Extract or Exclude types to extract values from a type that are falsy (or truthy).

import type { FalsyValue } from "check-is-truthy";

function doSomething<T>(
  value: T,
  whenTruthy: (value: Exclude<T, FalsyValue>) => void,
  whenFalsy: (value: Extract<T, FalsyValue>) => void
) {
  if (value) {
    // in this branch, we know `value` is truthy
    whenTruthy(value);
  } else {
    // in this branch, we know `value` is falsy
    whenFalsy(value);
  }
}

NaN

The reserved value NaN. This is equivalent to the global NaN value.

This has a special type of NaN, which is an opaque value that can represent NaN.

It is not fully safe to use, as NaN values can be easily created through other means that are not typed as NaN.

isNaN

Equivalent to Number.isNaN.

This is provided as a convenience as a check for the NaN value as the NaN type.