macoolka-collection
Version:
`macoolka-collection` Define Data Collection Interface.
70 lines • 3.43 kB
JavaScript
;
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, getAt = _a.getAt, empty = _a.empty, findFirstIndex = _a.findFirstIndex, findLastIndex = _a.findLastIndex, findIndex = _a.findIndex, forEachIndex = _a.forEachIndex, existAt = _a.existAt, _getAt = _a._getAt;
var ListData = from(basic_1.data);
var as = from([1, 2, 3]);
describe('Collection Reader', function () {
it('existAt', function () {
expect(existAt(-1)(as)).toBeFalsy();
expect(existAt(3)(as)).toBeFalsy();
expect(existAt(0)(as)).toBeTruthy();
expect(existAt(2)(as)).toBeTruthy();
});
it('forEachIndex', function () {
var result1 = [];
var keyResult1 = [];
(0, pipeable_1.pipe)(as, forEachIndex(function (index, a) {
result1.push(a);
keyResult1.push(index);
return true;
}));
expect(result1).toEqual([1, 2, 3]);
expect(keyResult1).toEqual([0, 1, 2]);
var result2 = [];
var keyResult2 = [];
(0, pipeable_1.pipe)(as, forEachIndex(function (index, a) {
if (index === 2) {
return false;
}
else {
result2.push(a);
keyResult2.push(index);
return true;
}
}));
expect(result2).toEqual([1, 2]);
expect(keyResult2).toEqual([0, 1]);
});
it('getAt', function () {
(0, pipeable_1.pipe)(ListData, getAt(0), expect(O.some(basic_1.data[0])).toEqual);
(0, pipeable_1.pipe)(ListData, getAt(100), expect(O.none).toEqual);
});
it('_getAt', function () {
(0, pipeable_1.pipe)(ListData, _getAt(0), expect(basic_1.data[0]).toEqual);
(0, pipeable_1.pipe)(ListData, _getAt(100), expect(null || undefined).toEqual);
});
it('findFirstIndex', function () {
(0, pipeable_1.pipe)(ListData, findFirstIndex(function (x) { return x.name === 'john'; }), expect(O.some(0)).toEqual);
(0, pipeable_1.pipe)(ListData, findFirstIndex(function (x) { return x.name === 'john1'; }), expect(O.none).toEqual);
});
it('findIndex', function () {
(0, pipeable_1.pipe)(ListData, findIndex(function (x) { return x.city === 'beijing'; }), expect([0, 1]).toEqual);
(0, pipeable_1.pipe)(ListData, findIndex(function (x) { return x.name === 'john1'; }), expect([]).toEqual);
});
it('findLastIndex', function () {
var xs = from([{ a: 1, b: 0 }, { a: 1, b: 1 }]);
(0, pipeable_1.pipe)(xs, findLastIndex(function (x) { return x.a === 1; }), O.fold(throwError, function (value) { return (0, pipeable_1.pipe)(value, expect(1).toEqual); }));
(0, pipeable_1.pipe)(xs, findLastIndex(function (x) { return x.a === 4; }), expect(O.none).toEqual);
(0, pipeable_1.pipe)(empty(), findLastIndex(function (x) { return x.a === 4; }), expect(O.none).toEqual);
});
});
}
exports.default = test;
//# sourceMappingURL=IndexReader.js.map