New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@sweet-monads/either

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sweet-monads/either - npm Package Compare versions

Comparing version 3.1.0 to 3.2.0

11

cjs/index.d.ts

@@ -112,2 +112,4 @@ import type { AsyncMonad, Alternative, Container } from "@sweet-monads/interfaces";

static from<T>(v: T): Either<never, T>;
static fromPromise<L, R>(promise: Promise<R>): Promise<Either<L, R>>;
static fromTry<L, R>(fn: () => R): Either<L, R>;
static right<L = never, T = never>(v: T): Either<L, T>;

@@ -130,7 +132,10 @@ static left<T = never, R = never>(v: T): Either<T, R>;

or(x: Either<L, R>): Either<L, R>;
unwrap(): R;
unwrap(errorFactory?: (x: L) => unknown): R;
unwrapOr(x: R): R;
unwrapOrElse(f: (l: L) => R): R;
get [Symbol.toStringTag](): string;
}
export declare type Either<L, R> = EitherConstructor<L, R, EitherType.Right> | EitherConstructor<L, R, EitherType.Left>;
export declare const merge: typeof EitherConstructor.mergeInOne, mergeInOne: typeof EitherConstructor.mergeInOne, mergeInMany: typeof EitherConstructor.mergeInMany, left: typeof EitherConstructor.left, right: typeof EitherConstructor.right, from: typeof EitherConstructor.from, chain: typeof EitherConstructor.chain;
export type Either<L, R> = EitherConstructor<L, R, EitherType.Right> | EitherConstructor<L, R, EitherType.Left>;
export declare const merge: typeof EitherConstructor.mergeInOne, mergeInOne: typeof EitherConstructor.mergeInOne, mergeInMany: typeof EitherConstructor.mergeInMany, left: typeof EitherConstructor.left, right: typeof EitherConstructor.right, from: typeof EitherConstructor.from, fromTry: typeof EitherConstructor.fromTry, fromPromise: typeof EitherConstructor.fromPromise, chain: typeof EitherConstructor.chain;
export declare const isEither: <L, R>(value: unknown) => value is Either<L, R>;
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isEither = exports.chain = exports.from = exports.right = exports.left = exports.mergeInMany = exports.mergeInOne = exports.merge = void 0;
exports.isEither = exports.chain = exports.fromPromise = exports.fromTry = exports.from = exports.right = exports.left = exports.mergeInMany = exports.mergeInOne = exports.merge = void 0;
var EitherType;

