You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
838 B

4 years ago
  1. 'use strict';
  2. module.exports = function crossJoin() {
  3. function join(collection, constructor, args) {
  4. var current = args[0];
  5. if (current instanceof constructor) {
  6. current = current.all();
  7. }
  8. var rest = args.slice(1);
  9. var last = !rest.length;
  10. var result = [];
  11. for (var i = 0; i < current.length; i += 1) {
  12. var collectionCopy = collection.slice();
  13. collectionCopy.push(current[i]);
  14. if (last) {
  15. result.push(collectionCopy);
  16. } else {
  17. result = result.concat(join(collectionCopy, constructor, rest));
  18. }
  19. }
  20. return result;
  21. }
  22. for (var _len = arguments.length, values = Array(_len), _key = 0; _key < _len; _key++) {
  23. values[_key] = arguments[_key];
  24. }
  25. return new this.constructor(join([], this.constructor, [].concat([this.items], values)));
  26. };