@sweet-monads/maybe
Advanced tools
Comparing version 1.2.2 to 1.3.0
import { Monad } from "@sweet-monads/interfaces"; | ||
export declare class Maybe<T> implements Monad<T> { | ||
export default class Maybe<T> implements Monad<T> { | ||
private readonly state; | ||
@@ -4,0 +4,0 @@ readonly value?: T | undefined; |
79
index.js
@@ -1,35 +0,40 @@ | ||
const MaybeState = { | ||
Just: "Just", | ||
None: "None" | ||
}; | ||
"use strict"; | ||
exports.__esModule = true; | ||
var MaybeState; | ||
(function (MaybeState) { | ||
MaybeState["Just"] = "Just"; | ||
MaybeState["None"] = "None"; | ||
})(MaybeState || (MaybeState = {})); | ||
function isWrappedFunction(m) { | ||
return typeof m.value === "function"; | ||
} | ||
module.exports = class Maybe { | ||
constructor(state, value) { | ||
var Maybe = /** @class */ (function () { | ||
function Maybe(state, value) { | ||
this.state = state; | ||
this.value = value; | ||
} | ||
static merge(maybies) { | ||
return maybies.reduce((res, v) => v.chain(v => res.map(res => res.concat([v]))), Maybe.just([])); | ||
} | ||
static from(v) { | ||
Maybe.merge = function (maybies) { | ||
return maybies.reduce(function (res, v) { | ||
return v.chain(function (v) { return res.map(function (res) { return res.concat([v]); }); }); | ||
}, Maybe.just([])); | ||
}; | ||
Maybe.from = function (v) { | ||
return this.just(v); | ||
} | ||
static none() { | ||
}; | ||
Maybe.none = function () { | ||
return new Maybe(MaybeState.None); | ||
} | ||
static just(v) { | ||
}; | ||
Maybe.just = function (v) { | ||
return new Maybe(MaybeState.Just, v); | ||
} | ||
isNone() { | ||
}; | ||
Maybe.prototype.isNone = function () { | ||
return this.state === MaybeState.None; | ||
} | ||
isJust() { | ||
}; | ||
Maybe.prototype.isJust = function () { | ||
return this.state === MaybeState.Just; | ||
} | ||
join() { | ||
return this.chain(x => x); | ||
} | ||
map(f) { | ||
}; | ||
Maybe.prototype.join = function () { | ||
return this.chain(function (x) { return x; }); | ||
}; | ||
Maybe.prototype.map = function (f) { | ||
if (this.isJust()) { | ||
@@ -39,10 +44,10 @@ return Maybe.just(f(this.value)); | ||
return Maybe.none(); | ||
} | ||
asyncMap(f) { | ||
}; | ||
Maybe.prototype.asyncMap = function (f) { | ||
if (this.isNone()) { | ||
return Promise.resolve(Maybe.none()); | ||
} | ||
return f(this.value).then(v => Maybe.just(v)); | ||
} | ||
apply(argOrFn) { | ||
return f(this.value).then(function (v) { return Maybe.just(v); }); | ||
}; | ||
Maybe.prototype.apply = function (argOrFn) { | ||
if (isWrappedFunction(this) && !isWrappedFunction(argOrFn)) { | ||
@@ -55,4 +60,4 @@ if (this.isJust()) { | ||
return argOrFn.apply(this); | ||
} | ||
asyncApply(argOrFn) { | ||
}; | ||
Maybe.prototype.asyncApply = function (argOrFn) { | ||
if (isWrappedFunction(this) && !isWrappedFunction(argOrFn)) { | ||
@@ -65,4 +70,4 @@ if (this.isJust()) { | ||
return argOrFn.asyncApply(this); | ||
} | ||
chain(f) { | ||
}; | ||
Maybe.prototype.chain = function (f) { | ||
if (this.isNone()) { | ||
@@ -72,4 +77,4 @@ return Maybe.none(); | ||
return f(this.value); | ||
} | ||
asyncChain(f) { | ||
}; | ||
Maybe.prototype.asyncChain = function (f) { | ||
if (this.isNone()) { | ||
@@ -79,3 +84,5 @@ return Promise.resolve(Maybe.none()); | ||
return f(this.value); | ||
} | ||
} | ||
}; | ||
return Maybe; | ||
}()); | ||
exports["default"] = Maybe; |
{ | ||
"name": "@sweet-monads/maybe", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "", | ||
@@ -12,2 +12,8 @@ "main": "index.js", | ||
}, | ||
"scripts": { | ||
"build": "npm run build:clean && npm run build:start && npm run build:copy", | ||
"build:start": "tsc --strict --declaration --lib es2015 --outDir ./build ./*.ts", | ||
"build:clean": "rm -rf build && mkdir build", | ||
"build:copy": "cp ./package.json ./build/package.json && cp ./README.md ./build/README.md" | ||
}, | ||
"dependencies": { | ||
@@ -14,0 +20,0 @@ "@sweet-monads/interfaces": "^1.2.0" |
@@ -21,3 +21,3 @@ # @sweet-monads/maybe | ||
```typescript | ||
import { Maybe } from "@sweet-monads/maybe"; | ||
import Maybe from "@sweet-monads/maybe"; | ||
@@ -24,0 +24,0 @@ type User = { email: string, password: string }; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
14572
4
114
1