UNPKG

iterama

Version:

Composable functional (async) iterable helpers

132 lines (101 loc) 4.49 kB
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.skip = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _circularr = _interopRequireDefault(require("circularr")); var _iterate = require("./iterate"); function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } var skipFirst = function skipFirst(n) { return function (iterable) { return (0, _defineProperty2.default)({}, Symbol.iterator, _regenerator.default.mark(function _callee() { var iterator, i, _iterator, _step, value; return _regenerator.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: iterator = (0, _iterate.iterate)(iterable); i = 0; while (i < n && !iterator.next().done) { i++; } _iterator = _createForOfIteratorHelperLoose(iterator); case 4: if ((_step = _iterator()).done) { _context.next = 10; break; } value = _step.value; _context.next = 8; return value; case 8: _context.next = 4; break; case 10: case "end": return _context.stop(); } } }, _callee); })); }; }; var skipLast = function skipLast(n) { return function (iterable) { return (0, _defineProperty2.default)({}, Symbol.iterator, _regenerator.default.mark(function _callee2() { var iterator, last, i, result, _iterator2, _step2, value; return _regenerator.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: iterator = (0, _iterate.iterate)(iterable); last = new _circularr.default(n); i = 0; case 3: if (!(i < n)) { _context2.next = 11; break; } result = iterator.next(); if (!result.done) { _context2.next = 7; break; } return _context2.abrupt("break", 11); case 7: last.shift(result.value); case 8: i++; _context2.next = 3; break; case 11: _iterator2 = _createForOfIteratorHelperLoose(iterator); case 12: if ((_step2 = _iterator2()).done) { _context2.next = 18; break; } value = _step2.value; _context2.next = 16; return last.shift(value); case 16: _context2.next = 12; break; case 18: case "end": return _context2.stop(); } } }, _callee2); })); }; }; var skip = function skip(n) { return n < 0 ? skipLast(-n) : skipFirst(n); }; exports.skip = skip; //# sourceMappingURL=skip.js.map