babel-plugin-comparison-chaining

Unlike the discipline of mathematics, JavaScript interpreters don't recognize chains of comparison operators as a linear sequence, but as recursive calls compare the right-most node and everything to the left of the operator. If a developer wishes to write conditionally chained expressions that are linearly evaluated, this plugin is of use.

Usage no npm install needed!

<script type="module">
  import babelPluginComparisonChaining from 'https://cdn.skypack.dev/babel-plugin-comparison-chaining';
</script>

README

babel-plugin-comparison-chaining

Unlike the discipline of mathematics, JavaScript interpreters don't recognize chains of comparison operators as a linear sequence, but as recursive calls compare the right-most node and everything to the left of the operator. If a developer wishes to write conditionally chained expressions that are linearly evaluated, this plugin is of use.

Examples

JavaScript will recursively evaluate the right-most node compared against whatever is on the left.

// written
1 < 6 < 4 < 2 < 7

//evaluated
   (1 < 6 < 4 < 2) < 7 
  ((1 < 6 < 4) < 2) < 7
 (((1 < 6) < 4) < 2) < 7
((((1) < 6) < 4) < 2) < 7

Written expression evaluated linearly

1 < 6 && 6 < 4 && 4 < 2 && 2 < 7;

Installation

$ npm install --save-dev babel-plugin-comparison-chaining

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["comparison-chaining"]
}

Via CLI

$ babel --plugins comparison-chaining script.js

Via Node API

require('babel-core').transform('code', {
  plugins: ['comparison-chaining']
});

License

MIT