Design & Download
Your JS Library
in Seconds!!!

Snippet: and() & or() & xor()

Written by Christopher West (cwest) on August 21, 2016.
Determine the result of using AND, OR, or XOR on an array of values.
'xor ^ &&a)&&(b|| ! and & &&  or | ||  '
  .replace(/(.+?) (.) (.+?) (.?) /g, function(m, name, op, ops, op2) {
    YourJS[name] = Function(
      'c,g',
      'for(var e=1,f=c.length,a=c[0],b=a;e<f;a=c[e++],b=g?b@a:@(b@a));return b'
        .replace('@', op)
        .replace('@', op2)
        .replace('@', ops)
    );
  });

and(...) API Documentation

Description

Determine the result of executing AND on an array of values. Defaults to logical calculations but bitwise calculations are also possible.
NOTE: Short-circuit evaluation is never used.

Parameters

  1. values {Array}:
    Array of the values to AND together.
  2. opt_bitwise {boolean}:
    Optional. Defaults to false. If trueish, logical AND will be used, otherwise, bitwise AND will be used.

Returns

Returns the result of executing AND on everything with the values array.

Example

/*** Logical AND ***/
console.log(YourJS.and([]));                 // -> undefined
console.log(YourJS.and([false]));            // -> false
console.log(YourJS.and([true]));             // -> true
console.log(YourJS.and(['No']));             // -> 'No'
console.log(YourJS.and(['',true]));          // -> ''
console.log(YourJS.and(['Yes',true,4]));     // -> 4

/*** Bitwise AND ***/
console.log(YourJS.and([], true));           // -> undefined
console.log(YourJS.and([0], true));          // -> 0
console.log(YourJS.and([1], true));          // -> 1
console.log(YourJS.and([1,2], true));        // -> 0
console.log(YourJS.and([7,14], true));       // -> 6
console.log(YourJS.and([14,7], true));       // -> 6
console.log(YourJS.and([15,14,5], true));    // -> 4
console.log(YourJS.and([false], true));      // -> false
console.log(YourJS.and([true], true));       // -> true
console.log(YourJS.and([false,true], true)); // -> 0
console.log(YourJS.and([true,false], true)); // -> 0
console.log(YourJS.and([true,true], true));  // -> 1

or(...) API Documentation

Description

Determine the result of executing OR on an array of values. Defaults to logical calculations but bitwise calculations are also possible.
NOTE: Short-circuit evaluation is never used.

Parameters

  1. values {Array}:
    Array of the values to OR together.
  2. opt_bitwise {boolean}:
    Optional. Defaults to false. If trueish, logical OR will be used, otherwise, bitwise OR will be used.

Returns

Returns the result of executing OR on everything with the values array.

Example

/*** Logical OR ***/
console.log(YourJS.or([]));                             // -> undefined
console.log(YourJS.or([false]));                        // -> false
console.log(YourJS.or([true]));                         // -> true
console.log(YourJS.or(['Bubble']));                     // -> 'Bubble'
console.log(YourJS.or([false,true]));                   // -> true
console.log(YourJS.or([true,false]));                   // -> true
console.log(YourJS.or([false,true,false]));             // -> true
console.log(YourJS.or([true,false,true]));              // -> true
console.log(YourJS.or([true,false,true,false]));        // -> true
console.log(YourJS.or([true,0,true,false,'Yes']));      // -> true

/*** Bitwise OR ***/
console.log(YourJS.or([], true));                       // -> undefined
console.log(YourJS.or([0], true));                      // -> 0
console.log(YourJS.or([1], true));                      // -> 1
console.log(YourJS.or([0,1], true));                    // -> 1
console.log(YourJS.or([1,0], true));                    // -> 1
console.log(YourJS.or([1,0,2], true));                  // -> 3
console.log(YourJS.or([1,0,2,5], true));                // -> 7
console.log(YourJS.or([0,2,5,true,false,false], true)); // -> 7

xor(...) API Documentation

Description

Determine the result of executing XOR on an array of values. Defaults to logical calculations but bitwise calculations are also possible.

Parameters

  1. values {Array}:
    Array of the values to XOR together.
  2. opt_bitwise {boolean}:
    Optional. Defaults to false. If trueish, logical XOR will be used, otherwise, bitwise XOR will be used.

Returns

Returns the result of executing XOR on everything with the values array.

Example

/*** Logical XOR ***/
console.log(YourJS.xor([]));                               // -> undefined
console.log(YourJS.xor([false]));                          // -> false
console.log(YourJS.xor([true]));                           // -> true
console.log(YourJS.xor(['Bubble']));                       // -> 'Bubble'
console.log(YourJS.xor([false,true]));                     // -> true
console.log(YourJS.xor([true,false]));                     // -> true
console.log(YourJS.xor([false,true,false]));               // -> true
console.log(YourJS.xor([true,false,true]));                // -> false
console.log(YourJS.xor([true,false,true,false]));          // -> false
console.log(YourJS.xor([true,false,false,true]));          // -> false
console.log(YourJS.xor([true,false,false,true,true]));     // -> true
console.log(YourJS.xor([true,false,true,false,true]));     // -> true
console.log(YourJS.xor([true,0,true,false,'Yes']));        // -> 'Yes'

/*** Bitwise XOR ***/
console.log(YourJS.xor([], true));                         // -> undefined
console.log(YourJS.xor([0], true));                        // -> 0
console.log(YourJS.xor([1], true));                        // -> 1
console.log(YourJS.xor([0,1], true));                      // -> 1
console.log(YourJS.xor([1,0], true));                      // -> 1
console.log(YourJS.xor([1,0,2], true));                    // -> 3
console.log(YourJS.xor([1,0,2,5], true));                  // -> 6
console.log(YourJS.xor([1,0,2,5,true,false,false], true)); // -> 7