macoolka-collection
Version:
`macoolka-collection` Define Data Collection Interface.
229 lines • 8.05 kB
JavaScript
;
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.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initReaderCollection = void 0;
var pipeable_1 = require("fp-ts/pipeable");
var function_1 = require("fp-ts/function");
var O = require("fp-ts/Option");
var macoolka_object_1 = require("macoolka-object");
function initReaderCollection(options) {
var size = options.size, getiterator = options.getiterator, reverse = options.reverse;
/**
* True if a `Collection` size equal zero
*
* @example
* import { isEmpty } from 'fp-ts/Array'
*
* assert.deepStrictEqual(isEmpty([1, 2, 3]), false)
* assert.deepStrictEqual(isEmpty([1, 2, 3]), true)
*
* @since 0.5.0
*/
function isEmpty(as) {
return size(as) === 0;
}
/**
* False if a `Collection` size equal zero
*
* @example
* import { notEmpty } from 'fp-ts/Array'
*
* assert.deepStrictEqual(notEmpty([1, 2, 3]), false)
* assert.deepStrictEqual(notEmpty([1, 2, 3]), true)
*
* @since 0.5.0
*/
function notEmpty(as) {
return (0, pipeable_1.pipe)(as, (0, function_1.not)(isEmpty));
}
function first(as) {
return (0, pipeable_1.pipe)(getiterator(as), function (as) { return as[Symbol.iterator]().next(); }, function (result) { return O.fromNullable((0, macoolka_object_1.cloneDeep)(result.value)); });
}
/**
* Get the last element in an list, or `None` if the list is empty
*
* @example
* import { getLast } from 'fp-ts/List'
* import { some, none } from 'fp-ts/Option'
*
* assert.deepStrictEqual(getLast([1, 2, 3]), some(3))
* assert.deepStrictEqual(getLast([]), none)
*
* @since 0.5.0
*/
function last(as) {
return (0, pipeable_1.pipe)(as, reverse, getiterator, function (as) { return as[Symbol.iterator]().next(); }, function (result) { return O.fromNullable((0, macoolka_object_1.cloneDeep)(result.value)); });
}
var forEach = 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(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; }
}
}; };
function findFirst(predicate) {
return function (as) {
var result = O.none;
(0, pipeable_1.pipe)(as, forEach(function (value) {
if (predicate(value)) {
result = O.some(value);
return false;
}
return true;
}));
return result;
};
}
/**
* Find the first element returned by an option based selector function
*
* @example
* import { findFirstMap } from 'fp-ts/List'
* import { some, none } from 'fp-ts/Option'
*
* interface Person {
* name: string
* age?: number
* }
*
* const persons: List<Person> = [{ name: 'John' }, { name: 'Mary', age: 45 }, { name: 'Joey', age: 28 }]
*
* // returns the name of the first person that has an age
* assert.deepStrictEqual(findFirstMap((p: Person) => (p.age === undefined ? none : some(p.name)))(persons), some('Mary'))
*
* @since 0.5.0
*/
function findFirstMap(f) {
return function (as) {
var result = O.none;
(0, pipeable_1.pipe)(as, findFirst(function (a) {
result = f(a);
return O.isSome(result);
}));
return result;
};
}
function findLast(predicate) {
return function (as) {
return (0, pipeable_1.pipe)(as, reverse, findFirst(predicate));
};
}
/**
* Find the last element returned by an option based selector function
*
* @example
* import { findLastMap } from 'fp-ts/List'
* import { some, none } from 'fp-ts/Option'
*
* interface Person {
* name: string
* age?: number
* }
*
* const persons: List<Person> = [{ name: 'John' }, { name: 'Mary', age: 45 }, { name: 'Joey', age: 28 }]
*
* // returns the name of the last person that has an age
* assert.deepStrictEqual(findLastMap((p: Person) => (p.age === undefined ? none : some(p.name)))(persons), some('Joey'))
*
* @since 0.5.0
*/
function findLastMap(f) {
return function (as) {
return (0, pipeable_1.pipe)(as, reverse, findFirstMap(f));
};
}
function toArray(as) {
return __spreadArray([], __read(getiterator(as)), false);
}
var every = function (predicate) { return function (as) {
var result = true;
(0, pipeable_1.pipe)(as, forEach(function (a) {
if (!predicate(a)) {
result = false;
return false;
}
return true;
}));
return result;
}; };
var some = function (predicate) { return function (as) {
var result = false;
(0, pipeable_1.pipe)(as, forEach(function (i) {
if (predicate(i)) {
result = true;
return false;
}
return true;
}));
return result;
}; };
var exist = function (predicate) { return function (as) {
var result = false;
(0, pipeable_1.pipe)(as, findFirst(predicate), O.isSome);
return result;
}; };
return __assign({ isEmpty: isEmpty, notEmpty: notEmpty, findFirst: findFirst, findFirstMap: findFirstMap, findLast: findLast, findLastMap: findLastMap, first: first, last: last, forEach: forEach, exist: exist, toArray: toArray, every: every, some: some }, options);
}
exports.initReaderCollection = initReaderCollection;
//# sourceMappingURL=ReaderCollection.js.map