Skip to content

Commit 7c7120f

Browse files
committed
fix: add few helpers
1 parent 5b1dc3d commit 7c7120f

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ const user = getUser(1).map(({ email }) => email);
4848
- [`from`](#from)
4949
- [`fromPromise`](#fromPromise)
5050
- [`fromTry`](#fromTry)
51-
- [`from`](#from)
5251
- [`fromMaybe`](#frommaybe)
5352
- [`fromEither`](#fromeither)
5453
- [`isResult`](#isresult)

packages/ts-result/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lonli-lokli/ts-result",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"private": false,
55
"sideEffects": false,
66
"main": "./cjs/index.js",

packages/ts-result/src/lib/ts-result.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,19 +399,34 @@ class ResultConstructor<F, S, T extends ResultType = ResultType>
399399
);
400400
}
401401

402-
private static _initialInstance: ResultConstructor<never, never, ResultType.Initial>;
402+
private static _initialInstance: ResultConstructor<
403+
never,
404+
never,
405+
ResultType.Initial
406+
>;
403407
static initial(): Result<never, never> {
404408
if (ResultConstructor._initialInstance === undefined) {
405-
ResultConstructor._initialInstance = new ResultConstructor<never, never, ResultType.Initial>(ResultType.Initial, undefined);
409+
ResultConstructor._initialInstance = new ResultConstructor<
410+
never,
411+
never,
412+
ResultType.Initial
413+
>(ResultType.Initial, undefined);
406414
}
407415
return ResultConstructor._initialInstance;
408416
}
409417

410-
411-
private static _pendingInstance: ResultConstructor<never, never, ResultType.Pending>;
418+
private static _pendingInstance: ResultConstructor<
419+
never,
420+
never,
421+
ResultType.Pending
422+
>;
412423
static pending(): Result<never, never> {
413424
if (ResultConstructor._pendingInstance === undefined) {
414-
ResultConstructor._pendingInstance = new ResultConstructor<never, never, ResultType.Pending>(ResultType.Pending, undefined);
425+
ResultConstructor._pendingInstance = new ResultConstructor<
426+
never,
427+
never,
428+
ResultType.Pending
429+
>(ResultType.Pending, undefined);
415430
}
416431
return ResultConstructor._pendingInstance;
417432
}
@@ -650,7 +665,7 @@ class ResultConstructor<F, S, T extends ResultType = ResultType>
650665

651666
throw new Error('Result state is not Right');
652667
}
653-
668+
654669
unwrapOr(x: S): S {
655670
return this.isSuccess() ? this.value : x;
656671
}
@@ -690,3 +705,23 @@ export const pending = ResultConstructor.pending();
690705
export const isResult = <F, S>(
691706
value: unknown | Result<F, S>
692707
): value is Result<F, S> => value instanceof ResultConstructor;
708+
709+
export const isInitial = <F, S>(
710+
value: unknown | Result<F, S>
711+
): value is ResultConstructor<F, S, ResultType.Initial> =>
712+
isResult(value) && value.isInitial();
713+
714+
export const isPending = <F, S>(
715+
value: unknown | Result<F, S>
716+
): value is ResultConstructor<F, S, ResultType.Pending> =>
717+
isResult(value) && value.isPending();
718+
719+
export const isSuccess = <F, S>(
720+
value: unknown | Result<F, S>
721+
): value is ResultConstructor<F, S, ResultType.Success> =>
722+
isResult(value) && value.isSuccess();
723+
724+
export const isFailure = <F, S>(
725+
value: unknown | Result<F, S>
726+
): value is ResultConstructor<F, S, ResultType.Failure> =>
727+
isResult(value) && value.isFailure();

0 commit comments

Comments
 (0)