generated from ryansonshine/typescript-npm-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add maybify
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
failureMaybe, | ||
IfMaybe, | ||
isFailureMaybe, | ||
isSuccessMaybe, | ||
Maybe, | ||
successMaybe, | ||
} from './types'; | ||
|
||
/** | ||
* Converts a function that may throw into a function that returns a maybe object. | ||
* @param fn | ||
* @returns | ||
*/ | ||
export function maybify<TIn extends any[], TOut>(fn: (...input: TIn) => Promise<TOut>): (...input: TIn) => Promise<IfMaybe<TOut, TOut, Maybe<TOut>>> { | ||
// TODO: this should gracefully handle Synchronous functions as well. | ||
//@ts-ignore | ||
return async (...input: TIn) => { | ||
try { | ||
const result = await fn(...input); | ||
|
||
// If this is already a maybe, return it as-is. | ||
if (isSuccessMaybe(result) || isFailureMaybe(result)) { | ||
return result; | ||
} | ||
// If this wasn't a maybe, return a success maybe. | ||
else { | ||
return successMaybe(result); | ||
} | ||
} catch (error: any) { | ||
// If we encounter any errors, return a failure maybe. | ||
return failureMaybe(error); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import _ from 'lodash'; | ||
|
||
import { maybify } from '../src/functions/maybify'; | ||
import { Maybe } from '../src/functions/types'; | ||
|
||
describe('maybify', () => { | ||
it('Should handle an already-maybed function correctly.', (): any => { | ||
const fn = async (input: number): Promise<Maybe<number>> => { | ||
return { | ||
success: true, | ||
data: input + 1 | ||
} | ||
} | ||
|
||
const result = maybify(fn)(5); | ||
|
||
expect(result).resolves.toEqual({ success: true, data: 6, error: undefined }); | ||
}) | ||
|
||
it('Should handle a function that returns a value correctly.', (): any => { | ||
const fn = async (input: number) => { | ||
return input + 1; | ||
}; | ||
|
||
const result = maybify(fn)(5); | ||
|
||
expect(result).resolves.toEqual({ success: true, data: 6, error: undefined }); | ||
}) | ||
}) |