New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

keymbinatorial

Package Overview
Dependencies
Maintainers
2
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

keymbinatorial - npm Package Compare versions

Comparing version

to
0.0.2

48

index.js
'use strict';
const clone = require('clone');
const joi = require('joi');
+/**
/**
* Helper function to recursively go through generating all the permutations

@@ -9,24 +10,30 @@ * @method generateMatrix

* @param {Object} currentCombination Object of Key => Value of current combination
* @param {Array} keysToIterate Array of keys to be adding to the combination.
Should be the Array of keys of object options
* @param {Number} keyIndexer Current index of key to be adding into the currentCombination
* @param {Array} permutations The permutations array to be pushing to
* @return {Array} List of permutations of the environment
*/
function generateMatrixHelper(options, currentCombination, keysToIterate, keyIndexer, permutations) {
function generateMatrixHelper(options, currentCombination,
keysToIterate, keyIndexer, permutations) {
if (keysToIterate.every((optionKey) => currentCombination[optionKey])) {
permutations.push(currentSettings);
return;
}
permutations.push(currentCombination);
if (keyIndexer >= keysToIterate.length) {
return;
}
let keyToAdd = keysToIterate[keyIndexer];
keyIndexer += 1;
const keyToAdd = keysToIterate[keyIndexer];
const nextKeyIndexer = keyIndexer + 1;
options[keyToAdd].forEach( (value) => {
currentCombination[keyToAdd] = value;
return generateMatrixHelper(options, clone(currentCombination), keysToIterate, keyIndexer, permutations);
options[keyToAdd].forEach((value) => {
const nextCombination = clone(currentCombination);
nextCombination[keyToAdd] = value;
return generateMatrixHelper(options, nextCombination,
keysToIterate, nextKeyIndexer, permutations);
});
}
+/**
/**
* Generate all permutations of a given environment matrix

@@ -38,16 +45,11 @@ * @method generateMatrix

function generateMatrix(options) {
let thing = {};
let settings = {};
let perms = []
const perms = [];
const keysToIterate = Object.keys(options);
let keysToIterate = Object.keys(options);
let keyIndexer = 0;
let keyToStart = keysToIterate[keyIndexer];
options[keyToStart].forEach( (value) => {
// set { v: 1 } and then { v: 2 }
thing[keyToStart] = value;
return generateMatrixHelper(options, clone(thing), keysToIterate, 1, perms)
keysToIterate.forEach((key) => {
joi.assert(options[key], joi.array(), `${key} needs to be an array`);
});
generateMatrixHelper(options, {}, keysToIterate, 0, perms);
return perms;

@@ -54,0 +56,0 @@ }

{
"name": "keymbinatorial",
"version": "0.0.1",
"version": "0.0.2",
"description": "Module that generates the unique combinations of key values by taking a single value from each keys array",

@@ -18,3 +18,5 @@ "main": "index.js",

"screwdriver",
"yahoo"
"yahoo",
"combinations",
"combinatorial"
],

@@ -42,4 +44,5 @@ "license": "BSD-3-Clause",

"dependencies": {
"clone": "^1.0.2"
"clone": "^1.0.2",
"joi": "^9.0.1"
}
}
}
# Keymbinatorial
[![Version][npm-image]][npm-url] ![Downloads][downloads-image] [![Build Status][wercker-image]][wercker-url] [![Open Issues][issues-image]][issues-url] [![Dependency Status][daviddm-image]][daviddm-url] ![License][license-image]
> Module that generates the unique combinations of key values by taking a single value from each keys array
> Generates the unique combinations of key values by taking a single value from each keys array

@@ -9,5 +9,43 @@ ## Usage

```bash
npm install screwdriver-keymbinatorial
npm install keymbinatorial
```
```js
var keymbinatorial = require('keymbinatorial');
let objectToCombine = {
a: ['a', 'b', 'c'],
c: [1, 2],
e: [{ a: '1'}, {b: '2'}]
};
// combinations will be an array of unique combinations based on each key and the values in the array
let combinations = keymbinatorial(objectToCombine);
console.log(combinations);
/*
will output:
[
{ a: 'a', c: 1, e: { a: '1' } },
{ a: 'a', c: 1, e: { b: '2' } },
{ a: 'a', c: 2, e: { a: '1' } },
{ a: 'a', c: 2, e: { b: '2' } },
{ a: 'b', c: 1, e: { a: '1' } },
{ a: 'b', c: 1, e: { b: '2' } },
{ a: 'b', c: 2, e: { a: '1' } },
{ a: 'b', c: 2, e: { b: '2' } },
{ a: 'c', c: 1, e: { a: '1' } },
{ a: 'c', c: 1, e: { b: '2' } },
{ a: 'c', c: 2, e: { a: '1' } },
{ a: 'c', c: 2, e: { b: '2' } }
]
*/
```
The `keymbinatorial` function takes in an Nx1 object, where N is a set of keys that map to
an array.
For each key in the object, the function builds up a list of objects containing a unique combination
of keys to values in the array.
## Testing

@@ -14,0 +52,0 @@

@@ -5,5 +5,80 @@ 'use strict';

describe('index test', () => {
it('fails', () => {
assert.isTrue(false);
let index;
beforeEach(() => {
/* eslint-disable global-require */
index = require('../index');
/* eslint-enable global-require */
});
it('throws exception with invalid options', () => {
const objectToCombine = {
a: [1, 2],
b: 2
};
assert.throws(() => {
index(objectToCombine);
});
});
describe('returns correct', () => {
it('simple example', () => {
const objectToCombine = {
a: [1, 2]
};
const correctCombination = [
{
a: 1
},
{
a: 2
}
];
assert.deepEqual(index(objectToCombine), correctCombination);
});
it('complex example', () => {
const objectToCombine = {
a: [1, 2, 3],
b: ['a'],
c: ['c', 'd']
};
const correctCombination = [
{
a: 1,
b: 'a',
c: 'c'
},
{
a: 1,
b: 'a',
c: 'd'
},
{
a: 2,
b: 'a',
c: 'c'
},
{
a: 2,
b: 'a',
c: 'd'
},
{
a: 3,
b: 'a',
c: 'c'
},
{
a: 3,
b: 'a',
c: 'd'
}
];
assert.deepEqual(index(objectToCombine), correctCombination);
});
});
});