@sweet-monads/maybe
Advanced tools
Comparing version 3.1.0 to 3.2.0
@@ -34,2 +34,3 @@ import type { AsyncMonad, Alternative, Container } from "@sweet-monads/interfaces"; | ||
static fromNullable<T>(v: T): Maybe<Exclude<T, null | undefined>>; | ||
private static _noneInstance; | ||
static none<T = never>(): Maybe<T>; | ||
@@ -50,7 +51,10 @@ static just<T>(v: T): Maybe<T>; | ||
or(x: Maybe<T>): Maybe<T>; | ||
unwrap(): T; | ||
unwrap(errorFactory?: () => unknown): T; | ||
unwrapOr(x: T): T; | ||
unwrapOrElse(f: () => T): T; | ||
get [Symbol.toStringTag](): string; | ||
} | ||
export declare type Maybe<T> = MaybeConstructor<T, MaybeState.Just> | MaybeConstructor<T, MaybeState.None>; | ||
export type Maybe<T> = MaybeConstructor<T, MaybeState.Just> | MaybeConstructor<T, MaybeState.None>; | ||
export declare const merge: typeof MaybeConstructor.merge, just: typeof MaybeConstructor.just, none: typeof MaybeConstructor.none, from: typeof MaybeConstructor.from, fromNullable: typeof MaybeConstructor.fromNullable, chain: typeof MaybeConstructor.chain; | ||
export declare const isMaybe: <T>(value: unknown) => value is Maybe<T>; | ||
export {}; |
@@ -16,6 +16,2 @@ "use strict"; | ||
class MaybeConstructor { | ||
constructor(type, value) { | ||
this.type = type; | ||
this.value = value; | ||
} | ||
static chain(f) { | ||
@@ -36,3 +32,6 @@ return (m) => m.asyncChain(f); | ||
static none() { | ||
return new MaybeConstructor("None", undefined); | ||
if (MaybeConstructor._noneInstance === undefined) { | ||
MaybeConstructor._noneInstance = new MaybeConstructor("None", undefined); | ||
} | ||
return MaybeConstructor._noneInstance; | ||
} | ||
@@ -42,2 +41,6 @@ static just(v) { | ||
} | ||
constructor(type, value) { | ||
this.type = type; | ||
this.value = value; | ||
} | ||
isNone() { | ||
@@ -105,7 +108,16 @@ return this.type === "None"; | ||
} | ||
unwrap() { | ||
unwrap(errorFactory = () => new Error("Value is None")) { | ||
if (this.isJust()) | ||
return this.value; | ||
throw new Error("Value is None"); | ||
throw errorFactory(); | ||
} | ||
unwrapOr(x) { | ||
return this.isJust() ? this.value : x; | ||
} | ||
unwrapOrElse(f) { | ||
return this.isJust() ? this.value : f(); | ||
} | ||
get [Symbol.toStringTag]() { | ||
return "Maybe"; | ||
} | ||
} | ||
@@ -112,0 +124,0 @@ exports.default = MaybeConstructor; |
@@ -34,2 +34,3 @@ import type { AsyncMonad, Alternative, Container } from "@sweet-monads/interfaces"; | ||
static fromNullable<T>(v: T): Maybe<Exclude<T, null | undefined>>; | ||
private static _noneInstance; | ||
static none<T = never>(): Maybe<T>; | ||
@@ -50,7 +51,10 @@ static just<T>(v: T): Maybe<T>; | ||
or(x: Maybe<T>): Maybe<T>; | ||
unwrap(): T; | ||
unwrap(errorFactory?: () => unknown): T; | ||
unwrapOr(x: T): T; | ||
unwrapOrElse(f: () => T): T; | ||
get [Symbol.toStringTag](): string; | ||
} | ||
export declare type Maybe<T> = MaybeConstructor<T, MaybeState.Just> | MaybeConstructor<T, MaybeState.None>; | ||
export type Maybe<T> = MaybeConstructor<T, MaybeState.Just> | MaybeConstructor<T, MaybeState.None>; | ||
export declare const merge: typeof MaybeConstructor.merge, just: typeof MaybeConstructor.just, none: typeof MaybeConstructor.none, from: typeof MaybeConstructor.from, fromNullable: typeof MaybeConstructor.fromNullable, chain: typeof MaybeConstructor.chain; | ||
export declare const isMaybe: <T>(value: unknown) => value is Maybe<T>; | ||
export {}; |
@@ -13,6 +13,2 @@ var MaybeState; | ||
export default class MaybeConstructor { | ||
constructor(type, value) { | ||
this.type = type; | ||
this.value = value; | ||
} | ||
static chain(f) { | ||
@@ -33,3 +29,6 @@ return (m) => m.asyncChain(f); | ||
static none() { | ||
return new MaybeConstructor("None", undefined); | ||
if (MaybeConstructor._noneInstance === undefined) { | ||
MaybeConstructor._noneInstance = new MaybeConstructor("None", undefined); | ||
} | ||
return MaybeConstructor._noneInstance; | ||
} | ||
@@ -39,2 +38,6 @@ static just(v) { | ||
} | ||
constructor(type, value) { | ||
this.type = type; | ||
this.value = value; | ||
} | ||
isNone() { | ||
@@ -102,9 +105,18 @@ return this.type === "None"; | ||
} | ||
unwrap() { | ||
unwrap(errorFactory = () => new Error("Value is None")) { | ||
if (this.isJust()) | ||
return this.value; | ||
throw new Error("Value is None"); | ||
throw errorFactory(); | ||
} | ||
unwrapOr(x) { | ||
return this.isJust() ? this.value : x; | ||
} | ||
unwrapOrElse(f) { | ||
return this.isJust() ? this.value : f(); | ||
} | ||
get [Symbol.toStringTag]() { | ||
return "Maybe"; | ||
} | ||
} | ||
export const { merge, just, none, from, fromNullable, chain } = MaybeConstructor; | ||
export const isMaybe = (value) => value instanceof MaybeConstructor; |
@@ -34,2 +34,3 @@ import type { AsyncMonad, Alternative, Container } from "@sweet-monads/interfaces"; | ||
static fromNullable<T>(v: T): Maybe<Exclude<T, null | undefined>>; | ||
private static _noneInstance; | ||
static none<T = never>(): Maybe<T>; | ||
@@ -50,7 +51,10 @@ static just<T>(v: T): Maybe<T>; | ||
or(x: Maybe<T>): Maybe<T>; | ||
unwrap(): T; | ||
unwrap(errorFactory?: () => unknown): T; | ||
unwrapOr(x: T): T; | ||
unwrapOrElse(f: () => T): T; | ||
get [Symbol.toStringTag](): string; | ||
} | ||
export declare type Maybe<T> = MaybeConstructor<T, MaybeState.Just> | MaybeConstructor<T, MaybeState.None>; | ||
export type Maybe<T> = MaybeConstructor<T, MaybeState.Just> | MaybeConstructor<T, MaybeState.None>; | ||
export declare const merge: typeof MaybeConstructor.merge, just: typeof MaybeConstructor.just, none: typeof MaybeConstructor.none, from: typeof MaybeConstructor.from, fromNullable: typeof MaybeConstructor.fromNullable, chain: typeof MaybeConstructor.chain; | ||
export declare const isMaybe: <T>(value: unknown) => value is Maybe<T>; | ||
export {}; |
{ | ||
"name": "@sweet-monads/maybe", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "", | ||
@@ -8,6 +8,6 @@ "main": "./cjs/index.js", | ||
"exports": { | ||
"types": "./index.d.ts", | ||
"import": "./esm/index.js", | ||
"require": "./cjs/index.js" | ||
}, | ||
"types": "index.d.ts", | ||
"homepage": "https://github.com/JSMonk/sweet-monads/tree/master/maybe", | ||
@@ -33,3 +33,3 @@ "repository": { | ||
"dependencies": { | ||
"@sweet-monads/interfaces": "^3.1.0" | ||
"@sweet-monads/interfaces": "^3.2.0" | ||
}, | ||
@@ -36,0 +36,0 @@ "author": "", |
@@ -16,2 +16,3 @@ # @sweet-monads/maybe | ||
[maybe](https://github.com/JSMonk/sweet-monads/tree/master/maybe) | ||
[identity](https://github.com/JSMonk/sweet-monads/tree/master/identity) | ||
@@ -63,3 +64,3 @@ ## Usage | ||
- `fn: (v: A) => Promise<Maybe<B>>` - function which should be applied asynchronously to `Maybe<A>` value | ||
- Returns function with `Maybe<A>` argument and promisied `Maybe` with `Maybe.None` or maped by `fn` value (could be used inside `Promise#then` function). | ||
- Returns function with `Maybe<A>` argument and promised `Maybe` with `Maybe.None` or mapped by `fn` value (could be used inside `Promise#then` function). | ||
@@ -390,5 +391,13 @@ Example: | ||
```typescript | ||
const value = just(2).unwrap(); // returns 2 | ||
just(2).unwrap(); // returns 2 | ||
none().unwrap(); // Throws error | ||
none().unwrap(); // Throws error | ||
just(2).unwrap(() => new Error("NEVER!")); // returns 2 | ||
none().unwrap(() => new CustomError("My error")); // Throws CustomError | ||
just(2).unwrapOr(3) // returns 3 | ||
none().unwrapOr(3) // returns 2 | ||
just(2).unwrapOrElse(num => num * 2) // returns 4 | ||
none().unwrapOrElse(num => num * 2) // returns 2 | ||
``` | ||
@@ -395,0 +404,0 @@ |
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
30623
415
405