larvitutils
Advanced tools
Comparing version 3.1.1 to 3.2.0
/// <reference types="node" /> | ||
import { Log, LogLevel, LogOptions } from './log'; | ||
declare type KeyValues = { | ||
[key: string]: string[]; | ||
}; | ||
declare type LogInstance = { | ||
@@ -11,2 +14,5 @@ silly(msg: string): void; | ||
}; | ||
declare type UniqueKeyValues = { | ||
[key: string]: string; | ||
}; | ||
declare type UtilsOptions = { | ||
@@ -20,2 +26,3 @@ log?: LogInstance; | ||
constructor(options?: UtilsOptions); | ||
getUniqueCombinations(keyValues: KeyValues): UniqueKeyValues[]; | ||
/** | ||
@@ -22,0 +29,0 @@ * Convert hrtime diff to milliseconds |
@@ -52,2 +52,35 @@ "use strict"; | ||
} | ||
Utils.prototype.getUniqueCombinations = function (keyValues) { | ||
var response = []; | ||
function addUniqueCombination(curKeyVals, subKeyValues) { | ||
var workSubKeyValuesName = Object.keys(subKeyValues)[Object.keys(curKeyVals).length]; | ||
var workSubKeyValues = subKeyValues[workSubKeyValuesName]; | ||
// If this is the last key val to look for, loop through them all, add them and return them | ||
if (Object.keys(curKeyVals).length === Object.keys(subKeyValues).length) { | ||
response.push(curKeyVals); | ||
// Loop through next key in line | ||
} | ||
else { | ||
for (var i = 0; workSubKeyValues.length !== i; i++) { | ||
var nextKeyVal = Object.assign({}, curKeyVals); | ||
var workSubKeyValue = workSubKeyValues[i]; | ||
nextKeyVal[workSubKeyValuesName] = workSubKeyValue; | ||
addUniqueCombination(nextKeyVal, subKeyValues); | ||
} | ||
} | ||
} | ||
if (Object.keys(keyValues).length === 0) { | ||
return response; | ||
} | ||
// Initiate the getter by calling addUniqueCombination() for each value on the first key | ||
var firstKeyValuesName = Object.keys(keyValues)[0]; | ||
var firstKeyValues = keyValues[firstKeyValuesName]; | ||
for (var i = 0; firstKeyValues.length !== i; i++) { | ||
var firstKeyValue = firstKeyValues[i]; | ||
var initKeyValList = {}; | ||
initKeyValList[firstKeyValuesName] = firstKeyValue; | ||
addUniqueCombination(initKeyValList, keyValues); | ||
} | ||
return response; | ||
}; | ||
/** | ||
@@ -54,0 +87,0 @@ * Convert hrtime diff to milliseconds |
{ | ||
"name": "larvitutils", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"description": "Misc utils", | ||
@@ -10,3 +10,3 @@ "main": "build/index.js", | ||
"devDependencies": { | ||
"@types/node": "12.7.1", | ||
"@types/node": "12.7.2", | ||
"@types/tape": "4.2.33", | ||
@@ -22,5 +22,5 @@ "coveralls": "3.0.6", | ||
"scripts": { | ||
"build": "tsc", | ||
"build": "npm run lint && tsc", | ||
"lint": "tslint ./src/* ./test/*", | ||
"test": "npm run lint && ts-node node_modules/.bin/tape test/*.ts" | ||
"test": "npm run build && ts-node node_modules/.bin/tape test/*.ts" | ||
}, | ||
@@ -27,0 +27,0 @@ "repository": { |
@@ -33,2 +33,4 @@ [![Build Status](https://travis-ci.org/larvit/larvitutils.svg)](https://travis-ci.org/larvit/larvitutils) | ||
v3.2.0 - Added getUniqueCombinations() | ||
v3.0.4 - Rewrite to TypeScript. Different initialization of the library (Log is no longer part of Utils). Log() now require exactly 'none' for no log output. Random strings will no longer be accepted. | ||
@@ -38,2 +40,28 @@ | ||
## Get unique combinations | ||
Breaks up object with key-values to an array of all possible, unique key-values. | ||
Example: | ||
```javascript | ||
const { Utils } = require('larvitutils'); | ||
const utils = new Utils(); | ||
console.log(utils.getUniqueCombinations({ | ||
foo: ['bar', 'baz'], | ||
buu: ['lenny', 'bosse'] | ||
})); | ||
/* | ||
Outputs: | ||
[ | ||
{ "foo": "bar, "buu": "lenny" }, | ||
{ "foo": "bar, "buu": "bosse" }, | ||
{ "foo": "baz, "buu": "lenny" }, | ||
{ "foo": "baz, "buu": "bosse" }, | ||
] | ||
*/ | ||
``` | ||
## Async setTimeout | ||
@@ -40,0 +68,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22573
401
193