take.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  3. module.exports = function take(length) {
  4. var _this = this;
  5. if (!Array.isArray(this.items) && _typeof(this.items) === 'object') {
  6. var keys = Object.keys(this.items);
  7. var slicedKeys;
  8. if (length < 0) {
  9. slicedKeys = keys.slice(length);
  10. } else {
  11. slicedKeys = keys.slice(0, length);
  12. }
  13. var collection = {};
  14. keys.forEach(function (prop) {
  15. if (slicedKeys.indexOf(prop) !== -1) {
  16. collection[prop] = _this.items[prop];
  17. }
  18. });
  19. return new this.constructor(collection);
  20. }
  21. if (length < 0) {
  22. return new this.constructor(this.items.slice(length));
  23. }
  24. return new this.constructor(this.items.slice(0, length));
  25. };