@perfective/maybe
Advanced tools
Comparing version 0.3.3 to 0.3.4
@@ -16,4 +16,2 @@ import { Predicate, Present, TypeGuard, Unary } from '../value/value'; | ||
when(outside: Condition): Maybe<T>; | ||
then<U>(next: (value: T) => Maybe<U>): Maybe<U>; | ||
then<U>(next: (value: T) => U | null | undefined): Maybe<U>; | ||
pick<K extends keyof T>(property: K): Maybe<Present<T[K]>>; | ||
@@ -20,0 +18,0 @@ index(index: number): Maybe<Present<ArrayElement<T>>>; |
@@ -58,10 +58,4 @@ "use strict"; | ||
}; | ||
Maybe.prototype.then = function (next) { | ||
if (value_1.isAbsent(this.value)) { | ||
return this.none(); | ||
} | ||
return maybeOf(next(this.value)); | ||
}; | ||
Maybe.prototype.pick = function (property) { | ||
return this.then(function (v) { return v[property]; }); | ||
return this.to(function (v) { return v[property]; }); | ||
}; | ||
@@ -68,0 +62,0 @@ Maybe.prototype.index = function (index) { |
@@ -17,3 +17,3 @@ "use strict"; | ||
function justDecimal(value) { | ||
return maybe_1.just(value).then(decimal); | ||
return maybe_1.just(value).to(decimal); | ||
} | ||
@@ -102,20 +102,2 @@ function split(value) { | ||
}); | ||
describe('then', function () { | ||
it('satisfies left identity monad law', function () { | ||
expect(maybe_1.just(3.14).then(justDecimal)) | ||
.toStrictEqual(justDecimal(3.14)); | ||
}); | ||
it('satisfies right identity monad law', function () { | ||
expect(maybe_1.just(3.14).then(maybe_1.maybe)) | ||
.toStrictEqual(maybe_1.just(3.14)); | ||
}); | ||
it('satisfies associativity monad law', function () { | ||
expect(maybe_1.just(3.14).then(function (x) { return justDecimal(x).then(split); })) | ||
.toStrictEqual(maybe_1.just(3.14).then(justDecimal).then(split)); | ||
}); | ||
it('wraps non-Maybe values into Maybe', function () { | ||
expect(maybe_1.just(3.14).then(decimal)) | ||
.toStrictEqual(maybe_1.just('3.14')); | ||
}); | ||
}); | ||
describe('that', function () { | ||
@@ -164,5 +146,5 @@ it('keeps the value when condition holds', function () { | ||
.with(property_1.definedProperty('option')) | ||
.then(function (v) { return v.required.toString(10) + ":" + v.option.toString(10); })).toStrictEqual(maybe_1.just(check) | ||
.to(function (v) { return v.required.toString(10) + ":" + v.option.toString(10); })).toStrictEqual(maybe_1.just(check) | ||
.with(property_1.definedProperty('required', 'option')) | ||
.then(function (v) { return v.required.toString(10) + ":" + v.option.toString(10); })); | ||
.to(function (v) { return v.required.toString(10) + ":" + v.option.toString(10); })); | ||
}); | ||
@@ -202,6 +184,6 @@ }); | ||
.pick('value') | ||
.then(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.just(boxed) | ||
.then(function (b) { return b.value; }) | ||
.then(function (v) { return v.value; }) | ||
.then(function (v) { return v.toString(10); })); | ||
.to(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.just(boxed) | ||
.to(function (b) { return b.value; }) | ||
.to(function (v) { return v.value; }) | ||
.to(function (v) { return v.toString(10); })); | ||
}); | ||
@@ -227,6 +209,6 @@ }); | ||
.index(1) | ||
.then(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.just(matrix) | ||
.then(function (m) { return m[0]; }) | ||
.then(function (m) { return m[1]; }) | ||
.then(function (v) { return v.toString(10); })); | ||
.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); })); | ||
}); | ||
@@ -308,20 +290,2 @@ }); | ||
}); | ||
describe('then', function () { | ||
it('satisfies left identity monad law for an undefined value', function () { | ||
expect(maybe_1.nothing().then(justDecimal)) | ||
.toStrictEqual(maybe_1.nothing()); | ||
}); | ||
it('satisfies right identity monad law', function () { | ||
expect(maybe_1.nothing().then(maybe_1.maybe)) | ||
.toStrictEqual(maybe_1.nothing()); | ||
}); | ||
it('satisfies associativity monad law', function () { | ||
expect(maybe_1.nothing().then(function (x) { return justDecimal(x).then(split); })) | ||
.toStrictEqual(maybe_1.nothing().then(justDecimal).then(split)); | ||
}); | ||
it('wraps non-Maybe values into Maybe', function () { | ||
expect(maybe_1.nothing().then(decimal).value) | ||
.toBeUndefined(); | ||
}); | ||
}); | ||
describe('that', function () { | ||
@@ -362,6 +326,6 @@ it('remains nothing when condition holds', function () { | ||
.pick('value') | ||
.then(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.nothing() | ||
.then(function (b) { return b.value; }) | ||
.then(function (v) { return v.value; }) | ||
.then(function (v) { return v.toString(10); })); | ||
.to(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.nothing() | ||
.to(function (b) { return b.value; }) | ||
.to(function (v) { return v.value; }) | ||
.to(function (v) { return v.toString(10); })); | ||
}); | ||
@@ -378,6 +342,6 @@ }); | ||
.index(1) | ||
.then(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.nothing() | ||
.then(function (m) { return m[0]; }) | ||
.then(function (m) { return m[1]; }) | ||
.then(function (v) { return v.toString(10); })); | ||
.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); })); | ||
}); | ||
@@ -459,20 +423,2 @@ }); | ||
}); | ||
describe('then', function () { | ||
it('satisfies left identity monad law for an undefined value', function () { | ||
expect(maybe_1.nil().then(justDecimal)) | ||
.toStrictEqual(maybe_1.nil()); | ||
}); | ||
it('satisfies right identity monad law', function () { | ||
expect(maybe_1.nil().then(maybe_1.maybe)) | ||
.toStrictEqual(maybe_1.nil()); | ||
}); | ||
it('satisfies associativity monad law', function () { | ||
expect(maybe_1.nil().then(function (x) { return justDecimal(x).then(split); })) | ||
.toStrictEqual(maybe_1.nil().then(justDecimal).then(split)); | ||
}); | ||
it('wraps non-Maybe values into Maybe', function () { | ||
expect(maybe_1.nil().then(decimal)) | ||
.toStrictEqual(maybe_1.nil()); | ||
}); | ||
}); | ||
describe('that', function () { | ||
@@ -513,6 +459,6 @@ it('remains nil when condition holds', function () { | ||
.pick('value') | ||
.then(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.nil() | ||
.then(function (b) { return b.value; }) | ||
.then(function (v) { return v.value; }) | ||
.then(function (v) { return v.toString(10); })); | ||
.to(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.nil() | ||
.to(function (b) { return b.value; }) | ||
.to(function (v) { return v.value; }) | ||
.to(function (v) { return v.toString(10); })); | ||
}); | ||
@@ -529,6 +475,6 @@ }); | ||
.index(1) | ||
.then(function (v) { return v.toString(10); })).toStrictEqual(maybe_1.nil() | ||
.then(function (m) { return m[0]; }) | ||
.then(function (m) { return m[1]; }) | ||
.then(function (v) { return v.toString(10); })); | ||
.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); })); | ||
}); | ||
@@ -535,0 +481,0 @@ }); |
@@ -50,3 +50,3 @@ "use strict"; | ||
return function (error, data) { return maybe_1.maybe(error) | ||
.then(reject) | ||
.to(reject) | ||
.or(function () { return resolve(data); }); }; | ||
@@ -53,0 +53,0 @@ } |
{ | ||
"name": "@perfective/maybe", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"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
Sorry, the diff of this file is not supported yet
135600
1607