@perfective/maybe
Advanced tools
Comparing version 0.3.4 to 0.4.0
@@ -0,8 +1,4 @@ | ||
export { has, lift, onto, or, otherwise, pick, run, that, to, when, } from './maybe/lift'; | ||
export { Just, Maybe, Nil, Nothing, just, maybe, nil, nothing, nullable, optional, } from './maybe/maybe'; | ||
export { isError, panic, throws, } from './error/error'; | ||
export { absentValueOrPanic, absentValueOrThrow, definedValueOrPanic, definedValueOrThrow, notNullValueOrPanic, notNullValueOrThrow, nullValueOrPanic, nullValueOrThrow, presentValueOrPanic, presentValueOrThrow, undefinedValueOrPanic, undefinedValueOrThrow, } from './error/value'; | ||
export { Callback, Reject, Resolve, Run, promise, result, } from './promise/promise'; | ||
export { WithAbsent, WithDefined, WithNotNull, WithNull, WithPresent, WithUndefined, absentProperty, definedProperty, hasAbsentProperty, hasDefinedProperty, hasNotNullProperty, hasNullProperty, hasPresentProperty, hasUndefinedProperty, notNullProperty, nullProperty, presentProperty, undefinedProperty, } from './value/property'; | ||
export { Absent, Defined, NotNull, Present, isAbsent, isDefined, isNotNull, isNull, isPresent, isUndefined, } from './value/value'; | ||
export { voidable, } from './value/void'; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var lift_1 = require("./maybe/lift"); | ||
Object.defineProperty(exports, "has", { enumerable: true, get: function () { return lift_1.has; } }); | ||
Object.defineProperty(exports, "lift", { enumerable: true, get: function () { return lift_1.lift; } }); | ||
Object.defineProperty(exports, "onto", { enumerable: true, get: function () { return lift_1.onto; } }); | ||
Object.defineProperty(exports, "or", { enumerable: true, get: function () { return lift_1.or; } }); | ||
Object.defineProperty(exports, "otherwise", { enumerable: true, get: function () { return lift_1.otherwise; } }); | ||
Object.defineProperty(exports, "pick", { enumerable: true, get: function () { return lift_1.pick; } }); | ||
Object.defineProperty(exports, "run", { enumerable: true, get: function () { return lift_1.run; } }); | ||
Object.defineProperty(exports, "that", { enumerable: true, get: function () { return lift_1.that; } }); | ||
Object.defineProperty(exports, "to", { enumerable: true, get: function () { return lift_1.to; } }); | ||
Object.defineProperty(exports, "when", { enumerable: true, get: function () { return lift_1.when; } }); | ||
var maybe_1 = require("./maybe/maybe"); | ||
exports.Just = maybe_1.Just; | ||
exports.Maybe = maybe_1.Maybe; | ||
exports.Nil = maybe_1.Nil; | ||
exports.Nothing = maybe_1.Nothing; | ||
exports.just = maybe_1.just; | ||
exports.maybe = maybe_1.maybe; | ||
exports.nil = maybe_1.nil; | ||
exports.nothing = maybe_1.nothing; | ||
exports.nullable = maybe_1.nullable; | ||
exports.optional = maybe_1.optional; | ||
var error_1 = require("./error/error"); | ||
exports.isError = error_1.isError; | ||
exports.panic = error_1.panic; | ||
exports.throws = error_1.throws; | ||
var value_1 = require("./error/value"); | ||
exports.absentValueOrPanic = value_1.absentValueOrPanic; | ||
exports.absentValueOrThrow = value_1.absentValueOrThrow; | ||
exports.definedValueOrPanic = value_1.definedValueOrPanic; | ||
exports.definedValueOrThrow = value_1.definedValueOrThrow; | ||
exports.notNullValueOrPanic = value_1.notNullValueOrPanic; | ||
exports.notNullValueOrThrow = value_1.notNullValueOrThrow; | ||
exports.nullValueOrPanic = value_1.nullValueOrPanic; | ||
exports.nullValueOrThrow = value_1.nullValueOrThrow; | ||
exports.presentValueOrPanic = value_1.presentValueOrPanic; | ||
exports.presentValueOrThrow = value_1.presentValueOrThrow; | ||
exports.undefinedValueOrPanic = value_1.undefinedValueOrPanic; | ||
exports.undefinedValueOrThrow = value_1.undefinedValueOrThrow; | ||
Object.defineProperty(exports, "Just", { enumerable: true, get: function () { return maybe_1.Just; } }); | ||
Object.defineProperty(exports, "Maybe", { enumerable: true, get: function () { return maybe_1.Maybe; } }); | ||
Object.defineProperty(exports, "Nil", { enumerable: true, get: function () { return maybe_1.Nil; } }); | ||
Object.defineProperty(exports, "Nothing", { enumerable: true, get: function () { return maybe_1.Nothing; } }); | ||
Object.defineProperty(exports, "just", { enumerable: true, get: function () { return maybe_1.just; } }); | ||
Object.defineProperty(exports, "maybe", { enumerable: true, get: function () { return maybe_1.maybe; } }); | ||
Object.defineProperty(exports, "nil", { enumerable: true, get: function () { return maybe_1.nil; } }); | ||
Object.defineProperty(exports, "nothing", { enumerable: true, get: function () { return maybe_1.nothing; } }); | ||
Object.defineProperty(exports, "nullable", { enumerable: true, get: function () { return maybe_1.nullable; } }); | ||
Object.defineProperty(exports, "optional", { enumerable: true, get: function () { return maybe_1.optional; } }); | ||
var promise_1 = require("./promise/promise"); | ||
exports.promise = promise_1.promise; | ||
exports.result = promise_1.result; | ||
var property_1 = require("./value/property"); | ||
exports.absentProperty = property_1.absentProperty; | ||
exports.definedProperty = property_1.definedProperty; | ||
exports.hasAbsentProperty = property_1.hasAbsentProperty; | ||
exports.hasDefinedProperty = property_1.hasDefinedProperty; | ||
exports.hasNotNullProperty = property_1.hasNotNullProperty; | ||
exports.hasNullProperty = property_1.hasNullProperty; | ||
exports.hasPresentProperty = property_1.hasPresentProperty; | ||
exports.hasUndefinedProperty = property_1.hasUndefinedProperty; | ||
exports.notNullProperty = property_1.notNullProperty; | ||
exports.nullProperty = property_1.nullProperty; | ||
exports.presentProperty = property_1.presentProperty; | ||
exports.undefinedProperty = property_1.undefinedProperty; | ||
var value_2 = require("./value/value"); | ||
exports.isAbsent = value_2.isAbsent; | ||
exports.isDefined = value_2.isDefined; | ||
exports.isNotNull = value_2.isNotNull; | ||
exports.isNull = value_2.isNull; | ||
exports.isPresent = value_2.isPresent; | ||
exports.isUndefined = value_2.isUndefined; | ||
var void_1 = require("./value/void"); | ||
exports.voidable = void_1.voidable; | ||
Object.defineProperty(exports, "promise", { enumerable: true, get: function () { return promise_1.promise; } }); | ||
Object.defineProperty(exports, "result", { enumerable: true, get: function () { return promise_1.result; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -1,7 +0,4 @@ | ||
import { Predicate, Present, TypeGuard, Unary } from '../value/value'; | ||
export declare type Nullary<T> = () => T; | ||
export declare type Fallback<T> = T | Nullary<T>; | ||
export declare type Condition = boolean | Nullary<boolean>; | ||
export declare type Bind<T, R> = Unary<T, Maybe<R>> | Unary<T, R | null | undefined>; | ||
export declare type ArrayElement<T> = T extends readonly (infer V)[] ? V : undefined; | ||
import { Fallback, Predicate, TypeGuard } from '@perfective/fp'; | ||
import { Present } from '@perfective/value'; | ||
export declare type Condition = Fallback<boolean>; | ||
export declare abstract class Maybe<T> { | ||
@@ -13,7 +10,6 @@ readonly value: T | null | undefined; | ||
to<U>(map: (value: T) => U | null | undefined): Maybe<U>; | ||
that(is: Predicate<T>): Maybe<T>; | ||
with<V extends T>(is: TypeGuard<T, V>): Maybe<V>; | ||
when(outside: Condition): Maybe<T>; | ||
that(filter: Predicate<T>): Maybe<T>; | ||
has<U extends T>(filter: TypeGuard<T, U>): Maybe<U>; | ||
when(condition: Condition): Maybe<T>; | ||
pick<K extends keyof T>(property: K): Maybe<Present<T[K]>>; | ||
index(index: number): Maybe<Present<ArrayElement<T>>>; | ||
otherwise(fallback: Fallback<T>): Just<T>; | ||
@@ -23,3 +19,4 @@ or(fallback: Fallback<T>): T; | ||
or(fallback: undefined): T | undefined; | ||
lift<V>(lift: (value: T | null | undefined) => V): Maybe<V>; | ||
run(procedure: (value: T) => void): Maybe<T>; | ||
lift<U>(map: (value: T | null | undefined) => U | null | undefined): Maybe<U>; | ||
} | ||
@@ -44,5 +41,3 @@ export declare class Just<T> extends Maybe<T> { | ||
export declare function optional<T>(value: T | undefined): Maybe<T>; | ||
export declare function maybeOf<T>(value: Maybe<T> | T | null | undefined): Maybe<T>; | ||
export declare function fallbackTo<T>(fallback: Fallback<T>): T; | ||
export declare function holds(condition: Condition): boolean; | ||
export declare function isTrue(condition: Condition): boolean; | ||
//# sourceMappingURL=maybe.d.ts.map |
@@ -16,3 +16,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var value_1 = require("../value/value"); | ||
exports.isTrue = exports.optional = exports.nullable = exports.maybe = exports.nil = exports.Nil = exports.nothing = exports.Nothing = exports.just = exports.Just = exports.Maybe = void 0; | ||
var fp_1 = require("@perfective/fp"); | ||
var value_1 = require("@perfective/value"); | ||
var Maybe = (function () { | ||
@@ -35,5 +37,5 @@ function Maybe(value, none) { | ||
}; | ||
Maybe.prototype.that = function (is) { | ||
Maybe.prototype.that = function (filter) { | ||
if (value_1.isPresent(this.value)) { | ||
if (is(this.value)) { | ||
if (filter(this.value)) { | ||
return this; | ||
@@ -45,5 +47,5 @@ } | ||
}; | ||
Maybe.prototype.with = function (is) { | ||
Maybe.prototype.has = function (filter) { | ||
if (value_1.isPresent(this.value)) { | ||
if (is(this.value)) { | ||
if (filter(this.value)) { | ||
return this; | ||
@@ -55,4 +57,4 @@ } | ||
}; | ||
Maybe.prototype.when = function (outside) { | ||
if (value_1.isPresent(this.value) && holds(outside)) { | ||
Maybe.prototype.when = function (condition) { | ||
if (value_1.isPresent(this.value) && isTrue(condition)) { | ||
return this; | ||
@@ -65,8 +67,2 @@ } | ||
}; | ||
Maybe.prototype.index = function (index) { | ||
if (Array.isArray(this.value)) { | ||
return maybe(this.value[index]); | ||
} | ||
return this.none(); | ||
}; | ||
Maybe.prototype.otherwise = function (fallback) { | ||
@@ -76,3 +72,3 @@ if (value_1.isPresent(this.value)) { | ||
} | ||
return just(fallbackTo(fallback)); | ||
return just(fp_1.fallbackTo(fallback)); | ||
}; | ||
@@ -84,9 +80,15 @@ Maybe.prototype.or = function (fallback) { | ||
if (value_1.isPresent(fallback)) { | ||
return fallbackTo(fallback); | ||
return fp_1.fallbackTo(fallback); | ||
} | ||
return fallback; | ||
}; | ||
Maybe.prototype.lift = function (lift) { | ||
return maybe(lift(this.value)); | ||
Maybe.prototype.run = function (procedure) { | ||
if (value_1.isPresent(this.value)) { | ||
procedure(this.value); | ||
} | ||
return this; | ||
}; | ||
Maybe.prototype.lift = function (map) { | ||
return maybe(map(this.value)); | ||
}; | ||
return Maybe; | ||
@@ -163,17 +165,3 @@ }()); | ||
exports.optional = optional; | ||
function maybeOf(value) { | ||
if (value instanceof Maybe) { | ||
return value; | ||
} | ||
return maybe(value); | ||
} | ||
exports.maybeOf = maybeOf; | ||
function fallbackTo(fallback) { | ||
if (fallback instanceof Function) { | ||
return fallback(); | ||
} | ||
return fallback; | ||
} | ||
exports.fallbackTo = fallbackTo; | ||
function holds(condition) { | ||
function isTrue(condition) { | ||
if (condition instanceof Function) { | ||
@@ -184,3 +172,3 @@ return condition(); | ||
} | ||
exports.holds = holds; | ||
exports.isTrue = isTrue; | ||
//# sourceMappingURL=maybe.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var error_1 = require("../error/error"); | ||
var property_1 = require("../value/property"); | ||
var value_1 = require("../value/value"); | ||
var error_1 = require("@perfective/error"); | ||
var fp_1 = require("@perfective/fp"); | ||
var object_1 = require("@perfective/object"); | ||
var real_1 = require("@perfective/real"); | ||
var value_1 = require("@perfective/value"); | ||
var maybe_1 = require("./maybe"); | ||
function isGreaterThan(value) { | ||
return function (input) { return input > value; }; | ||
} | ||
function isLessThan(value) { | ||
return function (input) { return input < value; }; | ||
} | ||
function decimal(value) { | ||
return value.toString(10); | ||
} | ||
function justDecimal(value) { | ||
return maybe_1.just(value).to(decimal); | ||
return maybe_1.just(value).to(real_1.decimal); | ||
} | ||
function split(value) { | ||
function justSplit(value) { | ||
return maybe_1.just(value.split('.')); | ||
} | ||
function constant(value) { | ||
return function () { return value; }; | ||
} | ||
var check = { | ||
@@ -83,4 +73,4 @@ required: 3.14, | ||
it('satisfies associativity monad law', function () { | ||
expect(maybe_1.just(3.14).onto(function (x) { return justDecimal(x).onto(split); })) | ||
.toStrictEqual(maybe_1.just(3.14).onto(justDecimal).onto(split)); | ||
expect(maybe_1.just(3.14).onto(function (x) { return justDecimal(x).onto(justSplit); })) | ||
.toStrictEqual(maybe_1.just(3.14).onto(justDecimal).onto(justSplit)); | ||
}); | ||
@@ -90,4 +80,4 @@ }); | ||
it('returns Just next value when the bind function returns a present value', function () { | ||
expect(maybe_1.just(3.14).to(decimal)) | ||
.toStrictEqual(maybe_1.just(decimal(3.14))); | ||
expect(maybe_1.just(3.14).to(real_1.decimal)) | ||
.toStrictEqual(maybe_1.just(real_1.decimal(3.14))); | ||
}); | ||
@@ -105,37 +95,37 @@ it('returns Nothing when the bind function returns undefined', function () { | ||
it('keeps the value when condition holds', function () { | ||
expect(maybe_1.just(3.14).that(isGreaterThan(2.71))) | ||
expect(maybe_1.just(3.14).that(real_1.isGreaterThan(2.71))) | ||
.toStrictEqual(maybe_1.just(3.14)); | ||
}); | ||
it('filters out the value when condition fails', function () { | ||
expect(maybe_1.just(3.14).that(isLessThan(2.71))) | ||
expect(maybe_1.just(3.14).that(real_1.isLessThan(2.71))) | ||
.toStrictEqual(maybe_1.nothing()); | ||
}); | ||
}); | ||
describe('with', function () { | ||
describe('has', function () { | ||
it('returns just() when value passes the type guard', function () { | ||
expect(maybe_1.just(check).with(property_1.definedProperty('required'))) | ||
expect(maybe_1.just(check).has(object_1.definedProperty('required'))) | ||
.toStrictEqual(maybe_1.just(check)); | ||
expect(maybe_1.just(check).with(property_1.undefinedProperty('optional'))) | ||
expect(maybe_1.just(check).has(object_1.undefinedProperty('optional'))) | ||
.toStrictEqual(maybe_1.just(check)); | ||
expect(maybe_1.just(check).with(property_1.notNullProperty('nullable'))) | ||
expect(maybe_1.just(check).has(object_1.notNullProperty('nullable'))) | ||
.toStrictEqual(maybe_1.just(check)); | ||
expect(maybe_1.just(check).with(property_1.nullProperty('possible'))) | ||
expect(maybe_1.just(check).has(object_1.nullProperty('possible'))) | ||
.toStrictEqual(maybe_1.just(check)); | ||
expect(maybe_1.just(check).with(property_1.presentProperty('maybe'))) | ||
expect(maybe_1.just(check).has(object_1.presentProperty('maybe'))) | ||
.toStrictEqual(maybe_1.just(check)); | ||
expect(maybe_1.just(check).with(property_1.absentProperty('option'))) | ||
expect(maybe_1.just(check).has(object_1.absentProperty('option'))) | ||
.toStrictEqual(maybe_1.just(check)); | ||
}); | ||
it('returns nothing() when value fails the type guard', function () { | ||
expect(maybe_1.just(check).with(property_1.undefinedProperty('required'))) | ||
expect(maybe_1.just(check).has(object_1.undefinedProperty('required'))) | ||
.toStrictEqual(maybe_1.nothing()); | ||
expect(maybe_1.just(check).with(property_1.definedProperty('optional'))) | ||
expect(maybe_1.just(check).has(object_1.definedProperty('optional'))) | ||
.toStrictEqual(maybe_1.nothing()); | ||
expect(maybe_1.just(check).with(property_1.nullProperty('nullable'))) | ||
expect(maybe_1.just(check).has(object_1.nullProperty('nullable'))) | ||
.toStrictEqual(maybe_1.nothing()); | ||
expect(maybe_1.just(check).with(property_1.notNullProperty('possible'))) | ||
expect(maybe_1.just(check).has(object_1.notNullProperty('possible'))) | ||
.toStrictEqual(maybe_1.nothing()); | ||
expect(maybe_1.just(check).with(property_1.absentProperty('maybe'))) | ||
expect(maybe_1.just(check).has(object_1.absentProperty('maybe'))) | ||
.toStrictEqual(maybe_1.nothing()); | ||
expect(maybe_1.just(check).with(property_1.presentProperty('option'))) | ||
expect(maybe_1.just(check).has(object_1.presentProperty('option'))) | ||
.toStrictEqual(maybe_1.nothing()); | ||
@@ -145,7 +135,7 @@ }); | ||
expect(maybe_1.just(check) | ||
.with(property_1.definedProperty('required')) | ||
.with(property_1.definedProperty('option')) | ||
.to(function (v) { return v.required.toString(10) + ":" + v.option.toString(10); })).toStrictEqual(maybe_1.just(check) | ||
.with(property_1.definedProperty('required', 'option')) | ||
.to(function (v) { return v.required.toString(10) + ":" + v.option.toString(10); })); | ||
.has(object_1.definedProperty('required')) | ||
.has(object_1.definedProperty('option')) | ||
.to(function (v) { return real_1.decimal(v.required) + ":" + real_1.decimal(v.option); })).toStrictEqual(maybe_1.just(check) | ||
.has(object_1.definedProperty('required', 'option')) | ||
.to(function (v) { return real_1.decimal(v.required) + ":" + real_1.decimal(v.option); })); | ||
}); | ||
@@ -185,3 +175,3 @@ }); | ||
.pick('value') | ||
.to(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.just(boxed) | ||
.to(real_1.decimal)).toStrictEqual(maybe_1.just(boxed) | ||
.to(function (b) { return b.value; }) | ||
@@ -192,26 +182,2 @@ .to(function (v) { return v.value; }) | ||
}); | ||
describe('index', function () { | ||
it('returns an existing element from an array', function () { | ||
expect(maybe_1.just([2.71, 3.14]).index(0)) | ||
.toStrictEqual(maybe_1.just(2.71)); | ||
expect(maybe_1.just([2.71, 3.14]).index(1)) | ||
.toStrictEqual(maybe_1.just(3.14)); | ||
}); | ||
it('returns nothing for a missing elements from an array', function () { | ||
expect(maybe_1.just([2.71, 3.14]).index(2)) | ||
.toStrictEqual(maybe_1.nothing()); | ||
expect(maybe_1.just([2.71, 3.14]).index(-1)) | ||
.toStrictEqual(maybe_1.nothing()); | ||
}); | ||
it('is an equivalent of the then() chain', function () { | ||
var matrix = [[undefined, 3.14], [2.71, null]]; | ||
expect(maybe_1.just(matrix) | ||
.index(0) | ||
.index(1) | ||
.to(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.just(matrix) | ||
.to(function (m) { return m[0]; }) | ||
.to(function (m) { return m[1]; }) | ||
.to(function (v) { return v.toString(10); })); | ||
}); | ||
}); | ||
describe('otherwise', function () { | ||
@@ -223,3 +189,3 @@ it('returns the Maybe value when fallback is constant', function () { | ||
it('returns the Maybe value when fallback is a callback', function () { | ||
expect(maybe_1.just(3.14).otherwise(constant(2.71))) | ||
expect(maybe_1.just(3.14).otherwise(fp_1.constant(2.71))) | ||
.toStrictEqual(maybe_1.just(3.14)); | ||
@@ -238,3 +204,3 @@ }); | ||
it('returns the Maybe value when fallback is a callback', function () { | ||
expect(maybe_1.just(3.14).or(constant(2.71))) | ||
expect(maybe_1.just(3.14).or(fp_1.constant(2.71))) | ||
.toStrictEqual(3.14); | ||
@@ -259,2 +225,12 @@ }); | ||
}); | ||
describe('run', function () { | ||
var a = 2.71; | ||
it('keeps the present value the same', function () { | ||
expect(maybe_1.just(3.14).run(function (pi) { return a = pi; })) | ||
.toStrictEqual(maybe_1.just(3.14)); | ||
}); | ||
it('runs a procedure on the present value', function () { | ||
expect(a).toStrictEqual(3.14); | ||
}); | ||
}); | ||
describe('lift', function () { | ||
@@ -284,4 +260,4 @@ it('applies the function to the plain monadic value', function () { | ||
it('satisfies associativity monad law', function () { | ||
expect(maybe_1.nothing().onto(function (x) { return justDecimal(x).onto(split); })) | ||
.toStrictEqual(maybe_1.nothing().onto(justDecimal).onto(split)); | ||
expect(maybe_1.nothing().onto(function (x) { return justDecimal(x).onto(justSplit); })) | ||
.toStrictEqual(maybe_1.nothing().onto(justDecimal).onto(justSplit)); | ||
}); | ||
@@ -291,3 +267,3 @@ }); | ||
it('skips the mapping function', function () { | ||
expect(maybe_1.nothing().to(decimal)) | ||
expect(maybe_1.nothing().to(real_1.decimal)) | ||
.toStrictEqual(maybe_1.nothing()); | ||
@@ -306,5 +282,5 @@ }); | ||
}); | ||
describe('with', function () { | ||
describe('has', function () { | ||
it('remains nothing() after value is checked by the type guard', function () { | ||
expect(maybe_1.nothing().with(property_1.presentProperty('maybe'))) | ||
expect(maybe_1.nothing().has(object_1.presentProperty('maybe'))) | ||
.toStrictEqual(maybe_1.nothing()); | ||
@@ -332,23 +308,8 @@ }); | ||
.pick('value') | ||
.to(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.nothing() | ||
.to(real_1.decimal)).toStrictEqual(maybe_1.nothing() | ||
.to(function (b) { return b.value; }) | ||
.to(function (v) { return v.value; }) | ||
.to(function (v) { return v.toString(10); })); | ||
.to(real_1.decimal)); | ||
}); | ||
}); | ||
describe('index', function () { | ||
it('returns nothing for a missing elements from an array', function () { | ||
expect(maybe_1.nothing().index(0)) | ||
.toStrictEqual(maybe_1.nothing()); | ||
}); | ||
it('is an equivalent of the then() chain', function () { | ||
expect(maybe_1.nothing() | ||
.index(0) | ||
.index(1) | ||
.to(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.nothing() | ||
.to(function (m) { return m[0]; }) | ||
.to(function (m) { return m[1]; }) | ||
.to(function (v) { return v.toString(10); })); | ||
}); | ||
}); | ||
describe('otherwise', function () { | ||
@@ -360,3 +321,3 @@ it('returns a fallback value when fallback is constant', function () { | ||
it('returns a result of the fallback callback', function () { | ||
expect(maybe_1.nothing().otherwise(constant(2.71))) | ||
expect(maybe_1.nothing().otherwise(fp_1.constant(2.71))) | ||
.toStrictEqual(maybe_1.just(2.71)); | ||
@@ -375,3 +336,3 @@ }); | ||
it('returns a result of the fallback callback', function () { | ||
expect(maybe_1.nothing().or(constant(2.71))) | ||
expect(maybe_1.nothing().or(fp_1.constant(2.71))) | ||
.toStrictEqual(2.71); | ||
@@ -396,2 +357,12 @@ }); | ||
}); | ||
describe('run', function () { | ||
var a = 2.71; | ||
it('keeps nothing the same', function () { | ||
expect(maybe_1.nothing().run(function (none) { return a = none; })) | ||
.toStrictEqual(maybe_1.nothing()); | ||
}); | ||
it('skips running the procedure on nothing', function () { | ||
expect(a).toStrictEqual(2.71); | ||
}); | ||
}); | ||
describe('lift', function () { | ||
@@ -421,4 +392,4 @@ it('applies the function to the plain monadic value', function () { | ||
it('satisfies associativity monad law', function () { | ||
expect(maybe_1.nil().onto(function (x) { return justDecimal(x).onto(split); })) | ||
.toStrictEqual(maybe_1.nil().onto(justDecimal).onto(split)); | ||
expect(maybe_1.nil().onto(function (x) { return justDecimal(x).onto(justSplit); })) | ||
.toStrictEqual(maybe_1.nil().onto(justDecimal).onto(justSplit)); | ||
}); | ||
@@ -428,3 +399,3 @@ }); | ||
it('skips the mapping function', function () { | ||
expect(maybe_1.nil().to(decimal)) | ||
expect(maybe_1.nil().to(real_1.decimal)) | ||
.toStrictEqual(maybe_1.nil()); | ||
@@ -443,5 +414,5 @@ }); | ||
}); | ||
describe('with', function () { | ||
describe('has', function () { | ||
it('remains nothing() after value is checked by the type guard', function () { | ||
expect(maybe_1.nil().with(property_1.presentProperty('maybe'))) | ||
expect(maybe_1.nil().has(object_1.presentProperty('maybe'))) | ||
.toStrictEqual(maybe_1.nil()); | ||
@@ -469,23 +440,8 @@ }); | ||
.pick('value') | ||
.to(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.nil() | ||
.to(real_1.decimal)).toStrictEqual(maybe_1.nil() | ||
.to(function (b) { return b.value; }) | ||
.to(function (v) { return v.value; }) | ||
.to(function (v) { return v.toString(10); })); | ||
.to(real_1.decimal)); | ||
}); | ||
}); | ||
describe('index', function () { | ||
it('returns nothing for a missing elements from an array', function () { | ||
expect(maybe_1.nil().index(0)) | ||
.toStrictEqual(maybe_1.nil()); | ||
}); | ||
it('is an equivalent of the then() chain', function () { | ||
expect(maybe_1.nil() | ||
.index(0) | ||
.index(1) | ||
.to(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.nil() | ||
.to(function (m) { return m[0]; }) | ||
.to(function (m) { return m[1]; }) | ||
.to(function (v) { return v.toString(10); })); | ||
}); | ||
}); | ||
describe('otherwise', function () { | ||
@@ -497,3 +453,3 @@ it('returns a fallback value when fallback is a constant', function () { | ||
it('returns a result of the fallback callback', function () { | ||
expect(maybe_1.nil().otherwise(constant(2.71))) | ||
expect(maybe_1.nil().otherwise(fp_1.constant(2.71))) | ||
.toStrictEqual(maybe_1.just(2.71)); | ||
@@ -512,3 +468,3 @@ }); | ||
it('returns a result of the fallback callback', function () { | ||
expect(maybe_1.nil().or(constant(2.71))) | ||
expect(maybe_1.nil().or(fp_1.constant(2.71))) | ||
.toStrictEqual(2.71); | ||
@@ -533,2 +489,12 @@ }); | ||
}); | ||
describe('run', function () { | ||
var a = 2.71; | ||
it('keeps nil the same', function () { | ||
expect(maybe_1.nil().run(function (none) { return a = none; })) | ||
.toStrictEqual(maybe_1.nil()); | ||
}); | ||
it('skips running the procedure on nil', function () { | ||
expect(a).toStrictEqual(2.71); | ||
}); | ||
}); | ||
describe('lift', function () { | ||
@@ -541,22 +507,2 @@ it('applies the function to the plain monadic value', function () { | ||
}); | ||
describe('maybeOf', function () { | ||
it('returns Maybe<T> when value is not Maybe', function () { | ||
expect(maybe_1.maybeOf(3.14)) | ||
.toStrictEqual(maybe_1.maybe(3.14)); | ||
}); | ||
it('returns Maybe<T> when value is Maybe', function () { | ||
expect(maybe_1.maybeOf(maybe_1.maybe(3.14))) | ||
.toStrictEqual(maybe_1.maybe(3.14)); | ||
}); | ||
}); | ||
describe('fallbackTo', function () { | ||
it('returns constant when value is constant', function () { | ||
expect(maybe_1.fallbackTo(3.14)) | ||
.toStrictEqual(3.14); | ||
}); | ||
it('returns result of the fallback callback', function () { | ||
expect(maybe_1.fallbackTo(constant(3.14))) | ||
.toStrictEqual(3.14); | ||
}); | ||
}); | ||
//# sourceMappingURL=maybe.spec.js.map |
@@ -39,2 +39,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.result = exports.promise = void 0; | ||
var maybe_1 = require("../maybe/maybe"); | ||
@@ -41,0 +42,0 @@ function promise(run) { |
{ | ||
"name": "@perfective/maybe", | ||
"version": "0.3.4", | ||
"version": "0.4.0", | ||
"description": "Maybe monad-like container", | ||
@@ -15,10 +15,17 @@ "author": "Andrey Mikheychik <a.mikheychik@gmail.com>", | ||
"scripts": { | ||
"build": "tsc", | ||
"build": "tsc --project ./tsconfig.build.json", | ||
"build:clean": "npm run clean && npm run build", | ||
"clean": "rm -rf dist && rm -f tsconfig.tsbuildinfo && jest --clearCache", | ||
"clean": "rm -rf dist && rm -f *.tsbuildinfo && jest --clearCache", | ||
"lint": "npm run lint:eslint; npm run lint:tslint", | ||
"lint:eslint": "eslint --fix --ext .ts ./src", | ||
"lint:tslint": "tslint --fix --project ./tsconfig.json", | ||
"lint:tslint": "tslint --fix --project ./tsconfig.lint.json", | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"@perfective/error": "^0.1.0", | ||
"@perfective/fp": "^0.1.3", | ||
"@perfective/object": "^0.1.0", | ||
"@perfective/real": "^0.2.0", | ||
"@perfective/value": "^0.1.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
92659
5
33
1045
+ Added@perfective/error@^0.1.0
+ Added@perfective/fp@^0.1.3
+ Added@perfective/object@^0.1.0
+ Added@perfective/real@^0.2.0
+ Added@perfective/value@^0.1.0
+ Added@perfective/error@0.1.1(transitive)
+ Added@perfective/fp@0.1.3(transitive)
+ Added@perfective/object@0.1.0(transitive)
+ Added@perfective/real@0.2.1(transitive)
+ Added@perfective/value@0.1.0(transitive)