UNPKG

macoolka-collection

Version:

`macoolka-collection` Define Data Collection Interface.

102 lines 5.39 kB
"use strict"; 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 }); var basic_1 = require("./fixtures/basic"); var O = require("fp-ts/Option"); var pipeable_1 = require("fp-ts/pipeable"); var throwError = function () { throw new Error('error'); }; function test(_a) { var from = _a.from, size = _a.size, getiterator = _a.getiterator, reverse = _a.reverse, isEmpty = _a.isEmpty, findFirst = _a.findFirst, empty = _a.empty, findFirstMap = _a.findFirstMap, findLast = _a.findLast, findLastMap = _a.findLastMap, first = _a.first, last = _a.last, forEach = _a.forEach; var ListData = from(basic_1.data); var as = from([1, 2, 3]); describe('Collection Reader', function () { it('from and getiterator', function () { (0, pipeable_1.pipe)([], from, getiterator, function (a) { return __spreadArray([], __read(a), false); }, expect([]).toEqual); (0, pipeable_1.pipe)([1, 2, 3], from, getiterator, function (a) { return __spreadArray([], __read(a), false); }, expect([1, 2, 3]).toEqual); }); it('isEmpty', function () { expect(isEmpty(from([]))).toBeTruthy(); expect(isEmpty(ListData)).toBeFalsy(); }); it('forEach', function () { var result1 = []; (0, pipeable_1.pipe)(as, forEach(function (a) { result1.push(a); return true; })); expect(result1).toEqual([1, 2, 3]); var result2 = []; (0, pipeable_1.pipe)(as, forEach(function (a) { if (a === 3) { return false; } else { result2.push(a); return true; } })); expect(result2).toEqual([1, 2]); }); it('size', function () { (0, pipeable_1.pipe)(ListData, size, expect(basic_1.data.length).toEqual); }); it('findFirst', function () { (0, pipeable_1.pipe)(ListData, findFirst(function (x) { return x.name === 'john'; }), expect(O.some(basic_1.data[0])).toEqual); (0, pipeable_1.pipe)(ListData, findFirst(function (x) { return x.name === 'john1'; }), expect(O.none).toEqual); }); it('findFirstMap', function () { (0, pipeable_1.pipe)(ListData, findFirstMap(function (x) { return x.name === 'john' ? O.some('ok') : O.none; }), expect(O.some('ok')).toEqual); (0, pipeable_1.pipe)(ListData, findFirstMap(function (x) { return x.name === 'john1' ? O.some('ok') : O.none; }), expect(O.none).toEqual); }); it('findLast', function () { (0, pipeable_1.pipe)(empty(), findLast(function (x) { return x === 2; }), expect(O.none).toEqual); (0, pipeable_1.pipe)(from([{ a: 1, b: 1 }, { a: 1, b: 2 }]), findLast(function (x) { return x.a === 1; }), O.fold(throwError, function (value) { return (0, pipeable_1.pipe)(value, expect({ a: 1, b: 2 }).toEqual); })); (0, pipeable_1.pipe)(from([{ a: 1, b: 2 }, { a: 2, b: 1 }]), findLast(function (x) { return x.a === 1; }), O.fold(throwError, function (value) { return (0, pipeable_1.pipe)(value, expect({ a: 1, b: 2 }).toEqual); })); (0, pipeable_1.pipe)(from(['a', 1]), findLast(function (x) { return x === 1; }), O.fold(throwError, function (value) { return (0, pipeable_1.pipe)(value, expect(1).toEqual); })); }); it('findLastMap', function () { (0, pipeable_1.pipe)(empty(), findLastMap(function (x) { return x === 2 ? O.some('a') : O.none; }), expect(O.none).toEqual); (0, pipeable_1.pipe)(from([{ a: 1, b: 1 }, { a: 1, b: 2 }]), findLastMap(function (x) { return x.a === 1 ? O.some(x.b) : O.none; }), O.fold(throwError, function (value) { return (0, pipeable_1.pipe)(value, expect(2).toEqual); })); }); it('getFirst', function () { (0, pipeable_1.pipe)(first(as), expect(O.some(1)).toEqual); (0, pipeable_1.pipe)(first(empty()), expect(O.none).toEqual); }); it('getLast', function () { (0, pipeable_1.pipe)(last(as), expect(O.some(3)).toEqual); (0, pipeable_1.pipe)(last(from([])), expect(O.none).toEqual); }); it('reverse', function () { (0, pipeable_1.pipe)(reverse(empty()), expect(empty()).toEqual); (0, pipeable_1.pipe)(reverse(as), expect(from([3, 2, 1])).toEqual); }); }); } exports.default = test; //# sourceMappingURL=Reader.js.map