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 🙏

© 2025 – Pkg Stats / Ryan Hefner

typescript-nullsafe

v1.0.2

Published

Nullable functions

Downloads

72

Readme

Install

$ npm install typescript-nullsafe --save

or

$ yarn add typescript-nullsafe

The problem

In JavaScript undefined and nullvalues are hard to check. When using !variablecheck then

  • !undefined === true
  • !null === true
  • !false === true
  • !"" === true
  • !Number.NaN === true

This basically means

!undefined === !null === !false === !"" == !Number.NaN

which is OK in most cases but when working with data might make code quite messy and end up with the code like:

const parseNumber = (value?: number | null) => {
  if (value === undefined || value === null || Number.isNaN(value)) {
    return "Unknown";
  }
  return `${value}`
}

How would you like to write the same code as:

const parseNumber = (value?: number | null) => 
  hasValue(value) ? `${value}` : "Unknown";

The solution

To reduce the amount of code there are few simple methods described below. Also all methods are typesafe so there should be no problem using method such as:

const method1 = (value: string) => {
/** do something with the string **/
}

const method2 = (value: string | undefined | null) => {
    if (hasValue(value))
        method1(value);
    // or
    if (!hasNoValue(value))
        console.log("no value!");
    else
        method1(value);
}

There should be no TypeScript problems as hasValue enforces the value to be not null nor undefined.

hasValue method

The library solves the typescript issue with having null, undefined, false or empty string introducing methods:

  • hasValue(undefined) === false
  • hasValue(null) === false
  • hasValue(false) === true
  • hasValue("") === true
  • hasValue(Number.NaN) === false

hasNoValue method

Or reverted

  • hasNoValue(undefined) === true
  • hasNoValue(null) === true
  • hasNoValue(false) === false
  • hasNoValue("") === false
  • hasNoValue(Number.NaN) === true

valueOf method

And to get default values

  • valueOf(undefined, "abc") === "abc"
  • valueOf(null, "abc") === "abc"
  • valueOf(false, true) === false
  • valueOf("", "abc") === ""
  • valueOf(Number.NaN, 1) === 1

The second argument can also be a callable and is called when first is undefined, null or Number.Nan for more complex logic

  • valueOf(undefined, () => "abc") === "abc"
  • valueOf(null, () => "abc") === "abc"
  • valueOf(false, () => true) === false
  • valueOf("", () => "abc") === ""
  • valueOf(Number.NaN, () => 1) === 1

When, let's say we would like to get the default value from some endpoint or asynchronous code then we could use

  • valueOfAsync(undefined, async () => "abc") === "abc"
  • valueOfAsync(null, async () => "abc") === "abc"
  • valueOfAsync(false, async () => true) === false
  • valueOfAsync("", async () => "abc") === ""
  • valueOfAsync(Number.NaN, async () => 1) === 1

valuesOf method

Or to get value from array

valuesOf([null, 1, undefined, Number.NaN, 2]) === [1,2]

which basically can be written as [null, 1, undefined, Number.NaN, 2].filter(hasValue)but solves the issue with null or undefined:

  • valuesOf(null) === []
  • valuesOf(undefined) === []

And reduced the amount of code.

valueOfFloat and valueOfInteger methods

Also there is a method valueOfFloat and valueOfInteger which finds numbers from the string as in some regions comma is written as a dot.

  • valueOfFloat("ab.0.1") === 0.1

hasNoValueOrEquals

This is helper method to check if both variables are null or undefined or the same. It is shortcut for ((hasNoValue(left) && hasNoValue(right)) || left === right) With one small fix. Sometimes we need to dig deeper so another variable is optionally added to check the value. For example:

hasNoValueOrEquals(
  new Date(),
  new Date(),
  (left, right) => left.getMonth() == right.getMonth()
) === true;

The third argument is called when both first two arguments are not null, undefined nor Number.NaN.

Optional

And introduces implementation of Optional - not so popular but known from other languages for null safeness.