Skip to content

Instantly share code, notes, and snippets.

@KOBA789
Created December 11, 2011 07:05
Show Gist options
  • Select an option

  • Save KOBA789/1458993 to your computer and use it in GitHub Desktop.

Select an option

Save KOBA789/1458993 to your computer and use it in GitHub Desktop.
var toList = function (value) {
return Array.isArray(value) ? value : [value];
};
var range = function (from, to) {
var result = [];
for (var i = from; i <= to; i ++) {
result.push(i);
}
return result;
};
var combine = function () {
var args = Array.prototype.slice.call(arguments);
var lists = args.map(toList);
var first = lists.shift();
var second = lists.shift();
var result = [];
for (var j = 0; j < first.length; j ++) {
for (var i = 0; i < second.length; i ++) {
result.push(String(first[j]) + String(second[i]));
}
}
return lists.length === 0 ? result : combine.apply(this, [result].concat(lists));
};
console.log(combine('hoge', ['foo', 'bar'], range(1, 3)));
// => ["hogefoo1", "hogefoo2", "hogefoo3", "hogebar1", "hogebar2", "hogebar3"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment