@sweet-monads/maybe
Advanced tools
Comparing version 2.1.2 to 2.1.4
149
index.js
@@ -0,83 +1,92 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isWrappedFunction(m) { | ||
return typeof m.value === "function"; | ||
return typeof m.value === "function"; | ||
} | ||
export default class MaybeConstructor { | ||
constructor(type, value) { | ||
this.type = type; | ||
this.value = value; | ||
class MaybeConstructor { | ||
constructor(type, value) { | ||
this.type = type; | ||
this.value = value; | ||
} | ||
static merge(maybies) { | ||
return maybies.reduce( | ||
(res, v) => v.chain(v => res.map(res => res.concat([v]))), | ||
MaybeConstructor.just([]) | ||
); | ||
} | ||
static from(v) { | ||
return this.just(v); | ||
} | ||
static none() { | ||
return new MaybeConstructor("None", undefined); | ||
} | ||
static just(v) { | ||
return new MaybeConstructor("Just", v); | ||
} | ||
isNone() { | ||
return this.type === "None"; | ||
} | ||
isJust() { | ||
return this.type === "Just"; | ||
} | ||
join() { | ||
return this.chain(x => x); | ||
} | ||
map(f) { | ||
if (this.isJust()) { | ||
return MaybeConstructor.just(f(this.value)); | ||
} | ||
static merge(maybies) { | ||
return maybies.reduce((res, v) => v.chain(v => res.map(res => res.concat([v]))), MaybeConstructor.just([])); | ||
return MaybeConstructor.none(); | ||
} | ||
asyncMap(f) { | ||
if (this.isNone()) { | ||
return Promise.resolve(MaybeConstructor.none()); | ||
} | ||
static from(v) { | ||
return this.just(v); | ||
return f(this.value).then(v => MaybeConstructor.just(v)); | ||
} | ||
apply(argOrFn) { | ||
if (this.isNone() || argOrFn.isNone()) { | ||
return MaybeConstructor.none(); | ||
} | ||
static none() { | ||
return new MaybeConstructor("None", undefined); | ||
if (isWrappedFunction(this)) { | ||
return argOrFn.map(this.value); | ||
} | ||
static just(v) { | ||
return new MaybeConstructor("Just", v); | ||
if (isWrappedFunction(argOrFn)) { | ||
return argOrFn.apply(this); | ||
} | ||
isNone() { | ||
return this.type === "None"; | ||
throw new Error("Some of the arguments should be a function"); | ||
} | ||
asyncApply(argOrFn) { | ||
if (this.isNone() || argOrFn.isNone()) { | ||
return Promise.resolve(MaybeConstructor.none()); | ||
} | ||
isJust() { | ||
return this.type === "Just"; | ||
if (isWrappedFunction(this)) { | ||
return argOrFn.asyncMap(this.value); | ||
} | ||
join() { | ||
return this.chain(x => x); | ||
if (isWrappedFunction(argOrFn)) { | ||
return argOrFn.asyncApply(this); | ||
} | ||
map(f) { | ||
if (this.isJust()) { | ||
return MaybeConstructor.just(f(this.value)); | ||
} | ||
return MaybeConstructor.none(); | ||
throw new Error("Some of the arguments should be a function"); | ||
} | ||
chain(f) { | ||
if (this.isNone()) { | ||
return MaybeConstructor.none(); | ||
} | ||
asyncMap(f) { | ||
if (this.isNone()) { | ||
return Promise.resolve(MaybeConstructor.none()); | ||
} | ||
return f(this.value).then(v => MaybeConstructor.just(v)); | ||
return f(this.value); | ||
} | ||
asyncChain(f) { | ||
if (this.isNone()) { | ||
return Promise.resolve(MaybeConstructor.none()); | ||
} | ||
apply(argOrFn) { | ||
if (this.isNone() || argOrFn.isNone()) { | ||
return MaybeConstructor.none(); | ||
} | ||
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.isNone() || argOrFn.isNone()) { | ||
return Promise.resolve(MaybeConstructor.none()); | ||
} | ||
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.isNone()) { | ||
return MaybeConstructor.none(); | ||
} | ||
return f(this.value); | ||
} | ||
asyncChain(f) { | ||
if (this.isNone()) { | ||
return Promise.resolve(MaybeConstructor.none()); | ||
} | ||
return f(this.value); | ||
} | ||
or(x) { | ||
return this.isNone() ? x : this; | ||
} | ||
return f(this.value); | ||
} | ||
or(x) { | ||
return this.isNone() ? x : this; | ||
} | ||
} | ||
export const { merge, just, none, from } = MaybeConstructor; | ||
export const isMaybe = (value) => value instanceof MaybeConstructor; | ||
exports.default = MaybeConstructor; | ||
(exports.merge = MaybeConstructor.merge), | ||
(exports.just = MaybeConstructor.just), | ||
(exports.none = MaybeConstructor.none), | ||
(exports.from = MaybeConstructor.from); | ||
exports.isMaybe = value => value instanceof MaybeConstructor; |
{ | ||
"name": "@sweet-monads/maybe", | ||
"version": "2.1.2", | ||
"version": "2.1.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
316
22409