UNPKG

macoolka-collection

Version:

`macoolka-collection` Define Data Collection Interface.

108 lines 4.16 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.initIndexReaderCollection = void 0; var pipeable_1 = require("fp-ts/pipeable"); var O = require("fp-ts/Option"); function initIndexReaderCollection(options) { var size = options.size, getiterator = options.getiterator, _getAt = options._getAt, reverse = options.reverse; var getAt = function (i) { return function (as) { return O.fromNullable(_getAt(i)(as)); }; }; var existAt = function (i) { return function (as) { return i >= 0 && i < size(as); }; }; var forEachIndex = function (f) { return function (as) { var e_1, _a; var iterator = (0, pipeable_1.pipe)(as, getiterator); var i = 0; try { for (var iterator_1 = __values(iterator), iterator_1_1 = iterator_1.next(); !iterator_1_1.done; iterator_1_1 = iterator_1.next()) { var value = iterator_1_1.value; var a = f(i, value); if (a === false) { break; } i = ++i; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (iterator_1_1 && !iterator_1_1.done && (_a = iterator_1.return)) _a.call(iterator_1); } finally { if (e_1) throw e_1.error; } } }; }; var findIndex = function (predicate) { return function (as) { var result = []; (0, pipeable_1.pipe)(as, forEachIndex(function (i, value) { if (predicate(value)) { result.push(i); // return false } return true; })); return result; }; }; var findFirstIndex = function (predicate) { return function (as) { var result = O.none; (0, pipeable_1.pipe)(as, forEachIndex(function (i, value) { if (predicate(value)) { result = O.some(i); return false; } return true; })); return result; }; }; var findLastIndex = function (predicate) { return function (as) { return (0, pipeable_1.pipe)(as, reverse, findFirstIndex(predicate), O.map(function (value) { return size(as) - value - 1; })); }; }; var everyIndex = function (predicate) { return function (as) { var result = true; (0, pipeable_1.pipe)(as, forEachIndex(function (i, a) { if (!predicate(i, a)) { result = false; return false; } return true; })); return result; }; }; var someIndex = function (predicate) { return function (as) { var result = false; (0, pipeable_1.pipe)(as, forEachIndex(function (i, a) { if (predicate(i, a)) { result = true; return false; } return true; })); return result; }; }; return __assign({ someIndex: someIndex, everyIndex: everyIndex, findFirstIndex: findFirstIndex, findLastIndex: findLastIndex, findIndex: findIndex, forEachIndex: forEachIndex, getAt: getAt, existAt: existAt }, options); } exports.initIndexReaderCollection = initIndexReaderCollection; //# sourceMappingURL=IndexReaderCollection.js.map