@@ -13,6 +13,2 @@ (function (EitherType) {

class EitherConstructor {
constructor(type, value) {
this.type = type;
this.value = value;
}
static chain(f) {

@@ -37,2 +33,13 @@ return (m) => m.asyncChain(f);

}
static fromPromise(promise) {
return promise.then(EitherConstructor.right).catch(EitherConstructor.left);
}
static fromTry(fn) {
try {
return EitherConstructor.right(fn());
}
catch (e) {
return EitherConstructor.left(e);
}
}
static right(v) {

@@ -44,2 +51,6 @@ return new EitherConstructor("Right", v);

}
constructor(type, value) {
this.type = type;
this.value = value;
}
isLeft() {

@@ -122,11 +133,20 @@ return this.type === "Left";

}
unwrap() {
unwrap(errorFactory = () => new Error("Either state is Left")) {
if (this.isRight())
return this.value;
throw new Error("Either state is Left");
throw errorFactory(this.value);
}
unwrapOr(x) {
return this.isRight() ? this.value : x;
}
unwrapOrElse(f) {
return this.isRight() ? this.value : f(this.value);
}
get [Symbol.toStringTag]() {
return "Either";
}
}
EitherConstructor.merge = EitherConstructor.mergeInOne;
exports.merge = EitherConstructor.merge, exports.mergeInOne = EitherConstructor.mergeInOne, exports.mergeInMany = EitherConstructor.mergeInMany, exports.left = EitherConstructor.left, exports.right = EitherConstructor.right, exports.from = EitherConstructor.from, exports.chain = EitherConstructor.chain;
exports.merge = EitherConstructor.merge, exports.mergeInOne = EitherConstructor.mergeInOne, exports.mergeInMany = EitherConstructor.mergeInMany, exports.left = EitherConstructor.left, exports.right = EitherConstructor.right, exports.from = EitherConstructor.from, exports.fromTry = EitherConstructor.fromTry, exports.fromPromise = EitherConstructor.fromPromise, exports.chain = EitherConstructor.chain;
const isEither = (value) => value instanceof EitherConstructor;
exports.isEither = isEither;

@@ -112,2 +112,4 @@ import type { AsyncMonad, Alternative, Container } from "@sweet-monads/interfaces";

static from<T>(v: T): Either<never, T>;
static fromPromise<L, R>(promise: Promise<R>): Promise<Either<L, R>>;
static fromTry<L, R>(fn: () => R): Either<L, R>;
static right<L = never, T = never>(v: T): Either<L, T>;

@@ -130,7 +132,10 @@ static left<T = never, R = never>(v: T): Either<T, R>;

or(x: Either<L, R>): Either<L, R>;
unwrap(): R;
unwrap(errorFactory?: (x: L) => unknown): R;
unwrapOr(x: R): R;
unwrapOrElse(f: (l: L) => R): R;
get [Symbol.toStringTag](): string;
}
export declare type Either<L, R> = EitherConstructor<L, R, EitherType.Right> | EitherConstructor<L, R, EitherType.Left>;
export declare const merge: typeof EitherConstructor.mergeInOne, mergeInOne: typeof EitherConstructor.mergeInOne, mergeInMany: typeof EitherConstructor.mergeInMany, left: typeof EitherConstructor.left, right: typeof EitherConstructor.right, from: typeof EitherConstructor.from, chain: typeof EitherConstructor.chain;
export type Either<L, R> = EitherConstructor<L, R, EitherType.Right> | EitherConstructor<L, R, EitherType.Left>;
export declare const merge: typeof EitherConstructor.mergeInOne, mergeInOne: typeof EitherConstructor.mergeInOne, mergeInMany: typeof EitherConstructor.mergeInMany, left: typeof EitherConstructor.left, right: typeof EitherConstructor.right, from: typeof EitherConstructor.from, fromTry: typeof EitherConstructor.fromTry, fromPromise: typeof EitherConstructor.fromPromise, chain: typeof EitherConstructor.chain;
export declare const isEither: <L, R>(value: unknown) => value is Either<L, R>;
export {};

@@ -10,6 +10,2 @@ var EitherType;

class EitherConstructor {
constructor(type, value) {
this.type = type;
this.value = value;
}
static chain(f) {

@@ -34,2 +30,13 @@ return (m) => m.asyncChain(f);

}
static fromPromise(promise) {
return promise.then(EitherConstructor.right).catch(EitherConstructor.left);
}
static fromTry(fn) {
try {
return EitherConstructor.right(fn());
}
catch (e) {
return EitherConstructor.left(e);
}
}
static right(v) {

@@ -41,2 +48,6 @@ return new EitherConstructor("Right", v);

}
constructor(type, value) {
this.type = type;
this.value = value;
}
isLeft() {

@@ -119,10 +130,19 @@ return this.type === "Left";

}
unwrap() {
unwrap(errorFactory = () => new Error("Either state is Left")) {
if (this.isRight())
return this.value;
throw new Error("Either state is Left");
throw errorFactory(this.value);
}
unwrapOr(x) {
return this.isRight() ? this.value : x;
}
unwrapOrElse(f) {
return this.isRight() ? this.value : f(this.value);
}
get [Symbol.toStringTag]() {
return "Either";
}
}
EitherConstructor.merge = EitherConstructor.mergeInOne;
export const { merge, mergeInOne, mergeInMany, left, right, from, chain } = EitherConstructor;
export const { merge, mergeInOne, mergeInMany, left, right, from, fromTry, fromPromise, chain } = EitherConstructor;
export const isEither = (value) => value instanceof EitherConstructor;

@@ -112,2 +112,4 @@ import type { AsyncMonad, Alternative, Container } from "@sweet-monads/interfaces";

static from<T>(v: T): Either<never, T>;
static fromPromise<L, R>(promise: Promise<R>): Promise<Either<L, R>>;
static fromTry<L, R>(fn: () => R): Either<L, R>;
static right<L = never, T = never>(v: T): Either<L, T>;

@@ -130,7 +132,10 @@ static left<T = never, R = never>(v: T): Either<T, R>;

or(x: Either<L, R>): Either<L, R>;
unwrap(): R;
unwrap(errorFactory?: (x: L) => unknown): R;
unwrapOr(x: R): R;
unwrapOrElse(f: (l: L) => R): R;
get [Symbol.toStringTag](): string;
}
export declare type Either<L, R> = EitherConstructor<L, R, EitherType.Right> | EitherConstructor<L, R, EitherType.Left>;
export declare const merge: typeof EitherConstructor.mergeInOne, mergeInOne: typeof EitherConstructor.mergeInOne, mergeInMany: typeof EitherConstructor.mergeInMany, left: typeof EitherConstructor.left, right: typeof EitherConstructor.right, from: typeof EitherConstructor.from, chain: typeof EitherConstructor.chain;
export type Either<L, R> = EitherConstructor<L, R, EitherType.Right> | EitherConstructor<L, R, EitherType.Left>;
export declare const merge: typeof EitherConstructor.mergeInOne, mergeInOne: typeof EitherConstructor.mergeInOne, mergeInMany: typeof EitherConstructor.mergeInMany, left: typeof EitherConstructor.left, right: typeof EitherConstructor.right, from: typeof EitherConstructor.from, fromTry: typeof EitherConstructor.fromTry, fromPromise: typeof EitherConstructor.fromPromise, chain: typeof EitherConstructor.chain;
export declare const isEither: <L, R>(value: unknown) => value is Either<L, R>;
export {};
{
"name": "@sweet-monads/either",
"version": "3.1.0",
"version": "3.2.0",
"description": "Either monad",

@@ -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",
"scripts": {

@@ -33,6 +33,6 @@ "build": "run-s build:pre build:all build:after",

"dependencies": {
"@sweet-monads/interfaces": "^3.1.0"
"@sweet-monads/interfaces": "^3.2.0"
},
"author": "",
"license": "ISC"
"author": "JSMonk",
"license": "MIT"
}

@@ -46,2 +46,4 @@ # @sweet-monads/either

- [`from`](#from)
- [`fromTry`](#fromtry)
- [`fromPromise`](#frompromise)
- [`isEither`](#iseither)

@@ -69,3 +71,3 @@ - [`Either#isLeft`](#eitherisleft)

- `fn: (v: R) => Promise<Either<NL, NR>>` - function which should be applied asynchronously to `Either<L, R>` value
- Returns function with `Either<L, R>` argument and promisied `Either` with new error or maped by `fn` value (could be used inside `Promise#then` function).
- Returns function with `Either<L, R>` argument and promised `Either` with new error or mapped by `fn` value (could be used inside `Promise#then` function).

@@ -147,3 +149,3 @@ Example:

- `values: Array<Either<L, R>>` - Array of Either values which will be merged into Either of Array
- Returns `Either<Array<L>, Array<R>>` which will contain `Right<Array<R>>` if all of array elements was `Right<R>` otherwise array of all catched `Left<L>` values.
- Returns `Either<Array<L>, Array<R>>` which will contain `Right<Array<R>>` if all of array elements was `Right<R>` otherwise array of all caught `Left<L>` values.

@@ -206,2 +208,30 @@ Example:

#### `fromTry`
Returns `Right` with function result or `Left` if function execution throws an error.
```typescript
function fromTry<L, R>(fn: () => R): Either<L, R>;
```
```typescript
fromTry(() => 2); // Either<never, number>.Right
fromTry(() => {
throw new Error("test");
}); // Either<Error, never>.Left
```
#### `fromPromise`
Returns `Right` with the promise value if the provided promise fulfilled or `Left` with the error value if the provided promise rejected.
```typescript
function fromPromise<L, R>(promise: Promise<R>): Promise<Either<L, R>>;
```
```typescript
fromPromise(Promise.resolve(2)); // Either<never, number>.Right
fromPromise(Promise.reject(new Error("test"))); // Either<Error, never>.Left
```
#### `isEither`

@@ -485,5 +515,13 @@

```typescript
right(2).unwrap() // number
left(new TypeError()).unwrap() // throws value (TypeError)
left(2).unwrap() // throws 2 (don't do this)
right(2).unwrap(); // number
left(new TypeError()).unwrap(); // throws error
right(2).unwrap((; // number
left(new TypeError()).unwrap(x => x); // throws TypeError provied in arguments
left(2).unwrapOr(3) // returns 3
rigth(2).unwrapOr(3) // returns 2
left(2).unwrapOrElse(num => num * 2) // returns 4
right(2).unwrapOrElse(num => num * 2) // returns 2
```

@@ -490,0 +528,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc