JSPM

  • Created
  • Published
  • Downloads 62978
  • Score
    100M100P100Q170811F
  • 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 Coverage Status bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies Downloads/Month

Table of Contents

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 an AVA test, which uses objectBooleanCombinations() to create a combinations array of settings objects, then uses forEach() to iterate through them all, testing each:

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

Testing

$ npm test

Unit tests use AVA, Istanbul CLI and JS Standard notation.

Contributing

All contributions are welcome. Please stick to Standard JavaScript notation and supplement the test.js with new unit tests covering your feature(s).

If you see anything incorrect whatsoever, do raise an issue. If you file a pull request, I'll do my best to help you to get it merged in a timely manner. If you have any comments on the code, including ideas how to improve things, don't hesitate to contact me by email.

Licence

MIT License (MIT)

Copyright (c) 2017 Codsen Ltd, Roy Reveltas

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.