Skip to content

Commit 18570b1

Browse files
njlrTheAngryByrd
authored andcommitted
* Adds Option.ofResult and some tests
1 parent c3dbb5f commit 18570b1

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/FsToolkit.ErrorHandling/Option.fs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Option =
88
| None -> Ok None
99
| Some v -> f v |> Result.map Some
1010

11-
let sequenceResult opt =
11+
let sequenceResult opt =
1212
traverseResult id opt
1313

1414
#if !FABLE_COMPILER
@@ -28,5 +28,10 @@ module Option =
2828
/// <returns></returns>
2929
let zip (option1 : option<'a>) (option2 : option<'b>) =
3030
match option1, option2 with
31-
| Some v1, Some v2 -> Some(v1,v2)
31+
| Some v1, Some v2 -> Some(v1,v2)
3232
| _ -> None
33+
34+
let ofResult =
35+
function
36+
| Ok v -> Some v
37+
| Error _ -> None

tests/FsToolkit.ErrorHandling.Tests/Option.fs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ let traverseResultTests =
1616
testCase "traverseResult with Some of valid data" <| fun _ ->
1717
let (latitude, longitude) =
1818
(Some lat), (Some lng)
19-
20-
latitude
19+
20+
latitude
2121
|> Option.traverseResult Latitude.TryCreate
2222
|> Expect.hasOkValue (Some validLat)
23-
24-
longitude
23+
24+
longitude
2525
|> Option.traverseResult Longitude.TryCreate
2626
|> Expect.hasOkValue (Some validLng)
2727
]
@@ -46,9 +46,19 @@ let tryParseTests =
4646
Expect.equal parsedValue (Some expectedGuid) "Should be same guid"
4747
#endif
4848
]
49-
49+
50+
51+
let ofResultTests =
52+
testList "Option.ofResult Tests" [
53+
testCase "ofResult simple cases" <| fun _ ->
54+
Expect.equal (Option.ofResult (Ok 123)) (Some 123) "Ok int"
55+
Expect.equal (Option.ofResult (Ok "abc")) (Some "abc") "Ok string"
56+
Expect.equal (Option.ofResult (Error "x")) None "Error _"
57+
]
58+
5059

5160
let allTests = testList "Option Tests" [
5261
traverseResultTests
5362
tryParseTests
63+
ofResultTests
5464
]

0 commit comments

Comments
 (0)