Created
December 11, 2011 07:05
-
-
Save KOBA789/1458993 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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