Skip to content

FGRibreau/json-combine

Repository files navigation

json-combine

Generate every possible combination of values

Build Status Coverage Status Deps NPM version

Get help on Codementor available-for-advisory extra Slack

npm

npm install json-combine --save

usage

const {flatten, combine} = require('json-combine');

// mutation to apply
const mutations = [
  {
    key: 'a.b.c',
    values:[true, false]
  },
  {
    key: 'a.b.d.e',
    values:[1, 2, 3]
  },
];

// base object
const template = {
  a:{
    b:{
      c: false,
      d:{
        e: 0
      }
    }
  }
};

console.log(combine(flatten(mutations), template));

Outputs an array of every possible combinations :

[
 {
   "a": {
     "b": {
       "c": true,
       "d": {
         "e": 1
       }
     }
   }
 },
 {
   "a": {
     "b": {
       "c": true,
       "d": {
         "e": 2
       }
     }
   }
 },
 {
   "a": {
     "b": {
       "c": true,
       "d": {
         "e": 3
       }
     }
   }
 },
 {
   "a": {
     "b": {
       "c": false,
       "d": {
         "e": 1
       }
     }
   }
 },
 {
   "a": {
     "b": {
       "c": false,
       "d": {
         "e": 2
       }
     }
   }
 },
 {
   "a": {
     "b": {
       "c": false,
       "d": {
         "e": 3
       }
     }
   }
 }
]

test

npm test

About

Generate every possible combination of values

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published