@sweet-monads/maybe
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -1,2 +0,2 @@ | ||
import type { AsyncMonad, Alternative } from "@sweet-monads/interfaces"; | ||
import type { AsyncMonad, Alternative, Container } from "@sweet-monads/interfaces"; | ||
declare const enum MaybeState { | ||
@@ -6,3 +6,3 @@ Just = "Just", | ||
} | ||
export default class MaybeConstructor<T, S extends MaybeState = MaybeState> implements AsyncMonad<T>, Alternative<T> { | ||
export default class MaybeConstructor<T, S extends MaybeState = MaybeState> implements AsyncMonad<T>, Alternative<T>, Container<T> { | ||
private readonly type; | ||
@@ -49,3 +49,4 @@ readonly value: S extends MaybeState.Just ? T : undefined; | ||
asyncChain<V>(f: (r: T) => Promise<Maybe<V>>): Promise<Maybe<V>>; | ||
or(x: Maybe<T>): MaybeConstructor<T, MaybeState.None>; | ||
or(x: Maybe<T>): Maybe<T>; | ||
unwrap(): T; | ||
} | ||
@@ -52,0 +53,0 @@ export declare type Maybe<T> = MaybeConstructor<T, MaybeState.Just> | MaybeConstructor<T, MaybeState.None>; |
@@ -12,2 +12,5 @@ "use strict"; | ||
} | ||
function isWrappedAsyncFunction(m) { | ||
return typeof m.value === "function"; | ||
} | ||
class MaybeConstructor { | ||
@@ -75,3 +78,3 @@ constructor(type, value) { | ||
} | ||
if (isWrappedFunction(this)) { | ||
if (isWrappedAsyncFunction(this)) { | ||
return argOrFn | ||
@@ -81,3 +84,3 @@ .map(a => Promise.resolve(a)) | ||
} | ||
if (isWrappedFunction(argOrFn)) { | ||
if (isWrappedAsyncFunction(argOrFn)) { | ||
return argOrFn.asyncApply(this); | ||
@@ -102,2 +105,7 @@ } | ||
} | ||
unwrap() { | ||
if (this.isJust()) | ||
return this.value; | ||
throw new Error("Value is None"); | ||
} | ||
} | ||
@@ -104,0 +112,0 @@ exports.default = MaybeConstructor; |
@@ -1,2 +0,2 @@ | ||
import type { AsyncMonad, Alternative } from "@sweet-monads/interfaces"; | ||
import type { AsyncMonad, Alternative, Container } from "@sweet-monads/interfaces"; | ||
declare const enum MaybeState { | ||
@@ -6,3 +6,3 @@ Just = "Just", | ||
} | ||
export default class MaybeConstructor<T, S extends MaybeState = MaybeState> implements AsyncMonad<T>, Alternative<T> { | ||
export default class MaybeConstructor<T, S extends MaybeState = MaybeState> implements AsyncMonad<T>, Alternative<T>, Container<T> { | ||
private readonly type; | ||
@@ -49,3 +49,4 @@ readonly value: S extends MaybeState.Just ? T : undefined; | ||
asyncChain<V>(f: (r: T) => Promise<Maybe<V>>): Promise<Maybe<V>>; | ||
or(x: Maybe<T>): MaybeConstructor<T, MaybeState.None>; | ||
or(x: Maybe<T>): Maybe<T>; | ||
unwrap(): T; | ||
} | ||
@@ -52,0 +53,0 @@ export declare type Maybe<T> = MaybeConstructor<T, MaybeState.Just> | MaybeConstructor<T, MaybeState.None>; |
@@ -9,2 +9,5 @@ var MaybeState; | ||
} | ||
function isWrappedAsyncFunction(m) { | ||
return typeof m.value === "function"; | ||
} | ||
export default class MaybeConstructor { | ||
@@ -72,3 +75,3 @@ constructor(type, value) { | ||
} | ||
if (isWrappedFunction(this)) { | ||
if (isWrappedAsyncFunction(this)) { | ||
return argOrFn | ||
@@ -78,3 +81,3 @@ .map(a => Promise.resolve(a)) | ||
} | ||
if (isWrappedFunction(argOrFn)) { | ||
if (isWrappedAsyncFunction(argOrFn)) { | ||
return argOrFn.asyncApply(this); | ||
@@ -99,4 +102,9 @@ } | ||
} | ||
unwrap() { | ||
if (this.isJust()) | ||
return this.value; | ||
throw new Error("Value is None"); | ||
} | ||
} | ||
export const { merge, just, none, from, fromNullable, chain } = MaybeConstructor; | ||
export const isMaybe = (value) => value instanceof MaybeConstructor; |
@@ -1,2 +0,2 @@ | ||
import type { AsyncMonad, Alternative } from "@sweet-monads/interfaces"; | ||
import type { AsyncMonad, Alternative, Container } from "@sweet-monads/interfaces"; | ||
declare const enum MaybeState { | ||
@@ -6,3 +6,3 @@ Just = "Just", | ||
} | ||
export default class MaybeConstructor<T, S extends MaybeState = MaybeState> implements AsyncMonad<T>, Alternative<T> { | ||
export default class MaybeConstructor<T, S extends MaybeState = MaybeState> implements AsyncMonad<T>, Alternative<T>, Container<T> { | ||
private readonly type; | ||
@@ -49,3 +49,4 @@ readonly value: S extends MaybeState.Just ? T : undefined; | ||
asyncChain<V>(f: (r: T) => Promise<Maybe<V>>): Promise<Maybe<V>>; | ||
or(x: Maybe<T>): MaybeConstructor<T, MaybeState.None>; | ||
or(x: Maybe<T>): Maybe<T>; | ||
unwrap(): T; | ||
} | ||
@@ -52,0 +53,0 @@ export declare type Maybe<T> = MaybeConstructor<T, MaybeState.Just> | MaybeConstructor<T, MaybeState.None>; |
{ | ||
"name": "@sweet-monads/maybe", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "", | ||
@@ -32,3 +32,3 @@ "main": "./cjs/index.js", | ||
"dependencies": { | ||
"@sweet-monads/interfaces": "^3.0.0" | ||
"@sweet-monads/interfaces": "^3.1.0" | ||
}, | ||
@@ -35,0 +35,0 @@ "author": "", |
@@ -387,4 +387,10 @@ # @sweet-monads/maybe | ||
```typescript | ||
const value = just(2).unwrap(); // returns 2 | ||
none().unwrap(); // Throws error | ||
``` | ||
## License | ||
MIT (c) Artem Kobzar see LICENSE file. |
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
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
29029
379
396
0