Socket
Socket
Sign inDemoInstall

not-defined

Package Overview
Dependencies
0
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    not-defined

checks if foo is not defined, i.e. undefined, null, an empty string, array, object or NaN


Version published
Weekly downloads
119
increased by17.82%
Maintainers
1
Install size
4.60 kB
Created
Weekly downloads
 

Readme

Source

not-defined

checks if foo is not defined, i.e. undefined, null, an empty string, array, object or NaN

Installation | Usage | License

NPM version

Installation

npm install not-defined

Usage

This snippet of code

import notDefined from 'not-defined'

if (notDefined(foo)) {
  // do something, usually throw a TypeError
}

is equivalent to the following pseudocode

if (foo is not defined, i.e. is null, undefined, NaN, an empty string, array or object) {
  // do something, usually throw a TypeError
}

You can also use a shorter but still semantic form like

import no from 'not-defined'

if (no(foo)) {
  // do something, usually throw a TypeError
}

Follows a list of tested examples

no() // true
no(undefined) // true
no(null) // true
no('') // true
no([]) // true
no({}) // true
no(NaN) // true

no(0) // false
no(true) // false
no(false) // false
no('string') // false
no(['foo']) // false
no({ foo: true }) // false
no(42) // false
no(Infinity) // false
no(function () { return 1 }) // false

Pros

  • Type less.
  • Better readability (even your boss will understand your code ^:).
  • Can save bytes in your builds.
  • Easier to autocomplete in editors (for instance easier than typeof foo === 'undefined').

License

MIT

Keywords

FAQs

Last updated on 07 Mar 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc