@shnhrrsn/babel-plugin-transform-isnil

replace the comparing of null or undefined with isNil

Usage no npm install needed!

<script type="module">
  import shnhrrsnBabelPluginTransformIsnil from 'https://cdn.skypack.dev/@shnhrrsn/babel-plugin-transform-isnil';
</script>

README

@shnhrrsn/babel-plugin-transform-isNil

Legacy

This has been updated for Babel 7 for backwards compatibilty of existing code, however new code should migrate to the chaining operator: @babel/plugin-proposal-optional-chaining

Forked from MaxMEllon

This was forked from MaxMEllon/babel-plugin-transform-isNil, which has since been deprecated, and updated to work with Babel 7.

@shnhrrsn/babel-plugin-transform-isNil

About

I like Existential Operator in CoffeeScript. CoffeeScript can be written as follows:

hoge?

Become to JavaScript

hoge == null

Same meaning

hoge === null || hoge === undefined

I want to do the same thing in JavaScript.

Installation

$ npm install --save @shnhrrsn/babel-plugin-transform-isNil

Example

In

if (foo.isNil) {
  console.log('foo is null or undefined');
}

Out

if (foo === null || foo === undefined) {
  console.log('foo is null or undefined');
}

In

if (hoge.poge.isNil && foo.bar.isNil) {
  console.log('hoge.poge and foo.bar is null or undefined');
}

Out

if ((hoge.poge === null || hoge.poge === undefined) && (foo.bar === null || foo.bar === undefined)) {
  console.log('hoge.poge and foo.bar is null or undefined');
}

In

if (hoge.poge().isNil) {
  console.log('returned value of hoge.poge() function is null or undefined');
}

Out

if (hoge.poge() === null || hoge.poge() === undefined) {
  console.log('returned value of hoge.poge() function is null or undefined');
}

In

if (hoge.poge(hoge).isNil) {
  console.log('returned value of hoge.poge() function is null or undefined');
}

Out

if (hoge.poge(hoge) === null || hoge.poge(hoge) === undefined) {
  console.log('returned value of hoge.poge() function is null or undefined');
}

Usage

Via .babelrc

{
  "plugins": ["@shnhrrsn/plugin-transform-isNil"]
}

Development

Requirement global

  • Node v4 or above
$ git clone https://github.com/shnhrrsn/babel-plugin-transform-isNil
$ cd babel-plugin-transform-isNil
$ npm install

$ npm test

Special Thanks

LICENSE

MIT