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.

41 lines
1.2 KiB

4 years ago
  1. 'use strict';
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  3. module.exports = function chunk(size) {
  4. var _this = this;
  5. var chunks = [];
  6. var index = 0;
  7. if (Array.isArray(this.items)) {
  8. do {
  9. var items = this.items.slice(index, index + size);
  10. var collection = new this.constructor(items);
  11. chunks.push(collection);
  12. index += size;
  13. } while (index < this.items.length);
  14. } else if (_typeof(this.items) === 'object') {
  15. var keys = Object.keys(this.items);
  16. var _loop = function _loop() {
  17. var keysOfChunk = keys.slice(index, index + size);
  18. var collection = new _this.constructor({});
  19. keysOfChunk.forEach(function (key) {
  20. return collection.put(key, _this.items[key]);
  21. });
  22. chunks.push(collection);
  23. index += size;
  24. };
  25. do {
  26. _loop();
  27. } while (index < keys.length);
  28. } else {
  29. chunks.push(new this.constructor([this.items]));
  30. }
  31. return new this.constructor(chunks);
  32. };