@sweet-monads/maybe
Advanced tools
Comparing version 2.1.4 to 2.2.0
@@ -1,2 +0,2 @@ | ||
import { Monad, Alternative } from "@sweet-monads/interfaces"; | ||
import type { Monad, Alternative } from "@sweet-monads/interfaces"; | ||
declare const enum MaybeState { | ||
@@ -9,2 +9,3 @@ Just = "Just", | ||
readonly value: S extends MaybeState.Just ? T : undefined; | ||
static chain<A, B>(f: (v: A) => Promise<Maybe<B>>): (m: Maybe<A>) => Promise<Maybe<B>>; | ||
static merge<V1>(values: [Maybe<V1>]): Maybe<[V1]>; | ||
@@ -16,6 +17,44 @@ static merge<V1, V2>(values: [Maybe<V1>, Maybe<V2>]): Maybe<[V1, V2]>; | ||
static merge<V1, V2, V3, V4, V5, V6>(values: [Maybe<V1>, Maybe<V2>, Maybe<V3>, Maybe<V4>, Maybe<V5>, Maybe<V6>]): Maybe<[V1, V2, V3, V4, V5, V6]>; | ||
static merge<V1, V2, V3, V4, V5, V6, V7>(values: [Maybe<V1>, Maybe<V2>, Maybe<V3>, Maybe<V4>, Maybe<V5>, Maybe<V6>, Maybe<V7>]): Maybe<[V1, V2, V3, V4, V5, V6, V7]>; | ||
static merge<V1, V2, V3, V4, V5, V6, V7, V8>(values: [Maybe<V1>, Maybe<V2>, Maybe<V3>, Maybe<V4>, Maybe<V5>, Maybe<V6>, Maybe<V7>, Maybe<V8>]): Maybe<[V1, V2, V3, V4, V5, V6, V7, V8]>; | ||
static merge<V1, V2, V3, V4, V5, V6, V7, V8, V9>(values: [Maybe<V1>, Maybe<V2>, Maybe<V3>, Maybe<V4>, Maybe<V5>, Maybe<V6>, Maybe<V7>, Maybe<V8>, Maybe<V9>]): Maybe<[V1, V2, V3, V4, V5, V6, V7, V8, V9]>; | ||
static merge<V1, V2, V3, V4, V5, V6, V7, V8, V9, L10, V10>(values: [Maybe<V1>, Maybe<V2>, Maybe<V3>, Maybe<V4>, Maybe<V5>, Maybe<V6>, Maybe<V7>, Maybe<V8>, Maybe<V9>, Maybe<V10>]): Maybe<[V1, V2, V3, V4, V5, V6, V7, V8, V9, V10]>; | ||
static merge<V1, V2, V3, V4, V5, V6, V7>(values: [ | ||
Maybe<V1>, | ||
Maybe<V2>, | ||
Maybe<V3>, | ||
Maybe<V4>, | ||
Maybe<V5>, | ||
Maybe<V6>, | ||
Maybe<V7> | ||
]): Maybe<[V1, V2, V3, V4, V5, V6, V7]>; | ||
static merge<V1, V2, V3, V4, V5, V6, V7, V8>(values: [ | ||
Maybe<V1>, | ||
Maybe<V2>, | ||
Maybe<V3>, | ||
Maybe<V4>, | ||
Maybe<V5>, | ||
Maybe<V6>, | ||
Maybe<V7>, | ||
Maybe<V8> | ||
]): Maybe<[V1, V2, V3, V4, V5, V6, V7, V8]>; | ||
static merge<V1, V2, V3, V4, V5, V6, V7, V8, V9>(values: [ | ||
Maybe<V1>, | ||
Maybe<V2>, | ||
Maybe<V3>, | ||
Maybe<V4>, | ||
Maybe<V5>, | ||
Maybe<V6>, | ||
Maybe<V7>, | ||
Maybe<V8>, | ||
Maybe<V9> | ||
]): Maybe<[V1, V2, V3, V4, V5, V6, V7, V8, V9]>; | ||
static merge<V1, V2, V3, V4, V5, V6, V7, V8, V9, L10, V10>(values: [ | ||
Maybe<V1>, | ||
Maybe<V2>, | ||
Maybe<V3>, | ||
Maybe<V4>, | ||
Maybe<V5>, | ||
Maybe<V6>, | ||
Maybe<V7>, | ||
Maybe<V8>, | ||
Maybe<V9>, | ||
Maybe<V10> | ||
]): Maybe<[V1, V2, V3, V4, V5, V6, V7, V8, V9, V10]>; | ||
static merge<T>(maybies: Array<Maybe<T>>): Maybe<T[]>; | ||
@@ -37,7 +76,7 @@ static from<T>(v: T): Maybe<T>; | ||
asyncChain<V>(f: (r: T) => Promise<Maybe<V>>): Promise<Maybe<V>>; | ||
or(x: Maybe<T>): Maybe<T>; | ||
or(x: Maybe<T>): MaybeConstructor<T, MaybeState.None>; | ||
} | ||
export declare 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; | ||
export declare const merge: typeof MaybeConstructor.merge, just: typeof MaybeConstructor.just, none: typeof MaybeConstructor.none, from: typeof MaybeConstructor.from, chain: typeof MaybeConstructor.chain; | ||
export declare const isMaybe: <T>(value: unknown) => value is Maybe<T>; | ||
export {}; |
154
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isMaybe = exports.chain = exports.from = exports.none = exports.just = exports.merge = void 0; | ||
var MaybeState; | ||
(function (MaybeState) { | ||
MaybeState["Just"] = "Just"; | ||
MaybeState["None"] = "None"; | ||
})(MaybeState || (MaybeState = {})); | ||
function isWrappedFunction(m) { | ||
return typeof m.value === "function"; | ||
return typeof m.value === "function"; | ||
} | ||
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)); | ||
constructor(type, value) { | ||
this.type = type; | ||
this.value = value; | ||
} | ||
return MaybeConstructor.none(); | ||
} | ||
asyncMap(f) { | ||
if (this.isNone()) { | ||
return Promise.resolve(MaybeConstructor.none()); | ||
static chain(f) { | ||
return (m) => m.asyncChain(f); | ||
} | ||
return f(this.value).then(v => MaybeConstructor.just(v)); | ||
} | ||
apply(argOrFn) { | ||
if (this.isNone() || argOrFn.isNone()) { | ||
return MaybeConstructor.none(); | ||
static merge(maybies) { | ||
return maybies.reduce((res, v) => v.chain(v => res.map(res => res.concat([v]))), MaybeConstructor.just([])); | ||
} | ||
if (isWrappedFunction(this)) { | ||
return argOrFn.map(this.value); | ||
static from(v) { | ||
return this.just(v); | ||
} | ||
if (isWrappedFunction(argOrFn)) { | ||
return argOrFn.apply(this); | ||
static none() { | ||
return new MaybeConstructor("None", undefined); | ||
} | ||
throw new Error("Some of the arguments should be a function"); | ||
} | ||
asyncApply(argOrFn) { | ||
if (this.isNone() || argOrFn.isNone()) { | ||
return Promise.resolve(MaybeConstructor.none()); | ||
static just(v) { | ||
return new MaybeConstructor("Just", v); | ||
} | ||
if (isWrappedFunction(this)) { | ||
return argOrFn.asyncMap(this.value); | ||
isNone() { | ||
return this.type === "None"; | ||
} | ||
if (isWrappedFunction(argOrFn)) { | ||
return argOrFn.asyncApply(this); | ||
isJust() { | ||
return this.type === "Just"; | ||
} | ||
throw new Error("Some of the arguments should be a function"); | ||
} | ||
chain(f) { | ||
if (this.isNone()) { | ||
return MaybeConstructor.none(); | ||
join() { | ||
return this.chain(x => x); | ||
} | ||
return f(this.value); | ||
} | ||
asyncChain(f) { | ||
if (this.isNone()) { | ||
return Promise.resolve(MaybeConstructor.none()); | ||
map(f) { | ||
if (this.isJust()) { | ||
return MaybeConstructor.just(f(this.value)); | ||
} | ||
return MaybeConstructor.none(); | ||
} | ||
return f(this.value); | ||
} | ||
or(x) { | ||
return this.isNone() ? x : this; | ||
} | ||
asyncMap(f) { | ||
if (this.isJust()) { | ||
return f(this.value).then(MaybeConstructor.just); | ||
} | ||
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.isJust()) { | ||
return f(this.value); | ||
} | ||
return MaybeConstructor.none(); | ||
} | ||
asyncChain(f) { | ||
if (this.isJust()) { | ||
return f(this.value); | ||
} | ||
return Promise.resolve(MaybeConstructor.none()); | ||
} | ||
or(x) { | ||
return this.isNone() ? x : this; | ||
} | ||
} | ||
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; | ||
exports.merge = MaybeConstructor.merge, exports.just = MaybeConstructor.just, exports.none = MaybeConstructor.none, exports.from = MaybeConstructor.from, exports.chain = MaybeConstructor.chain; | ||
const isMaybe = (value) => value instanceof MaybeConstructor; | ||
exports.isMaybe = isMaybe; |
{ | ||
"name": "@sweet-monads/maybe", | ||
"version": "2.1.4", | ||
"version": "2.2.0", | ||
"description": "", | ||
@@ -14,3 +14,3 @@ "main": "index.js", | ||
"build": "npm run build:clean && npm run build:start && npm run build:copy", | ||
"build:start": "tsc --strict --declaration --lib es2015 --outDir ./build ./*.ts", | ||
"build:start": "cp ../tsconfig.json tsconfig.json && tsc --project ./tsconfig.json", | ||
"build:clean": "rm -rf build && mkdir build", | ||
@@ -17,0 +17,0 @@ "build:copy": "cp ./package.json ./build/package.json && cp ./README.md ./build/README.md" |
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
16658
4
175
1