JSPM

  • Created
  • Published
  • Downloads 62978
  • Score
    100M100P100Q165969F
  • License MIT

Generate an array full of object copies, each containing a unique Boolean value combination. Includes overrides.

Package Exports

  • object-boolean-combinations

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (object-boolean-combinations) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

object-boolean-combinations

Standard JavaScript

Take object, generate an array of its copies, each containing all possible combinations of Boolean true/false

Build Status bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies

Install

npm i object-boolean-combinations --save
npm test

API

objectBooleanCombinations(inputObject, [overrideObject]);

Usage

INPUT - OBJECT:

{
    a : true,
    b : true
}

OUTPUT - ARRAY OF OBJECTS:

[
  {
       a : true,
      b : true
  },
  {
      a : true,
      b : false
  },
  {
      a : false,
      b : true
  },
  {
      a : false,
      b : false
  }
]

That's nice, however we're not done here.

Overriding

Sometimes you want to override the object keys, for example, in the a settings object, I want to override all a keys to be only true. This reduces the object combinations from 2^2 to 2^(2-1):

INPUT - OBJECT:

{
    a : true,
    b : true
}

OVERRIDE OBJECT:

{
    a : true
}

RESULT - ARRAY OF OBJECTS:

[
  {
       a : true,
       b : true
  },
  {
      a : true,
      b : false
  }
]

Overriding the combinations — in practice

In practice, I use this overriding to perform the specific tests on Detergent.js. For example, let's say, I am testing: does Detergent encode entities correctly. In that case I need two arrays filled with objects:

  • first array — encodeEntities = true and all possible combinations of the other 9 settings (2^(10-1)=512 objects in array)
  • second array — encodeEntities = false and all possible combinations of the rest — again 512 objects in array.

Here's a Tape test, which uses objectBooleanCombinations() to create a combinations array of settings objects, then uses forEach() to iterate through them all, testing each using Tape's t.equal():

test('encode entities - pound sign', function (t) {
  objectBooleanCombinations(sampleObj, {
    convertEntities: true
    })
  .forEach(function (elem){
    t.equal(detergent(
      '\u00A3', elem),
      '£',
      'pound char converted into entity'
    );
  });
  t.end();
});

Licence

MIT © Roy Reveltas