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

@lightningspirit/isempty

v0.2.1

Published

Test for the emptiness of any value

Downloads

15

Readme

isempty

A tiny dependency-free TS library to test for emptiness of any value. This is a missing type-safe feature for JavaScript/TypeScript.

Installation

npm i @lightningspirit/isempty

Basic Usage

import isempty from '@lightningspirit/isempty';

const object = {};

console.log(
  isempty(object) ? "Object is empty" : "Not empty"
);

Usage where false can be empty

isempty(false) // false
isempty(false, {
  falseIsEmpty: true
}) // true

Test if a value is boolean false

isempty.false(0) // false
isempty.false('') // false
isempty.false(false) // true

Considerations

What is considered to be empty:

  • null, undefined, '' are empty values.
  • {} is an empty object.
  • [] is an empty array.
  • Map, Array, Set, Object without members are empty.

What is not empty:

  • false is not empty, it holds a boolean value (see below for the reason).
  • Infinity, NaN are not empty, they both have values.
  • new Error(), new Function() are not empty objects, they have their semantic and are treated diferently.
  • 0 is not empty, it is an integer with a value (see below the reason).
  • ' ' is not empty, has a space character.

Questions:

Why is 0 not considered empty?

  1. In mathematics, zero is a number in the set of real numbers with empty magnitude while null is a term used to denote the empty nature of a quantity or an entity.
  2. Many bugs occur because most dynamic languages consider 0 as null/false. Take the following example:
const position = "The lazy fox".indexOf("The"); // 0
isempty(position) // false

Why is false not considered empty by default?

While false is the negation of something being true, the value itself does state nothing.

It can either be considered true has it holds a meaning value or false as a result of a conditional statement.

In mathematics and logic, these kind of conditions are often called vacuous truth. We considered false as a universal statement, which means it exists and is not empty, and the programmer must give it the semantics.

If you want to override this behaviour you can pass the falseIsEmpty option to the function as such:

isempty(false, {
  falseIsEmpty: true
}) // true