Skip to content

Commit

Permalink
refactor: better names
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexXanderGrib committed Jun 17, 2024
1 parent 1b805ed commit 0e27bba
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 4,147 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "monads-io",
"version": "3.0.0",
"version": "3.0.1",
"description": "🚀 Efficient Monads for JS: Maybe (Option) and Either (Result)",
"scripts": {
"test": "jest",
Expand Down
78 changes: 40 additions & 38 deletions src/either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import type {
MaybePromiseLike,
Pair,
Pm,
Mapper,
AnyParameters,
AsyncMonad,
Alternative,
Expand All @@ -29,6 +29,8 @@ const enum EitherType {
}

const name = "Either";
const rightName = "Right";
const leftName = "Left";

export function right<L = never, T = never>(right: T): Either<L, T> {
return Right.create(right);
Expand All @@ -52,7 +54,7 @@ class EitherConstructor<L, R>
}

tap<P extends AnyParameters>(
callback: Pm<R, void, P>,
callback: Mapper<R, void, P>,
...parameters: P
): Either<L, R> {
this.map(callback, ...parameters);
Expand Down Expand Up @@ -81,39 +83,39 @@ class EitherConstructor<L, R>
}

mapLeft<T, P extends AnyParameters>(
map: Pm<L, T, P>,
map: Mapper<L, T, P>,
...parameters: P
): Either<T, R> {
return this.biMap(bind(map, parameters), identity);
}

map<T, P extends AnyParameters>(
map: Pm<R, T, P>,
map: Mapper<R, T, P>,
...parameters: P
): Either<L, T> {
return this.mapRight(map, ...parameters);
}

mapRight<T, P extends AnyParameters>(
map: Pm<R, T, P>,
map: Mapper<R, T, P>,
...parameters: P
): Either<L, T> {
return this.biMap(identity, bind(map, parameters));
}

apply<A, B, P extends AnyParameters>(
this: Either<L, Pm<A, B, P>>,
this: Either<L, Mapper<A, B, P>>,
argument: Either<L, A>,
...parameters: P
): Either<L, B>;
apply<A, B, P extends AnyParameters>(
this: Either<L, A>,
map: Either<L, Pm<A, B, P>>,
map: Either<L, Mapper<A, B, P>>,
...parameters: P
): Either<L, B>;
apply<A, B, P extends AnyParameters>(
this: Either<L, A | Pm<A, B, P>>,
argument: Either<L, A | Pm<A, B, P>>,
this: Either<L, A | Mapper<A, B, P>>,
argument: Either<L, A | Mapper<A, B, P>>,
...parameters: P
): Either<L, B> {
return this.zip(argument).map(([current, argument]): B => {
Expand All @@ -132,18 +134,18 @@ class EitherConstructor<L, R>
}

asyncApply<A, B, P extends AnyParameters>(
this: Either<L, Pm<A, MaybePromiseLike<B>, P>>,
this: Either<L, Mapper<A, MaybePromiseLike<B>, P>>,
argument: Either<L, A>,
...parameters: P
): Promise<Either<L, B>>;
asyncApply<A, B, P extends AnyParameters>(
this: Either<L, A>,
map: Either<L, Pm<A, MaybePromiseLike<B>, P>>,
map: Either<L, Mapper<A, MaybePromiseLike<B>, P>>,
...parameters: P
): Promise<Either<L, B>>;
async asyncApply<A, B, P extends AnyParameters>(
this: Either<L, A | Pm<A, MaybePromiseLike<B>, P>>,
argument: Either<L, A | Pm<A, MaybePromiseLike<B>, P>>,
this: Either<L, A | Mapper<A, MaybePromiseLike<B>, P>>,
argument: Either<L, A | Mapper<A, MaybePromiseLike<B>, P>>,
...parameters: P
): Promise<Either<L, B>> {
return await this.zip(argument)
Expand All @@ -168,26 +170,26 @@ class EitherConstructor<L, R>
}

chain<A, B, P extends AnyParameters>(
map: Pm<R, Either<A, B>, P>,
map: Mapper<R, Either<A, B>, P>,
...parameters: P
): Either<A | L, B> {
return this.fold(left, bind(map, parameters));
}

biMap<A, B>(mapLeft: Pm<L, A>, mapRight: Pm<R, B>): Either<A, B> {
biMap<A, B>(mapLeft: Mapper<L, A>, mapRight: Mapper<R, B>): Either<A, B> {
return this.fold(combine(mapLeft, left), combine(mapRight, right));
}

async asyncChain<A, B, P extends AnyParameters>(
map: Pm<R, MaybePromiseLike<Either<A, B>>, P>,
map: Mapper<R, MaybePromiseLike<Either<A, B>>, P>,
...parameters: P
): Promise<Either<A | L, B>> {
const result = await this.asyncMap<L, Either<A, B>, P>(map, ...parameters);
return result.join();
}

async asyncMap<A, B, P extends AnyParameters>(
map: Pm<R, MaybePromiseLike<B>, P>,
map: Mapper<R, MaybePromiseLike<B>, P>,
...parameters: P
): Promise<Either<A | L, B>> {
return await this.map(map, ...parameters).await();
Expand All @@ -199,7 +201,7 @@ class EitherConstructor<L, R>
);
}

fold<A, B = A>(mapLeft: Pm<L, A>, mapRight: Pm<R, B>): A | B {
fold<A, B = A>(mapLeft: Mapper<L, A>, mapRight: Mapper<R, B>): A | B {
if (this.isLeft()) {
return mapLeft(this.left);
}
Expand Down Expand Up @@ -269,8 +271,8 @@ class Left<L, R> extends EitherConstructor<L, R> implements SerializedLeft<L> {
return new Left(left);
}

get [Symbol.toStringTag](): "Left" {
return "Left";
get [Symbol.toStringTag](): typeof leftName {
return leftName;
}

get name(): typeof name {
Expand Down Expand Up @@ -316,8 +318,8 @@ class Right<L, R>
return new Right(right);
}

get [Symbol.toStringTag](): "Right" {
return "Right";
get [Symbol.toStringTag](): typeof rightName {
return rightName;
}

get name(): typeof name {
Expand Down Expand Up @@ -386,7 +388,7 @@ export function chain<L, R, NL, NR, P extends AnyParameters>(
export function fromJSON<L, R>(
serialized: SerializedEither<L, R>
): Either<L, R> {
if (serialized.name !== "Either") {
if (serialized.name !== name) {
throw new DeserializationError(
DeserializationError.Messages.EXPECTED_EITHER
);
Expand Down Expand Up @@ -541,17 +543,22 @@ export function DecorateAsyncLegacy(): LegacyMethodDecorator {
};
}

function requireDecorationMethod(
context: ClassMemberDecoratorContext
): asserts context is ClassMethodDecoratorContext {
if (context.kind !== "method") {
throw new DecorationError();
}
}

export function Decorate(): ModernMethodDecorator<Either> {
/* istanbul ignore next */
return function decorate(
method: any,
context: ClassMethodDecoratorContext
context: ClassMemberDecoratorContext
): any {
/* istanbul ignore next */
if (context.kind !== "method") {
throw new DecorationError();
}

requireDecorationMethod(context);
return wrap(method);
};
}
Expand All @@ -560,13 +567,10 @@ export function DecorateAsync(): ModernMethodDecorator<Promise<Either>> {
/* istanbul ignore next */
return function decorate(
method: any,
context: ClassMethodDecoratorContext
context: ClassMemberDecoratorContext
): any {
/* istanbul ignore next */
if (context.kind !== "method") {
throw new DecorationError();
}

requireDecorationMethod(context);
return wrapAsync(method);
};
}
Expand Down Expand Up @@ -628,9 +632,7 @@ export async function fromTryAsync<L, T>(
export function fromPromiseSettledResult<L, T>(
result: PromiseSettledResult<T>
): Either<L, T> {
if (result.status === "fulfilled") {
return right(result.value);
}

return left(result.reason);
return result.status === "fulfilled"
? right(result.value)
: left(result.reason);
}
44 changes: 23 additions & 21 deletions src/maybe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
MaybePromiseLike,
Nullable,
AnyParameters,
Pm,
Mapper,
Pair,
Alternative,
Monad,
Expand All @@ -21,6 +21,8 @@ export const enum MaybeState {
}

const name = "Maybe";
const justName = "Just";
const noneName = "None";

export function none<T = never>(): Maybe<T> {
return None.create();
Expand Down Expand Up @@ -54,32 +56,32 @@ class MaybeConstructor<T> implements Monad<T>, Alternative<T>, Container<T> {
}

map<V, A extends AnyParameters>(
map: Pm<T, V, A>,
map: Mapper<T, V, A>,
...parameters: A
): Maybe<V> {
return this.chain(combine(bind(map, parameters), just));
}

mapNullable<V, A extends AnyParameters>(
map: Pm<T, V | null | undefined, A>,
map: Mapper<T, V | null | undefined, A>,
...parameters: A
): Maybe<V> {
return this.chain(combine(bind(map, parameters), fromNullable));
}

apply<A, B, P extends AnyParameters>(
this: Maybe<Pm<A, B, P>>,
this: Maybe<Mapper<A, B, P>>,
argument: Maybe<A>,
...parameters: P
): Maybe<B>;
apply<A, B, P extends AnyParameters>(
this: Maybe<A>,
argument: Maybe<Pm<A, B, P>>,
argument: Maybe<Mapper<A, B, P>>,
...parameters: P
): Maybe<B>;
apply<A, B, P extends AnyParameters>(
this: Maybe<A | Pm<A, B, P>>,
argument: Maybe<A | Pm<A, B, P>>,
this: Maybe<A | Mapper<A, B, P>>,
argument: Maybe<A | Mapper<A, B, P>>,
...parameters: P
): Maybe<B> {
return this.zip(argument).map(([current, argument]): B => {
Expand All @@ -104,7 +106,7 @@ class MaybeConstructor<T> implements Monad<T>, Alternative<T>, Container<T> {
}

chain<V, A extends AnyParameters>(
map: Pm<T, Maybe<V>, A>,
map: Mapper<T, Maybe<V>, A>,
...parameters: A
): Maybe<V> {
return this.fold<Maybe<V>>(bind(map, parameters), none);
Expand All @@ -131,7 +133,7 @@ class MaybeConstructor<T> implements Monad<T>, Alternative<T>, Container<T> {
}

tap<P extends AnyParameters>(
callback: Pm<T, void, P>,
callback: Mapper<T, void, P>,
...parameters: P
): Maybe<T> {
this.map(callback, ...parameters);
Expand All @@ -140,7 +142,7 @@ class MaybeConstructor<T> implements Monad<T>, Alternative<T>, Container<T> {
}

flatMap<V, P extends AnyParameters>(
map: Pm<T, V, P>,
map: Mapper<T, V, P>,
...parameters: P
): V | undefined {
return this.fold(bind(map, parameters), noop);
Expand All @@ -150,7 +152,7 @@ class MaybeConstructor<T> implements Monad<T>, Alternative<T>, Container<T> {
return this.fold(identity, () => UnwrapCustomError.inlineThrow(message));
}

fold<A, B = A>(mapJust: Pm<T, A>, mapNone: Pm<void, B>): A | B {
fold<A, B = A>(mapJust: Mapper<T, A>, mapNone: Mapper<void, B>): A | B {
if (this.isJust()) {
return mapJust(this.value);
}
Expand All @@ -164,15 +166,15 @@ class MaybeConstructor<T> implements Monad<T>, Alternative<T>, Container<T> {
}

async asyncChain<V, P extends AnyParameters>(
map: Pm<T, MaybePromiseLike<Maybe<V>>, P>,
map: Mapper<T, MaybePromiseLike<Maybe<V>>, P>,
...parameters: P
): Promise<Maybe<V>> {
const result = await this.map(map, ...parameters).await();
return result.join();
}

async asyncMap<A, P extends AnyParameters>(
map: Pm<T, MaybePromiseLike<A>, P>,
map: Mapper<T, MaybePromiseLike<A>, P>,
...parameters: P
): Promise<Maybe<A>> {
return await this.map(map, ...parameters).await();
Expand All @@ -186,18 +188,18 @@ class MaybeConstructor<T> implements Monad<T>, Alternative<T>, Container<T> {
}

asyncApply<A, B, P extends AnyParameters>(
this: Maybe<Pm<A, MaybePromiseLike<B>, P>>,
this: Maybe<Mapper<A, MaybePromiseLike<B>, P>>,
argument: Maybe<A>,
...parameters: P
): Promise<Maybe<B>>;
asyncApply<A, B, P extends AnyParameters>(
this: Maybe<A>,
map: Maybe<Pm<A, MaybePromiseLike<B>, P>>,
map: Maybe<Mapper<A, MaybePromiseLike<B>, P>>,
...parameters: P
): Promise<Maybe<B>>;
async asyncApply<A, B, P extends AnyParameters>(
this: Maybe<A | Pm<A, MaybePromiseLike<B>, P>>,
argument: Maybe<A | Pm<A, MaybePromiseLike<B>, P>>,
this: Maybe<A | Mapper<A, MaybePromiseLike<B>, P>>,
argument: Maybe<A | Mapper<A, MaybePromiseLike<B>, P>>,
...parameters: P
): Promise<Maybe<B>> {
return await this.zip(argument)
Expand Down Expand Up @@ -232,8 +234,8 @@ class Just<T> extends MaybeConstructor<T> implements SerializedJust<T> {
return new Just(value);
}

get [Symbol.toStringTag](): "Just" {
return "Just";
get [Symbol.toStringTag](): typeof justName {
return justName;
}

get name(): typeof name {
Expand Down Expand Up @@ -269,8 +271,8 @@ class None<T = unknown> extends MaybeConstructor<T> implements SerializedNone {
return undefined;
}

get [Symbol.toStringTag](): "None" {
return "None";
get [Symbol.toStringTag](): typeof noneName {
return noneName;
}

get name(): typeof name {
Expand Down
Loading

0 comments on commit 0e27bba

Please sign in to comment.