@sweet-monads/maybe
Advanced tools
Comparing version 2.2.2 to 2.3.0
@@ -16,3 +16,6 @@ import type { Monad, Alternative } from "@sweet-monads/interfaces"; | ||
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: [ | ||
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, V10>(values: [ | ||
Maybe<V1>, | ||
@@ -24,34 +27,4 @@ Maybe<V2>, | ||
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>, | ||
@@ -62,2 +35,3 @@ Maybe<V10> | ||
static from<T>(v: T): Maybe<T>; | ||
static fromNullable<T>(v: T): Maybe<Exclude<T, null | undefined>>; | ||
static none<T>(): Maybe<T>; | ||
@@ -73,4 +47,4 @@ static just<T>(v: T): Maybe<T>; | ||
apply<A, B>(this: Maybe<A>, fn: Maybe<(a: A) => B>): Maybe<B>; | ||
asyncApply<A, B>(this: Maybe<(a: Promise<A> | A) => Promise<B>>, arg: Maybe<Promise<A> | A>): Promise<Maybe<B>>; | ||
asyncApply<A, B>(this: Maybe<Promise<A> | A>, fn: Maybe<(a: Promise<A> | A) => Promise<B>>): Promise<Maybe<B>>; | ||
asyncApply<A, B>(this: Maybe<(a: A) => Promise<B>>, arg: Maybe<Promise<A> | A>): Promise<Maybe<B>>; | ||
asyncApply<A, B>(this: Maybe<Promise<A> | A>, fn: Maybe<(a: A) => Promise<B>>): Promise<Maybe<B>>; | ||
chain<V>(f: (r: T) => Maybe<V>): Maybe<V>; | ||
@@ -81,4 +55,4 @@ asyncChain<V>(f: (r: T) => Promise<Maybe<V>>): Promise<Maybe<V>>; | ||
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, chain: typeof MaybeConstructor.chain; | ||
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": "2.2.2", | ||
"version": "2.3.0", | ||
"description": "", | ||
"main": "index.js", | ||
"main": "./cjs/index.js", | ||
"module": "./esm/index.js", | ||
"exports": { | ||
"import": "./esm/index.js", | ||
"require": "./cjs/index.js" | ||
}, | ||
"types": "index.d.ts", | ||
@@ -13,9 +18,17 @@ "homepage": "https://github.com/JSMonk/sweet-monads/tree/master/maybe", | ||
"scripts": { | ||
"build": "npm run build:clean && npm run build:start && npm run build:copy", | ||
"build:start": "cp ../tsconfig.json tsconfig.json && tsc --project ./tsconfig.json", | ||
"build": "run-s build:pre build:all build:after", | ||
"build:clean": "rm -rf build && mkdir build", | ||
"build:copy": "cp ./package.json ./build/package.json && cp ./README.md ./build/README.md" | ||
"build:config": "cp ../tsconfig.json tsconfig.json", | ||
"build:pre": "run-p build:clean build:config", | ||
"build:esm": "tsc --project ./tsconfig.json --module 'ESNext' --outDir './build/esm'", | ||
"build:cjs": "tsc --project ./tsconfig.json --module 'CommonJS' --outDir './build/cjs'", | ||
"build:declaration": "tsc --project ./tsconfig.json --outDir './build' --emitDeclarationOnly", | ||
"build:all": "run-p build:esm build:cjs build:declaration", | ||
"build:copy": "cp ./package.json ./build/package.json && cp ./README.md ./build/README.md", | ||
"build:fixcjs": "echo '{\"type\":\"commonjs\"}' > ./build/cjs/package.json", | ||
"build:fixesm": "echo '{\"type\":\"module\"}' > ./build/esm/package.json", | ||
"build:after": "run-p build:copy build:fixcjs build:fixesm" | ||
}, | ||
"dependencies": { | ||
"@sweet-monads/interfaces": "^1.3.0" | ||
"@sweet-monads/interfaces": "^2.3.0" | ||
}, | ||
@@ -22,0 +35,0 @@ "author": "", |
149
README.md
@@ -5,3 +5,3 @@ # @sweet-monads/maybe | ||
### This library belongs to *sweet-monads* project | ||
### This library belongs to _sweet-monads_ project | ||
@@ -13,5 +13,6 @@ > **sweet-monads** — easy-to-use monads implementation with static types definition and separated packages. | ||
- Check out all libraries: | ||
[either](https://github.com/JSMonk/sweet-monads/tree/master/either), | ||
[either](https://github.com/JSMonk/sweet-monads/tree/master/either), | ||
[iterator](https://github.com/JSMonk/sweet-monads/tree/master/iterator), | ||
[interfaces](https://github.com/JSMonk/sweet-monads/tree/master/interfaces), | ||
[maybe](https://github.com/JSMonk/sweet-monads/tree/master/maybe), | ||
@@ -25,3 +26,3 @@ ## Usage | ||
type User = { email: string, password: string }; | ||
type User = { email: string; password: string }; | ||
@@ -43,2 +44,3 @@ function getUser(id: number): Maybe<User> { | ||
- [`from`](#from) | ||
- [`fromNullable`](#fromnullable) | ||
- [`isMaybe`](#ismaybe) | ||
@@ -58,5 +60,7 @@ - [`Maybe#isNone`](#maybeisnone) | ||
#### `chain` | ||
```typescript | ||
function chain<A, B>(fn: (v: A) => Promise<Maybe<B>>): (m: Maybe<A>) => Promise<Maybe<B>> | ||
function chain<A, B>(fn: (v: A) => Promise<Maybe<B>>): (m: Maybe<A>) => Promise<Maybe<B>>; | ||
``` | ||
- `fn: (v: A) => Promise<Maybe<B>>` - function which should be applied asynchronously to `Maybe<A>` value | ||
@@ -66,2 +70,3 @@ - Returns function with `Maybe<A>` argument and promisied `Maybe` with `Maybe.None` or maped by `fn` value (could be used inside `Promise#then` function). | ||
Example: | ||
```typescript | ||
@@ -77,2 +82,3 @@ const getValue = async () => just(1); | ||
#### `merge` | ||
```typescript | ||
@@ -84,6 +90,8 @@ function merge<V1>(values: [Maybe<V1>]): Maybe<[V1]>; | ||
``` | ||
- `values: Array<Maybe<T>>` - Array of Maybe values which will be merged into Maybe of Array | ||
- Returns `Maybe<Array<T>>` Maybe of Array which will contain `Just<Array<T>>` if all of array elements was `Just<T>` else `None`. | ||
- `values: Array<Maybe<T>>` - Array of Maybe values which will be merged into Maybe of Array | ||
- Returns `Maybe<Array<T>>` Maybe of Array which will contain `Just<Array<T>>` if all of array elements was `Just<T>` else `None`. | ||
Example: | ||
```typescript | ||
@@ -94,12 +102,15 @@ const v1 = just(2); // Maybe<number>.Just | ||
merge([v1, v2]) // Maybe<[number, string]>.Just | ||
merge([v1, v2, v3]) // Maybe<[number, string, boolean]>.None | ||
merge([v1, v2]); // Maybe<[number, string]>.Just | ||
merge([v1, v2, v3]); // Maybe<[number, string, boolean]>.None | ||
``` | ||
#### `none` | ||
```typescript | ||
function none<T>(): Maybe<T>; | ||
``` | ||
- Returns `Maybe` with `None` state | ||
Example: | ||
Example: | ||
```typescript | ||
@@ -111,7 +122,10 @@ const v1 = none(); // Maybe<unknown>.None | ||
#### `just` | ||
```typescript | ||
function just<T>(value: T): Maybe<T>; | ||
``` | ||
- Returns `Maybe` with `Just` state which contain value with `T` type. | ||
Example: | ||
Example: | ||
```typescript | ||
@@ -129,4 +143,6 @@ const v1 = just(2); // Maybe<number>.Just | ||
``` | ||
- Returns `Maybe` with `Just` state which contain value with `T` type. | ||
Example: | ||
Example: | ||
```typescript | ||
@@ -137,2 +153,15 @@ const v1 = from(2); // Maybe<number>.Just | ||
#### `fromNullable` | ||
```typescript | ||
function fromNullable<T>(value: T): Maybe<Exclude<T, null | undefined>>; | ||
``` | ||
- Returns `Maybe` with `Just` state which contain value with `T` type if value is not null or undefined and `None` otherwise. | ||
Example: | ||
```typescript | ||
const v1 = fromNullable(2); // Maybe<number>.Just | ||
``` | ||
#### `isMaybe` | ||
@@ -142,8 +171,10 @@ | ||
function isMaybe<T>(value: unknown | Maybe<T>): value is Maybe<T>; | ||
``` | ||
``` | ||
- Returns `boolean` if given `value` is instance of Maybe constructor. | ||
Example: | ||
Example: | ||
```typescript | ||
const value: unknown = 2; | ||
if (isMaybe(value)) { | ||
if (isMaybe(value)) { | ||
// ... value is Maybe<unknown> at this block | ||
@@ -154,7 +185,10 @@ } | ||
#### `Maybe#isNone` | ||
```typescript | ||
function isNone(): boolean; | ||
``` | ||
- Returns `true` if state of `Maybe` is `None` otherwise `false` | ||
Example: | ||
Example: | ||
```typescript | ||
@@ -164,12 +198,15 @@ const v1 = just(2); | ||
v1.isNone() // false | ||
v2.isNone() // true | ||
v1.isNone(); // false | ||
v2.isNone(); // true | ||
``` | ||
#### `Maybe#isJust` | ||
```typescript | ||
function isJust(): boolean; | ||
``` | ||
- Returns `true` if state of `Maybe` is `Just` otherwise `false` | ||
Example: | ||
Example: | ||
```typescript | ||
@@ -179,12 +216,15 @@ const v1 = just(2); | ||
v1.isJust() // true | ||
v2.isJust() // false | ||
v1.isJust(); // true | ||
v2.isJust(); // false | ||
``` | ||
#### `Maybe#or` | ||
```typescript | ||
function or<T>(x: Maybe<T>): Maybe<T>; | ||
``` | ||
- Returns `Maybe<T>`. If state of `this` is `Just` then `this` will be returned otherwise `x` argument will be returned | ||
Example: | ||
Example: | ||
```typescript | ||
@@ -196,19 +236,22 @@ const v1 = just(1); | ||
v1.or(v2) // v1 will be returned | ||
v2.or(v1) // v1 will be returned | ||
v2.or(v3) // v3 will be returned | ||
v1.or(v4) // v1 will be returned | ||
v1.or(v2); // v1 will be returned | ||
v2.or(v1); // v1 will be returned | ||
v2.or(v3); // v3 will be returned | ||
v1.or(v4); // v1 will be returned | ||
v2.or(v3).or(v1) // v1 will be returned | ||
v2.or(v1).or(v3) // v1 will be returned | ||
v1.or(v2).or(v3) // v1 will be returned | ||
v2.or(v3).or(v1); // v1 will be returned | ||
v2.or(v1).or(v3); // v1 will be returned | ||
v1.or(v2).or(v3); // v1 will be returned | ||
``` | ||
#### `Maybe#join` | ||
```typescript | ||
function join<V>(this: Maybe<Maybe<V>>): Maybe<V>; | ||
``` | ||
- `this: Maybe<Maybe<V>>` - `Maybe` instance which contains other `Maybe` instance as `Just` value. | ||
- Returns unwrapped `Maybe` - if current `Maybe` has `Just` state and inner `Maybe` has `Just` state then returns inner `Maybe` `Just`, otherwise returns `Maybe` `None`. | ||
Example: | ||
Example: | ||
```typescript | ||
@@ -219,13 +262,16 @@ const v1 = just(just(2)); | ||
v1.join() // Maybe.Just with value 2 | ||
v2.join() // Maybe.None without value | ||
v3.join() // Maybe.None without value | ||
v1.join(); // Maybe.Just with value 2 | ||
v2.join(); // Maybe.None without value | ||
v3.join(); // Maybe.None without value | ||
``` | ||
#### `Maybe#map` | ||
```typescript | ||
function map<Val, NewVal>(fn: (val: Val) => NewVal): Maybe<NewVal>; | ||
``` | ||
- Returns mapped by `fn` function value wrapped by `Maybe` if `Maybe` is `Just` otherwise `None` | ||
Example: | ||
Example: | ||
```typescript | ||
@@ -240,7 +286,10 @@ const v1 = just(2); | ||
##### `Maybe#asyncMap` | ||
```typescript | ||
function asyncMap<Val, NewVal>(fn: (val: Val) => Promise<NewVal>): Promise<Maybe<NewVal>>; | ||
``` | ||
- Returns `Promise` with mapped by `fn` function value wrapped by `Maybe` if `Maybe` is `Just` otherwise `None` | ||
Example: | ||
Example: | ||
```typescript | ||
@@ -251,3 +300,3 @@ const v1 = just(2); | ||
// Promise<Maybe<string>.Just> with value "2" | ||
const newVal1 = v1.asyncMap(a => Promise.resolve(a.toString())); | ||
const newVal1 = v1.asyncMap(a => Promise.resolve(a.toString())); | ||
// Promise<Maybe<string>.None> without value | ||
@@ -258,2 +307,3 @@ const newVal2 = v2.asyncMap(a => Promise.resolve(a.toString())); | ||
##### `Maybe#apply` | ||
```typescript | ||
@@ -263,6 +313,8 @@ function apply<A, B>(this: Maybe<(a: A) => B>, arg: Maybe<A>): Maybe<B>; | ||
``` | ||
- `this | fn` - function wrapped by Maybe, which should be applied to value `arg` | ||
- `arg | this` - value which should be applied to `fn` | ||
- Returns mapped by `fn` function value wrapped by `Maybe` if `Maybe` is `Just` otherwise `None` | ||
Example: | ||
Example: | ||
```typescript | ||
@@ -274,3 +326,3 @@ const v1 = just(2); | ||
const newVal1 = fn1.apply(v1); // Maybe<number>.Just with value 4 | ||
const newVal1 = fn1.apply(v1); // Maybe<number>.Just with value 4 | ||
const newVal2 = fn1.apply(v2); // Maybe<number>.None without value | ||
@@ -286,16 +338,21 @@ const newVal3 = fn2.apply(v1); // Maybe<number>.None without value | ||
```typescript | ||
function asyncApply<A, B>(this: Maybe<(a: Promise<A> | A) => Promise<B>>, arg: Maybe<Promise<A> | A>): Promise<Maybe<B>>; | ||
function asyncApply<A, B>( | ||
this: Maybe<(a: Promise<A> | A) => Promise<B>>, | ||
arg: Maybe<Promise<A> | A> | ||
): Promise<Maybe<B>>; | ||
function asyncApply<A, B>(this: Maybe<Promise<A> | A>, fn: Maybe<(a: Promise<A> | A) => Promise<B>>): Promise<Maybe<B>>; | ||
``` | ||
- `this | fn` - function wrapped by Maybe, which should be applied to value `arg` | ||
- `arg | this` - value which should be applied to `fn` | ||
- Returns `Promise` with mapped by `fn` function value wrapped by `Maybe` if `Maybe` is `Just` otherwise `None` | ||
Example: | ||
Example: | ||
```typescript | ||
const v1 = just(2); | ||
const v2 = none<number>(); | ||
const fn1 = just((a: number) => Promise,resolve(a * 2)); | ||
const fn1 = just((a: number) => Promise, resolve(a * 2)); | ||
const fn2 = none<(a: number) => Promise<number>>(); | ||
const newVal1 = fn1.apply(v1); // Promise<Maybe<number>.Just> with value 4 | ||
const newVal1 = fn1.apply(v1); // Promise<Maybe<number>.Just> with value 4 | ||
const newVal2 = fn1.apply(v2); // Promise<Maybe<number>.None> without value | ||
@@ -307,7 +364,10 @@ const newVal3 = fn2.apply(v1); // Promise<Maybe<number>.None> without value | ||
#### `Maybe#chain` | ||
```typescript | ||
function chain<Val, NewVal>(fn: (val: Val) => Maybe<NewVal>): Maybe<NewVal>; | ||
``` | ||
- Returns mapped by `fn` function value wrapped by `Maybe` if `Maybe` is `Just` and returned by `fn` value is `Just` too otherwise `None` | ||
Example: | ||
Example: | ||
```typescript | ||
@@ -324,7 +384,10 @@ const v1 = just(2); | ||
##### `Maybe#asyncChain` | ||
```typescript | ||
function asyncChain<Val, NewVal>(fn: (val: Val) => Promise<Maybe<NewVal>>): Promise<Maybe<NewVal>>; | ||
``` | ||
- Returns `Promise` with mapped by `fn` function value wrapped by `Maybe` if `Maybe` is `Just` otherwise `None` | ||
Example: | ||
Example: | ||
```typescript | ||
@@ -331,0 +394,0 @@ const v1 = just(2); |
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
28346
9
360
390
1
+ Added@sweet-monads/interfaces@2.3.0(transitive)
- Removed@sweet-monads/interfaces@1.3.1(transitive)