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.

32 lines
720 B

4 years ago
  1. 'use strict';
  2. module.exports = function partition(fn) {
  3. var _this = this;
  4. var arrays = void 0;
  5. if (Array.isArray(this.items)) {
  6. arrays = [new this.constructor([]), new this.constructor([])];
  7. this.items.forEach(function (item) {
  8. if (fn(item) === true) {
  9. arrays[0].push(item);
  10. } else {
  11. arrays[1].push(item);
  12. }
  13. });
  14. } else {
  15. arrays = [new this.constructor({}), new this.constructor({})];
  16. Object.keys(this.items).forEach(function (prop) {
  17. var value = _this.items[prop];
  18. if (fn(value) === true) {
  19. arrays[0].put(prop, value);
  20. } else {
  21. arrays[1].put(prop, value);
  22. }
  23. });
  24. }
  25. return new this.constructor(arrays);
  26. };