@perfective/maybe
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -11,2 +11,4 @@ import { Predicate, Present, TypeGuard, Unary } from '../value/value'; | ||
protected constructor(value: T | null | undefined, none: <U>() => Nothing<U> | Nil<U>); | ||
onto<U>(bind: (value: T) => Maybe<U>): Maybe<U>; | ||
to<U>(map: (value: T) => U | null | undefined): Maybe<U>; | ||
that(is: Predicate<T>): Maybe<T>; | ||
@@ -44,3 +46,3 @@ with<V extends T>(is: TypeGuard<T, V>): Maybe<V>; | ||
export declare function nullable<T>(value: T | null): Maybe<T>; | ||
export declare function optional<T>(value?: T): 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>; | ||
@@ -47,0 +49,0 @@ export declare function fallbackTo<T>(fallback: Fallback<T>): T; |
@@ -22,2 +22,14 @@ "use strict"; | ||
} | ||
Maybe.prototype.onto = function (bind) { | ||
if (value_1.isPresent(this.value)) { | ||
return bind(this.value); | ||
} | ||
return this.none(); | ||
}; | ||
Maybe.prototype.to = function (map) { | ||
if (value_1.isPresent(this.value)) { | ||
return maybe(map(this.value)); | ||
} | ||
return this.none(); | ||
}; | ||
Maybe.prototype.that = function (is) { | ||
@@ -24,0 +36,0 @@ if (value_1.isPresent(this.value)) { |
@@ -52,3 +52,3 @@ "use strict"; | ||
it('equals nothing() when value is undefined', function () { | ||
expect(maybe_1.optional()) | ||
expect(maybe_1.optional(undefined)) | ||
.toStrictEqual(maybe_1.nothing()); | ||
@@ -74,2 +74,30 @@ }); | ||
}); | ||
describe('onto', function () { | ||
it('satisfies left identity monad law', function () { | ||
expect(maybe_1.just(3.14).onto(justDecimal)) | ||
.toStrictEqual(justDecimal(3.14)); | ||
}); | ||
it('satisfies right identity monad law', function () { | ||
expect(maybe_1.just(3.14).onto(maybe_1.maybe)) | ||
.toStrictEqual(maybe_1.just(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)); | ||
}); | ||
}); | ||
describe('to', function () { | ||
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))); | ||
}); | ||
it('returns Nothing when the bind function returns undefined', function () { | ||
expect(maybe_1.just(3.14).to(function () { return undefined; })) | ||
.toStrictEqual(maybe_1.nothing()); | ||
}); | ||
it('returns Nil when the bind function returns null', function () { | ||
expect(maybe_1.just(3.14).to(function () { return null; })) | ||
.toStrictEqual(maybe_1.nil()); | ||
}); | ||
}); | ||
describe('then', function () { | ||
@@ -269,2 +297,22 @@ it('satisfies left identity monad law', function () { | ||
}); | ||
describe('onto', function () { | ||
it('satisfies left identity monad law for an undefined value', function () { | ||
expect(maybe_1.nothing().onto(justDecimal)) | ||
.toStrictEqual(maybe_1.nothing()); | ||
}); | ||
it('satisfies right identity monad law', function () { | ||
expect(maybe_1.nothing().onto(maybe_1.maybe)) | ||
.toStrictEqual(maybe_1.nothing()); | ||
}); | ||
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)); | ||
}); | ||
}); | ||
describe('to', function () { | ||
it('skips the mapping function', function () { | ||
expect(maybe_1.nothing().to(decimal)) | ||
.toStrictEqual(maybe_1.nothing()); | ||
}); | ||
}); | ||
describe('then', function () { | ||
@@ -410,2 +458,22 @@ it('satisfies left identity monad law for an undefined value', function () { | ||
}); | ||
describe('onto', function () { | ||
it('satisfies left identity monad law for an undefined value', function () { | ||
expect(maybe_1.nil().onto(justDecimal)) | ||
.toStrictEqual(maybe_1.nil()); | ||
}); | ||
it('satisfies right identity monad law', function () { | ||
expect(maybe_1.nil().onto(maybe_1.maybe)) | ||
.toStrictEqual(maybe_1.nil()); | ||
}); | ||
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)); | ||
}); | ||
}); | ||
describe('to', function () { | ||
it('skips the mapping function', function () { | ||
expect(maybe_1.nil().to(decimal)) | ||
.toStrictEqual(maybe_1.nil()); | ||
}); | ||
}); | ||
describe('then', function () { | ||
@@ -412,0 +480,0 @@ it('satisfies left identity monad law for an undefined value', function () { |
{ | ||
"name": "@perfective/maybe", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Maybe monad-like container", | ||
@@ -5,0 +5,0 @@ "author": "Andrey Mikheychik <a.mikheychik@gmail.com>", |
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
144407
1719