File tree Expand file tree Collapse file tree 2 files changed +22
-7
lines changed
src/FsToolkit.ErrorHandling
tests/FsToolkit.ErrorHandling.Tests Expand file tree Collapse file tree 2 files changed +22
-7
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
5160let allTests = testList " Option Tests" [
5261 traverseResultTests
5362 tryParseTests
63+ ofResultTests
5464]
You can’t perform that action at this time.
0 commit comments