versus

Compare two variables with the comparison operator specified as a string.

Usage no npm install needed!

<script type="module">
  import versus from 'https://cdn.skypack.dev/versus';
</script>

README

versus.js npm Version Build Status Coverage Status

Compare two variables with the comparison operator specified as a string.

(This is really just a silly-simple switch-case abstracted into a reusable module.)

Usage

versus(1, '==' , 2); //=> false
versus(1, '===', 2); //=> false
versus(1, '!=' , 2); //=> true
versus(1, '!==', 2); //=> true
versus(1, '<'  , 2); //=> true
versus(1, '>'  , 2); //=> false
versus(1, '<=' , 2); //=> true
versus(1, '>=' , 2); //=> false

Versus uses deep-equal for the == and != comparisons. So we can do:

versus({ foo: 1 }, '==', { foo: 1 }); //=> true
versus({ foo: 1 }, '!=', { foo: 1 }); //=> false

Read the tests.

API

versus(a, op, b)

Returns a boolean, the result of comparing a and b using the comparison operator op specified as a string. Throws an error if op is not a valid comparison operator.

  • a, b — The two variables to be compared.
  • op — One of ==, ===, !=, !==, <, >, <=, or >=.

Installation

Install via npm:

$ npm i --save versus

Changelog

  • 0.2.0
  • 0.1.0
    • Initial release

License

MIT license