Skip to content

Instantly share code, notes, and snippets.

@dela3499
Last active March 1, 2016 12:34
Show Gist options
  • Save dela3499/5257a9da6e520d17b6d3 to your computer and use it in GitHub Desktop.
Save dela3499/5257a9da6e520d17b6d3 to your computer and use it in GitHub Desktop.
Given a format string, replace appropriate fields with all combinations of input lists.
function hasValue(x) {
return x.toString().length > 0;
}
function hasValues(values) {
for (var i = 0; i < values.length; i += 1) {
if (values[i].toString().length > 0) {
return true;
}
}
return false
}
function dropEmptyCells(x) {
return _.filter(x, hasValue)
}
function cleanAndCross(x) {
return cross(_.map(_.filter(transpose(x), hasValues), dropEmptyCells))
}
// http://stackoverflow.com/questions/17428587/transposing-a-2d-array-in-javascript
function transpose(array) {
return _.zip.apply(_, array)
};
// https://gist.github.com/ijy/6094414
function cartesianProductOf() {
return _.reduce(arguments, function(a, b) {
return _.flatten(_.map(a, function(x) {
return _.map(b, function(y) {
return x.concat([y]);
});
}), true);
}, [ [] ]);
};
function cross(x) {
return cartesianProductOf.apply(this, x)
}
function contains(a,b) {
return a.indexOf(b) != -1;
}
/* ------------------------------------------------
String -> Array String -> Array String -> String
------------------------------------------------
Given a format string, an array of search strings, and a an array of
replacement strings, replace the search strings in the format string.
*/
function multireplace(formatString, searchStrings, replaceStrings) {
var ret = formatString;
for (var i = 0; i < searchStrings.length; i += 1) {
ret = ret.replace(new RegExp(searchStrings[i], 'g'), replaceStrings[i]);
}
return ret
}
/* --------------------------------------------------------------
String -> Array String -> Array (Array String) -> Array String
--------------------------------------------------------------
Given a format string, an array of search strings, and multiple arrays of
replacement strings, return new format strings with replacements.
*/
function formatCombinations(formatString, searchStrings, combinations) {
return combinations.map(function(replaceStrings){return multireplace(formatString, searchStrings, replaceStrings)})
}
/* --------------------------------------------------------
String -> Array (Array String) -> Array String
--------------------------------------------------------
Given a format string, and multiple arrays, where the first are target strings,
and the rest replacement strings, return new format strings with replacements.
*/
function generateAllCombinations(string, array) {
function isValidColumnData(x) {
return hasValue(x[0]) && hasValues(_.tail(x)) && contains(string, x[0])
}
var columns = _.map(_.filter(transpose(array), isValidColumnData), dropEmptyCells)
var searchStrings = _.map(columns, _.head)
var replaceStrings = _.map(columns, _.tail)
var combinations = cross(replaceStrings)
return formatCombinations(string, searchStrings, combinations)
}
function numCombinations(string, array) {
function isValidColumnData(x) {
return hasValue(x[0]) && hasValues(_.tail(x)) && contains(string, x[0])
}
var columns = _.map(_.filter(transpose(array), isValidColumnData), dropEmptyCells)
var searchStrings = _.map(columns, _.head)
var replaceStrings = _.map(columns, _.tail)
var combinations = cross(replaceStrings)
return "# Combinations: " + combinations.length
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment