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
Generate an array full of object copies, each containing a unique Boolean value combination. Includes overrides.
Table of Contents
Install
npm i object-boolean-combinations// consume as a CommonJS require:
const objectBooleanCombinations = require("object-boolean-combinations");
// or as an ES Module:
import objectBooleanCombinations from "object-boolean-combinations";Here's what you'll get:
| Type | Key in package.json |
Path | Size |
|---|---|---|---|
Main export - CommonJS version, transpiled to ES5, contains require and module.exports |
main |
dist/object-boolean-combinations.cjs.js |
2 KB |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export. |
module |
dist/object-boolean-combinations.esm.js |
2 KB |
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-in |
browser |
dist/object-boolean-combinations.umd.js |
16 KB |
What it does
It consumes a plain object, takes its keys (values don't matter) and produces an array with every possible combination of each key's Boolean^ value. If you have n keys, you'll get 2^n objects in the resulting array.
const objectBooleanCombinations = require("object-boolean-combinations");
const test = objectBooleanCombinations({ a: "whatever" });
console.log(`test = ${JSON.stringify(test, null, 4)}`);
// => [
// {a: 0},
// {a: 1}
// ]^ We could generate true/false values, but for efficiency, we're generating 0/1 instead. Works the same in Boolean logic, but takes up less space.
PS. Observe how input values don't matter, we had: { a: 'whatever' }.
Sometimes, you don't want all the combinations, you might want to "pin" certain values to be constant across all combinations. In those cases, use overrides, see below.
API
objectBooleanCombinations(inputObject, [overrideObject]);API - Input
| Input argument | Type | Obligatory? | Description |
|---|---|---|---|
inputObject |
Plain object | yes | Plain object from which we should reference the keys. |
overrideObject |
Plain object | no | Keys in this object will be used as-is and will not be used for generating combinations. See overriding section below. |
Overriding
Sometimes you want to override the object keys, for example, in the a settings object, I want to override all a and b keys to be only true (1). This reduces the object combinations from 2^3 = 8 to: 2^(3-2) = 2^1 = 2:
const objectBooleanCombinations = require("object-boolean-combinations");
const test = objectBooleanCombinations(
{ a: 0, b: 0, c: 0 },
{ a: 1, b: 1 } // <----- Override. These values will be on all combinations.
);
console.log(`test = ${JSON.stringify(test, null, 4)}`);
// => [
// {a: 1, b: 1, c: 0},
// {a: 1, b: 1, c: 1}
// ]In example above, a and b are "pinned" to 1, thus reducing the amount of combinations by power of two, essentially halving resulting objects count twice. Notice how only c is having variations.
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 = trueand all possible combinations of the other 9 settings (2^(10-1)=512 objects in array) - second array —
encodeEntities = falseand 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"
);
});
});Contributing
If you want a new feature in this package or you would like us to change some of its functionality, raise an issue on this repo.
If you tried to use this library but it misbehaves, or you need advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo.
If you would like to add or change some features, just fork it, hack away, and file a pull request. We'll do our best to merge it quickly. Prettier is enabled, so you don't need to worry about the code style.
Licence
MIT License (MIT)
Copyright © 2018 Codsen Ltd, Roy Revelt