@sweet-monads/either
Advanced tools
Comparing version 2.1.3 to 2.1.4
196
index.js
@@ -0,111 +1,121 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isWrappedFunction(m) { | ||
return typeof m.value === "function"; | ||
return typeof m.value === "function"; | ||
} | ||
class EitherConstructor { | ||
constructor(type, value) { | ||
this.type = type; | ||
this.value = value; | ||
constructor(type, value) { | ||
this.type = type; | ||
this.value = value; | ||
} | ||
static mergeInOne(eithers) { | ||
return eithers.reduce( | ||
(res, v) => v.chain(v => res.map(res => res.concat([v]))), | ||
EitherConstructor.right([]) | ||
); | ||
} | ||
static mergeInMany(eithers) { | ||
return eithers.reduce((res, v) => { | ||
if (res.isLeft()) { | ||
return v.isLeft() | ||
? EitherConstructor.left(res.value.concat([v.value])) | ||
: res; | ||
} | ||
return v.isLeft() | ||
? EitherConstructor.left([v.value]) | ||
: v.chain(v => res.map(res => [...res, v])); | ||
}, EitherConstructor.right([])); | ||
} | ||
static from(v) { | ||
return this.right(v); | ||
} | ||
static right(v) { | ||
return new EitherConstructor("Right", v); | ||
} | ||
static left(v) { | ||
return new EitherConstructor("Left", v); | ||
} | ||
isLeft() { | ||
return this.type === "Left"; | ||
} | ||
isRight() { | ||
return this.type === "Right"; | ||
} | ||
join() { | ||
return this.chain(x => x); | ||
} | ||
mapRight(f) { | ||
return this.map(f); | ||
} | ||
mapLeft(f) { | ||
if (this.isLeft()) { | ||
return EitherConstructor.left(f(this.value)); | ||
} | ||
static mergeInOne(eithers) { | ||
return eithers.reduce((res, v) => v.chain(v => res.map(res => res.concat([v]))), EitherConstructor.right([])); | ||
return EitherConstructor.right(this.value); | ||
} | ||
map(f) { | ||
if (this.isLeft()) { | ||
return EitherConstructor.left(this.value); | ||
} | ||
static mergeInMany(eithers) { | ||
return eithers.reduce((res, v) => { | ||
if (res.isLeft()) { | ||
return v.isLeft() | ||
? EitherConstructor.left(res.value.concat([v.value])) | ||
: res; | ||
} | ||
return v.isLeft() | ||
? EitherConstructor.left([v.value]) | ||
: v.chain(v => res.map(res => [...res, v])); | ||
}, EitherConstructor.right([])); | ||
return EitherConstructor.right(f(this.value)); | ||
} | ||
asyncMap(f) { | ||
if (this.isLeft()) { | ||
return Promise.resolve(EitherConstructor.left(this.value)); | ||
} | ||
static from(v) { | ||
return this.right(v); | ||
return f(this.value).then(v => EitherConstructor.right(v)); | ||
} | ||
apply(argOrFn) { | ||
if (this.isLeft()) { | ||
return EitherConstructor.left(this.value); | ||
} | ||
static right(v) { | ||
return new EitherConstructor("Right", v); | ||
if (argOrFn.isLeft()) { | ||
return EitherConstructor.left(argOrFn.value); | ||
} | ||
static left(v) { | ||
return new EitherConstructor("Left", v); | ||
if (isWrappedFunction(this)) { | ||
return argOrFn.map(this.value); | ||
} | ||
isLeft() { | ||
return this.type === "Left"; | ||
if (isWrappedFunction(argOrFn)) { | ||
return argOrFn.apply(this); | ||
} | ||
isRight() { | ||
return this.type === "Right"; | ||
throw new Error("Some of the arguments should be a function"); | ||
} | ||
asyncApply(argOrFn) { | ||
if (this.isLeft()) { | ||
return Promise.resolve(EitherConstructor.left(this.value)); | ||
} | ||
join() { | ||
return this.chain(x => x); | ||
if (argOrFn.isLeft()) { | ||
return Promise.resolve(EitherConstructor.left(argOrFn.value)); | ||
} | ||
mapRight(f) { | ||
return this.map(f); | ||
if (isWrappedFunction(this)) { | ||
return argOrFn.asyncMap(this.value); | ||
} | ||
mapLeft(f) { | ||
if (this.isLeft()) { | ||
return EitherConstructor.left(f(this.value)); | ||
} | ||
return EitherConstructor.right(this.value); | ||
if (isWrappedFunction(argOrFn)) { | ||
return argOrFn.asyncApply(this); | ||
} | ||
map(f) { | ||
if (this.isLeft()) { | ||
return EitherConstructor.left(this.value); | ||
} | ||
return EitherConstructor.right(f(this.value)); | ||
throw new Error("Some of the arguments should be a function"); | ||
} | ||
chain(f) { | ||
if (this.isLeft()) { | ||
return EitherConstructor.left(this.value); | ||
} | ||
asyncMap(f) { | ||
if (this.isLeft()) { | ||
return Promise.resolve(EitherConstructor.left(this.value)); | ||
} | ||
return f(this.value).then(v => EitherConstructor.right(v)); | ||
return f(this.value); | ||
} | ||
asyncChain(f) { | ||
if (this.isLeft()) { | ||
return Promise.resolve(EitherConstructor.left(this.value)); | ||
} | ||
apply(argOrFn) { | ||
if (this.isLeft()) { | ||
return EitherConstructor.left(this.value); | ||
} | ||
if (argOrFn.isLeft()) { | ||
return EitherConstructor.left(argOrFn.value); | ||
} | ||
if (isWrappedFunction(this)) { | ||
return argOrFn.map(this.value); | ||
} | ||
if (isWrappedFunction(argOrFn)) { | ||
return argOrFn.apply(this); | ||
} | ||
throw new Error("Some of the arguments should be a function"); | ||
} | ||
asyncApply(argOrFn) { | ||
if (this.isLeft()) { | ||
return Promise.resolve(EitherConstructor.left(this.value)); | ||
} | ||
if (argOrFn.isLeft()) { | ||
return Promise.resolve(EitherConstructor.left(argOrFn.value)); | ||
} | ||
if (isWrappedFunction(this)) { | ||
return argOrFn.asyncMap(this.value); | ||
} | ||
if (isWrappedFunction(argOrFn)) { | ||
return argOrFn.asyncApply(this); | ||
} | ||
throw new Error("Some of the arguments should be a function"); | ||
} | ||
chain(f) { | ||
if (this.isLeft()) { | ||
return EitherConstructor.left(this.value); | ||
} | ||
return f(this.value); | ||
} | ||
asyncChain(f) { | ||
if (this.isLeft()) { | ||
return Promise.resolve(EitherConstructor.left(this.value)); | ||
} | ||
return f(this.value); | ||
} | ||
or(x) { | ||
return this.isLeft() ? x : this; | ||
} | ||
return f(this.value); | ||
} | ||
or(x) { | ||
return this.isLeft() ? x : this; | ||
} | ||
} | ||
EitherConstructor.merge = EitherConstructor.mergeInOne; | ||
export const { merge, mergeInOne, mergeInMany, left, right, from } = EitherConstructor; | ||
export const isEither = (value) => value instanceof EitherConstructor; | ||
(exports.merge = EitherConstructor.merge), | ||
(exports.mergeInOne = EitherConstructor.mergeInOne), | ||
(exports.mergeInMany = EitherConstructor.mergeInMany), | ||
(exports.left = EitherConstructor.left), | ||
(exports.right = EitherConstructor.right), | ||
(exports.from = EitherConstructor.from); | ||
exports.isEither = value => value instanceof EitherConstructor; |
{ | ||
"name": "@sweet-monads/either", | ||
"version": "2.1.3", | ||
"version": "2.1.4", | ||
"description": "Either monad", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
36473
7
660