New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-syntax-exists

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-syntax-exists

Allows parsing of an existential check in Babel

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

Babel Plugin: syntax-exists

This is a Babel plugin which adds syntax for making existential checks in your code.
It uses the the keyword ex which is short for exists. :sparkles:

Build Status Dependency Status devDependency Status

Features

  • Return true if value exists
  • Return false if value is null or undefined
  • Return child if parent exists
  • Return false on child if parent is null or undefined
  • Return function call if value is a function
  • Return false for function call if value is not a function

What?

From The Little Book on Coffeescript:

Using if for null checks in JavaScript is common, but has a few pitfalls in that empty strings and zero are both coerced into false, which can catch you out. CoffeeScript existential operator ? returns true unless a variable is null or undefined, similar to Ruby's nil?.

So what does it look like?

foo.ex compiles to typeof foo !== "undefined" && foo !== null

How about a more exhilirating use case?

Given an object:

let obj = {
  attr: 'value',
  nested_attr: { name: 'nested' },
  func: () => 777
}

We can write write the following statements and expect the subsequent returns:

obj.ex.attr              // 'value'
obj.attr.ex              // true
obj.nested_attr.ex.name  // 'nested'

// Oh, and it works for functions too...
// --------------------------------------
obj.func.ex              // true
obj.func.ex()            // 777

// But what about undefined errors?
// --------------------------------------
obj.doesnt_exist.ex      // false
// obj.doesnt_exist.ex.nope // false
obj.not_a_func.ex()      // false

Installation

$ npm install babel-plugin-syntax-exists

Usage

babelrc

{
  "plugins": ["syntax-exists"]
}

Via CLI

$ babel --plugins syntax-exists script.js

Via Node API

require('babel').transform('code',{
  plugins: ['syntax-exists']
});

Keywords

FAQs

Package last updated on 12 Oct 2016

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc