typed-utilities
Version:
Strongly typed general purpose utilities
33 lines (28 loc) • 1.38 kB
JavaScript
;
var _ = require("..");
test(`Either`, () => {
expect(_.Either.is.left(_.Either.of.left(1))).toEqual(true);
expect(_.Either.is.right(_.Either.of.left(1))).toEqual(false);
expect(() => _.Either.assert.left(_.Either.of.left(1))).not.toThrow();
expect(() => _.Either.assert.right(_.Either.of.left(1))).toThrow();
expect(_.Either.to.leftValue(_.Either.of.left(1))).toEqual(1);
expect(_.Either.is.left(_.Either.of.right(`1`))).toEqual(false);
expect(_.Either.is.right(_.Either.of.right(`1`))).toEqual(true);
expect(() => _.Either.assert.left(_.Either.of.right(`1`))).toThrow();
expect(() => _.Either.assert.right(_.Either.of.right(`1`))).not.toThrow();
expect(_.Either.to.rightValue(_.Either.of.right(`1`))).toEqual(`1`);
const t1 = [1, 2, 3].map(_.Either.of.left);
for (const m of t1) {
expect(_.Either.is.left(m));
expect(() => _.Either.assert.left(m)).not.toThrow();
expect(() => _.Either.assert.right(m)).toThrow();
}
const keepOdd = v => v % 2 === 1 ? _.Either.of.left(v) : _.Either.of.right(`not odd`);
const t2 = t1.map(m => _.Either.match(m, {
left: keepOdd,
right: _.Either.of.right
}));
expect(t2).toEqual([_.Either.of.left(1), _.Either.of.right(`not odd`), _.Either.of.left(3)]);
expect(t2.filter(_.Either.is.left).map(_.Either.to.leftValue)).toEqual([1, 3]);
});
//# sourceMappingURL=Either.test.js.map