Skip to content

Commit

Permalink
fix: add few helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonli-Lokli committed Apr 3, 2023
1 parent 5b1dc3d commit 7c7120f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const user = getUser(1).map(({ email }) => email);
- [`from`](#from)
- [`fromPromise`](#fromPromise)
- [`fromTry`](#fromTry)
- [`from`](#from)
- [`fromMaybe`](#frommaybe)
- [`fromEither`](#fromeither)
- [`isResult`](#isresult)
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-result/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lonli-lokli/ts-result",
"version": "2.0.0",
"version": "2.0.1",
"private": false,
"sideEffects": false,
"main": "./cjs/index.js",
Expand Down
47 changes: 41 additions & 6 deletions packages/ts-result/src/lib/ts-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,19 +399,34 @@ class ResultConstructor<F, S, T extends ResultType = ResultType>
);
}

private static _initialInstance: ResultConstructor<never, never, ResultType.Initial>;
private static _initialInstance: ResultConstructor<
never,
never,
ResultType.Initial
>;
static initial(): Result<never, never> {
if (ResultConstructor._initialInstance === undefined) {
ResultConstructor._initialInstance = new ResultConstructor<never, never, ResultType.Initial>(ResultType.Initial, undefined);
ResultConstructor._initialInstance = new ResultConstructor<
never,
never,
ResultType.Initial
>(ResultType.Initial, undefined);
}
return ResultConstructor._initialInstance;
}


private static _pendingInstance: ResultConstructor<never, never, ResultType.Pending>;
private static _pendingInstance: ResultConstructor<
never,
never,
ResultType.Pending
>;
static pending(): Result<never, never> {
if (ResultConstructor._pendingInstance === undefined) {
ResultConstructor._pendingInstance = new ResultConstructor<never, never, ResultType.Pending>(ResultType.Pending, undefined);
ResultConstructor._pendingInstance = new ResultConstructor<
never,
never,
ResultType.Pending
>(ResultType.Pending, undefined);
}
return ResultConstructor._pendingInstance;
}
Expand Down Expand Up @@ -650,7 +665,7 @@ class ResultConstructor<F, S, T extends ResultType = ResultType>

throw new Error('Result state is not Right');
}

unwrapOr(x: S): S {
return this.isSuccess() ? this.value : x;
}
Expand Down Expand Up @@ -690,3 +705,23 @@ export const pending = ResultConstructor.pending();
export const isResult = <F, S>(
value: unknown | Result<F, S>
): value is Result<F, S> => value instanceof ResultConstructor;

export const isInitial = <F, S>(
value: unknown | Result<F, S>
): value is ResultConstructor<F, S, ResultType.Initial> =>
isResult(value) && value.isInitial();

export const isPending = <F, S>(
value: unknown | Result<F, S>
): value is ResultConstructor<F, S, ResultType.Pending> =>
isResult(value) && value.isPending();

export const isSuccess = <F, S>(
value: unknown | Result<F, S>
): value is ResultConstructor<F, S, ResultType.Success> =>
isResult(value) && value.isSuccess();

export const isFailure = <F, S>(
value: unknown | Result<F, S>
): value is ResultConstructor<F, S, ResultType.Failure> =>
isResult(value) && value.isFailure();

0 comments on commit 7c7120f

Please sign in to comment.