Skip to content

Commit a6eb4bc

Browse files
AlbertoDePenaAlberto De Pena
andauthored
Added Option.ofPair and ValueOption.ofPair #207 (#208)
Co-authored-by: Alberto De Pena <[email protected]>
1 parent aa87d3d commit a6eb4bc

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

src/FsToolkit.ErrorHandling/Option.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,8 @@ module Option =
138138
match input with
139139
| Some x -> onSome x
140140
| None -> onNone ()
141+
142+
let ofPair (input: bool * 'a) =
143+
match input with
144+
| true, x -> Some x
145+
| false, _ -> None

src/FsToolkit.ErrorHandling/ValueOption.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,9 @@ module ValueOption =
143143
| ValueSome x -> onSome x
144144
| ValueNone -> onNone ()
145145

146+
let inline ofPair (input: bool * 'a) =
147+
match input with
148+
| true, x -> ValueSome x
149+
| false, _ -> ValueNone
150+
146151
#endif

tests/FsToolkit.ErrorHandling.Tests/Option.fs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,51 @@ let eitherTests =
127127
Expect.equal (Option.either add2 f value1) 42 ""
128128
]
129129

130+
let ofPairTests =
131+
testList "Option.ofPair Tests" [
132+
testCase "Int32.TryParse => Some Int32"
133+
<| fun _ ->
134+
let input = "1989"
135+
let pair = Int32.TryParse input
136+
Expect.equal (Option.ofPair pair) (Some 1989) ""
137+
testCase "Int32.TryParse => None"
138+
<| fun _ ->
139+
let input = "FsToolkit.ErrorHandling"
140+
let pair = Int32.TryParse input
141+
Expect.equal (Option.ofPair pair) (None) ""
142+
testCase "Int64.TryParse => Some Int64"
143+
<| fun _ ->
144+
let input = "1989"
145+
let pair = Int64.TryParse input
146+
Expect.equal (Option.ofPair pair) (Some 1989L) ""
147+
testCase "Int64.TryParse => None"
148+
<| fun _ ->
149+
let input = "FsToolkit.ErrorHandling"
150+
let pair = Int64.TryParse input
151+
Expect.equal (Option.ofPair pair) (None) ""
152+
testCase "Decimal.TryParse => Some Decimal"
153+
<| fun _ ->
154+
let input = "1989"
155+
let pair = Decimal.TryParse input
156+
Expect.equal (Option.ofPair pair) (Some 1989M) ""
157+
testCase "Decimal.TryParse => None"
158+
<| fun _ ->
159+
let input = "FsToolkit.ErrorHandling"
160+
let pair = Decimal.TryParse input
161+
Expect.equal (Option.ofPair pair) (None) ""
162+
testCase "Guid.TryParse => Some Guid"
163+
<| fun _ ->
164+
let guid = Guid.NewGuid()
165+
let input = guid.ToString()
166+
let pair = Guid.TryParse input
167+
Expect.equal (Option.ofPair pair) (Some guid) ""
168+
testCase "Guid.TryParse => None"
169+
<| fun _ ->
170+
let input = "FsToolkit.ErrorHandling"
171+
let pair = Guid.TryParse input
172+
Expect.equal (Option.ofPair pair) (None) ""
173+
]
174+
130175
let allTests =
131176
testList "Option Tests" [
132177
traverseResultTests
@@ -136,4 +181,5 @@ let allTests =
136181
ofNullTests
137182
bindNullTests
138183
eitherTests
184+
ofPairTests
139185
]

tests/FsToolkit.ErrorHandling.Tests/ValueOption.fs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,51 @@ let eitherTests =
131131
Expect.equal (ValueOption.either add2 f value1) 42 ""
132132
]
133133

134+
let ofPairTests =
135+
testList "ValueOption.ofPair Tests" [
136+
testCase "Int32.TryParse => ValueSome Int32"
137+
<| fun _ ->
138+
let input = "1989"
139+
let pair = Int32.TryParse input
140+
Expect.equal (ValueOption.ofPair pair) (ValueSome 1989) ""
141+
testCase "Int32.TryParse => ValueNone"
142+
<| fun _ ->
143+
let input = "FsToolkit.ErrorHandling"
144+
let pair = Int32.TryParse input
145+
Expect.equal (ValueOption.ofPair pair) (ValueNone) ""
146+
testCase "Int64.TryParse => ValueSome Int64"
147+
<| fun _ ->
148+
let input = "1989"
149+
let pair = Int64.TryParse input
150+
Expect.equal (ValueOption.ofPair pair) (ValueSome 1989L) ""
151+
testCase "Int64.TryParse => ValueNone"
152+
<| fun _ ->
153+
let input = "FsToolkit.ErrorHandling"
154+
let pair = Int64.TryParse input
155+
Expect.equal (ValueOption.ofPair pair) (ValueNone) ""
156+
testCase "Decimal.TryParse => ValueSome Decimal"
157+
<| fun _ ->
158+
let input = "1989"
159+
let pair = Decimal.TryParse input
160+
Expect.equal (ValueOption.ofPair pair) (ValueSome 1989M) ""
161+
testCase "Decimal.TryParse => ValueNone"
162+
<| fun _ ->
163+
let input = "FsToolkit.ErrorHandling"
164+
let pair = Decimal.TryParse input
165+
Expect.equal (ValueOption.ofPair pair) (ValueNone) ""
166+
testCase "Guid.TryParse => ValueSome Guid"
167+
<| fun _ ->
168+
let guid = Guid.NewGuid()
169+
let input = guid.ToString()
170+
let pair = Guid.TryParse input
171+
Expect.equal (ValueOption.ofPair pair) (ValueSome guid) ""
172+
testCase "Guid.TryParse => ValueNone"
173+
<| fun _ ->
174+
let input = "FsToolkit.ErrorHandling"
175+
let pair = Guid.TryParse input
176+
Expect.equal (ValueOption.ofPair pair) (ValueNone) ""
177+
]
178+
134179
let allTests =
135180
testList "ValueOption Tests" [
136181
traverseResultTests
@@ -140,6 +185,7 @@ let allTests =
140185
ofNullTests
141186
bindNullTests
142187
eitherTests
188+
ofPairTests
143189
]
144190
#else
145191

0 commit comments

Comments
 (0)