UNPKG

typescript-nullable

Version:

A TypeScript Nullable<T> Type and Monad Compliant Utility Functions

134 lines 6.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ramda_1 = require("ramda"); var _1 = require("."); describe('the Nullable module', function () { var name = 'noob noob'; describe('isNone', function () { it('determines if a particular Nullable is None', function () { expect(_1.Nullable.isNone(null)).toBe(true); expect(_1.Nullable.isNone('hi')).toBe(false); }); it('provides a typeguard', function () { var handleNullable = function (someNullable) { if (!_1.Nullable.isNone(someNullable)) { return someNullable; } return 'value was undefined'; }; expect(handleNullable('oooh wee!')).toBe('oooh wee!'); expect(handleNullable(null)).toBe('value was undefined'); }); }); describe('isSome', function () { it('determines if a particular Nullable is a concrete value', function () { expect(_1.Nullable.isSome(null)).toBe(false); expect(_1.Nullable.isSome('hi')).toBe(true); }); it('provides a typeguard', function () { var handleNullable = function (someNullable) { if (_1.Nullable.isSome(someNullable)) { return someNullable; } return 'value was undefined'; }; expect(handleNullable('oooh wee!')).toBe('oooh wee!'); expect(handleNullable(null)).toBe('value was undefined'); }); }); describe('Nullable.map', function () { describe('when given a None', function () { it('returns a None', function () { var result = _1.Nullable.map(ramda_1.toUpper)(null); expect(result).toEqual(null); }); }); describe('when given a concrete value', function () { it('applies the provided function to the concrete value value', function () { var result = _1.Nullable.map(ramda_1.toUpper, name); expect(result).toEqual(ramda_1.toUpper(name)); }); }); }); describe('Nullable.andThen', function () { var safeDivide = ramda_1.curry(function (a, b) { return a === 0 ? null : b / a; }); describe('when we encounter a None in a composition chain', function () { it('gracefully handles the absence', function () { var result = ramda_1.compose(_1.Nullable.andThen(safeDivide(3)), _1.Nullable.andThen(safeDivide(0)), _1.Nullable.andThen(safeDivide(4)), safeDivide(2))(32); expect(result).toEqual(null); }); }); describe('when a composition chain works out according to plan', function () { it('returns our desired result', function () { var result = ramda_1.compose(_1.Nullable.andThen(safeDivide(3)), _1.Nullable.andThen(safeDivide(5)), _1.Nullable.andThen(safeDivide(4)), safeDivide(2))(32); expect(result).toEqual(32 / 2 / 4 / 5 / 3); }); }); }); describe('Nullable.withDefault', function () { describe('when given a default value and a None', function () { it('returns the default value', function () { var result = _1.Nullable.withDefault(name)(null); expect(result).toBe(name); }); }); describe('when given a default value and a concrete value', function () { it('returns the concrete value value', function () { var result = _1.Nullable.withDefault('foo', name); expect(result).toBe(name); }); }); }); describe('Nullable.ap', function () { describe('when the applicative Nullable is None', function () { it('returns a None', function () { var result = _1.Nullable.ap(name)(null); expect(result).toEqual(null); }); }); describe('when the target Nullable is None', function () { it('returns a None', function () { var result = _1.Nullable.ap(null)(ramda_1.toUpper); expect(result).toEqual(null); }); }); describe('when both the applicative and target Nullables are Justs', function () { it('applies the wrapped function in the applicative Nullable to value in the target Nullable', function () { var result = _1.Nullable.ap(name)(ramda_1.toUpper); expect(result).toEqual(ramda_1.toUpper(name)); }); }); describe('lifting functions', function () { var addThreeNumbers = function (a) { return function (b) { return function (c) { return a + b + c; }; }; }; describe('when everything goes according to plan', function () { it('lifts a "regular" function into Nullable context and returns the correct value', function () { var result = ramda_1.compose(_1.Nullable.ap(3), _1.Nullable.ap(2), _1.Nullable.ap(1))(addThreeNumbers); expect(result).toBe(1 + 2 + 3); }); }); describe('when we exprience an absence', function () { it('lifts a "regular" function into Nullable context and returns null', function () { var result = ramda_1.compose(_1.Nullable.ap(3), _1.Nullable.ap(null), _1.Nullable.ap(1))(addThreeNumbers); expect(result).toBe(null); }); }); }); }); describe('Nullable.maybe', function () { describe('when given a None', function () { it('returns the default value', function () { expect(_1.Nullable.maybe(7, ramda_1.add(83), null)).toBe(7); }); }); describe('when given a concrete value', function () { it('applies the provided function to the concrete value and returns the resulting return value', function () { expect(_1.Nullable.maybe(7, ramda_1.add(83), 34)).toBe(83 + 34); }); }); }); }); //# sourceMappingURL=index.test.js.map