forked from jfmengels/node-elm-review
-
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.
refactor: make result even more elm-y
I started with uppercasing the `R`. Then, I replace `Err` with `Failure` so it doesn't get mixed up with native JS errors. I did the same with `Ok` and `Success`, so that the constructors line up. I tried uppercasing the constructors as well, to make it more elmish.* However, Eslint triggers `new-cap`, so I skipped that one and kept the Task terminology. Also, because objects are really inconvenient, I moved it to a folder and used evil barrel exports. * I tried to come up with a Fable pun, but I couldn't think of one.
- Loading branch information
Showing
7 changed files
with
74 additions
and
69 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
const Result = require('./result'); | ||
|
||
module.exports = { | ||
Result | ||
}; |
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,61 @@ | ||
/** | ||
* Port of `elm/core` primitives to TS. | ||
* Enables functional-style programming without having to pull in `Effect.TS`. | ||
*/ | ||
|
||
/** | ||
* @import {Fail, Success, Result} from './types/result'; | ||
*/ | ||
|
||
const {intoError} = require('../utils'); | ||
|
||
/** | ||
* Create a successful result. | ||
* | ||
* @template Value | ||
* | ||
* @param {Value} value | ||
* @returns {Success<Value>} | ||
*/ | ||
function succeed(value) { | ||
return {tag: 'ok', value}; | ||
} | ||
|
||
/** | ||
* Create a failed result. | ||
* | ||
* @template Failure | ||
* | ||
* @param {Failure} failure | ||
* @returns {Fail<Failure>} | ||
*/ | ||
function fail(failure) { | ||
return {tag: 'fail', failure}; | ||
} | ||
|
||
/** | ||
* Returns the value of a result, or throws if in an errored state. | ||
* | ||
* @remarks | ||
* Converts errors into {@linkcode Error}s before throwing. | ||
* For more details, see {@linkcode intoError}. | ||
* | ||
* @template Value | ||
* | ||
* @param {Result<unknown, Value>} value | ||
* @returns {Value} | ||
* @throws {Error} | ||
*/ | ||
function orThrow(value) { | ||
if (value.tag === 'ok') { | ||
return value.value; | ||
} | ||
|
||
throw intoError(value.failure); | ||
} | ||
|
||
module.exports = { | ||
succeed, | ||
fail, | ||
orThrow | ||
}; |
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,3 @@ | ||
export type Result<Error, Value> = Success<Value> | Fail<Error>; | ||
export type Success<Value> = {tag: 'ok'; value: Value}; | ||
export type Fail<Failure> = {tag: 'fail'; failure: Failure}; |
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
This file was deleted.
Oops, something went wrong.