Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check exactly one boolean option set

Well, this is kind of hacky:

function b2n(boo) {
    return boo ? 1 : 0;
}

if(b2n(opt1) + b2n(opt2) + b2n(opt3) !== 1) {
    throw new Error("Exactly one option must be set");
}

Is there a better way to do this in Javascript? Using any of

  • more intelligent boolean/number handling
  • sneaky array or functional operations

And so forth. Javascript and Node solutions welcome.

In my actual problem, the options are coming from the Node module commander, so I'm not dealing with true boolean, just truthy and falsy things. There may be a commander-solution too.

like image 264
djechlin Avatar asked Jun 25 '13 17:06

djechlin


3 Answers

Assuming you had an array of options, you could do:

if(opts.filter(Boolean).length !== 1) {}

It seems to me though that you ought to have one variable with three possible states instead...

var opt = 'a'; // (or 'b', or 'c')
like image 179
Stephen Sorensen Avatar answered Nov 14 '22 01:11

Stephen Sorensen


You can do this :

if ( !!opt1 + !!opt2 + !!opt3 !== 1 ) {

It works because

  • !! makes a boolean from any value (true if the objects evaluates as true in if(value))
  • when adding booleans you get 1 for true and 0 for false.
like image 6
Denys Séguret Avatar answered Nov 14 '22 01:11

Denys Séguret


You mentioned in your comment that this is coming from a commander options object.

You can do this more elegantly using Lodash:

if (_(options).values().compact().size() === 1)

If you only want to count a subset of the options, you can insert

.pick('a', 'b', 'c')
like image 1
SLaks Avatar answered Nov 14 '22 02:11

SLaks