xor-js

Basic XOR in JS

Usage no npm install needed!

<script type="module">
  import xorJs from 'https://cdn.skypack.dev/xor-js';
</script>

README

xor-js

Node.js CI npm Version npm Downloads Monthly

This package provides a rudimentary XOR check of a list of arguments in JS. Given any arity of arguments it will return true iff only one of the arguments is truthy, otherwise, it will return false.

Example Usage

npm install xor-js
# XOR ;)
yarn add xor-js

A powerful application is to combine this package with invariant to create rules for your API.

import xor from 'xor-js';
import invariant from 'invariant';

function MyComponent({ primary, secondary, tertiary, ...props }) {
  invariant(xor(primary, secondary, tertiary), 'Only one of primary, secondary, or tertiary may be true.')
  
  return (...);
}

Credits

This was adapted and inspired from this post on Code Review.