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

babel-plugin-syntax-exists

v0.0.3

Published

Allows parsing of an existential check in Babel

Downloads

18

Readme

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

  • [x] Return true if value exists
  • [x] Return false if value is null or undefined
  • [x] Return child if parent exists
  • [ ] Return false on child if parent is null or undefined
  • [x] Return function call if value is a function
  • [x] 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

Via .babelrc (Recommended)

babelrc

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

Via CLI

$ babel --plugins syntax-exists script.js

Via Node API

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