macoolka-collection
Version:
`macoolka-collection` Define Data Collection Interface.
85 lines • 5.22 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);
};
Object.defineProperty(exports, "__esModule", { value: true });
var O = require("fp-ts/Option");
var basic_1 = require("./fixtures/basic");
var pipeable_1 = require("fp-ts/pipeable");
var A = require("fp-ts/Array");
var throwError = function () {
throw new Error('error');
};
function test(_a) {
var from = _a.from, getAt = _a.getAt, empty = _a.empty, toArray = _a.toArray, cons = _a.cons, snoc = _a.snoc, deleteAt = _a.deleteAt, insertAt = _a.insertAt, updateAt = _a.updateAt, setAt = _a.setAt, _setAt = _a._setAt, _deleteAt = _a._deleteAt, _insertAt = _a._insertAt, _getAt = _a._getAt, deleteWith = _a.deleteWith, updateWith = _a.updateWith;
var as = from([1, 2, 3]);
var ListData = from(basic_1.data);
describe('Collection Reader', function () {
it('deleteAt', function () {
(0, pipeable_1.pipe)(ListData, deleteAt(0), O.map(toArray), expect(O.some(basic_1.data.slice(1))).toEqual);
(0, pipeable_1.pipe)(ListData, deleteAt(100), expect(O.none).toEqual);
});
it('_deleteAt', function () {
(0, pipeable_1.pipe)(ListData, _deleteAt(0), toArray, expect(basic_1.data.slice(1)).toEqual);
(0, pipeable_1.pipe)(ListData, _deleteAt(100), expect(ListData).toEqual);
});
it('setAt', function () {
(0, pipeable_1.pipe)(ListData, setAt(0, __assign(__assign({}, basic_1.data[0]), { name: 'test' })), O.fold(throwError, function (a) {
(0, pipeable_1.pipe)(a, getAt(0), O.fold(throwError, function (b) {
expect(b.name).toEqual('test');
}));
}));
(0, pipeable_1.pipe)(ListData, updateAt(10, function (a) { return (__assign(__assign({}, a), { name: 'test' })); }), expect(O.none).toEqual);
});
it('_setAt', function () {
(0, pipeable_1.pipe)(ListData, _setAt(0, __assign(__assign({}, basic_1.data[0]), { name: 'test' })), _getAt(0), function (a) { return expect(a.name).toEqual('test'); });
(0, pipeable_1.pipe)(ListData, _setAt(10, __assign(__assign({}, basic_1.data[0]), { name: 'test' })), expect(ListData).toEqual);
});
it('updateAt', function () {
(0, pipeable_1.pipe)(ListData, updateAt(0, function (a) { return (__assign(__assign({}, a), { name: 'test' })); }), O.fold(throwError, function (a) {
(0, pipeable_1.pipe)(a, getAt(0), O.fold(throwError, function (b) {
expect(b.name).toEqual('test');
}));
}));
(0, pipeable_1.pipe)(ListData, updateAt(10, function (a) { return (__assign(__assign({}, a), { name: 'test' })); }), expect(O.none).toEqual);
});
it('updateWith', function () {
(0, pipeable_1.pipe)(from([1, 2, 3]), updateWith(function (a) { return a === 2; }, function (a) { return a + 1; }), function (a) { return expect(a).toEqual(from([1, 3, 3])); });
(0, pipeable_1.pipe)(ListData, updateWith(function (x) { return x.city === 'beijing'; }, function (a) { return (__assign(__assign({}, a), { name: 'test' })); }), function (a) {
expect(a).toMatchSnapshot();
});
});
it('deleteWith', function () {
(0, pipeable_1.pipe)(from([1, 2, 3]), deleteWith(function (a) { return a === 2 || a === 1; }), function (a) { return expect(a).toEqual(from([3])); });
(0, pipeable_1.pipe)(ListData, deleteWith(function (x) { return x.city === 'beijing'; }), function (a) {
expect(a).toMatchSnapshot();
});
});
it('snoc', function () {
(0, pipeable_1.pipe)(ListData, snoc({ id: 'new', name: 'new' }), toArray, expect(A.snoc(basic_1.data, { id: 'new', name: 'new' })).toEqual);
});
it('cons', function () {
(0, pipeable_1.pipe)(ListData, cons({ id: 'new', name: 'new' }), toArray, expect(A.cons({ id: 'new', name: 'new' }, basic_1.data)).toEqual);
});
it('insertAt', function () {
(0, pipeable_1.pipe)(empty(), insertAt(1, 1), expect(O.none).toEqual);
(0, pipeable_1.pipe)(empty(), insertAt(0, 1), O.fold(throwError, function (value) { return (0, pipeable_1.pipe)(value, toArray, expect([1]).toEqual); }));
(0, pipeable_1.pipe)(as, insertAt(1, 5), O.fold(throwError, function (value) { return (0, pipeable_1.pipe)(value, toArray, expect([1, 5, 2, 3]).toEqual); }));
});
it('insertAt', function () {
(0, pipeable_1.pipe)(empty(), _insertAt(1, 1), expect(empty()).toEqual);
(0, pipeable_1.pipe)(empty(), _insertAt(0, 1), toArray, expect([1]).toEqual);
(0, pipeable_1.pipe)(as, _insertAt(1, 5), toArray, expect([1, 5, 2, 3]).toEqual);
});
});
}
exports.default = test;
//# sourceMappingURL=Writer.js.map