Skip to content

Commit f236b56

Browse files
goswinrMaxime Mangel
andauthored
Add null check to tryParse boolean (#3783)
* add null check to tryParse boolean * try fix test * Update changelog --------- Co-authored-by: Maxime Mangel <[email protected]>
1 parent 2bb5b43 commit f236b56

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/Fable.Cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
* [Rust] Fixed generic interface implementation types (by @ncave)
1515
* [Rust] Fixed Dictionary constructor from IEnumerable (by @ncave)
1616
* [Rust] Fixed Seq.cast support for arrays and lists (by @ncave)
17+
* [GH-3783](https://github.com/fable-compiler/Fable/pull/3783) [JS/TS] `Boolean.tryParse` should not crash on `null` string (@goswinr)
1718

1819
### Added
1920

src/fable-library-ts/Boolean.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { FSharpRef } from "./Types.js"
22

33
export function tryParse(str: string, defValue: FSharpRef<boolean>): boolean {
4-
if (str.match(/^\s*true\s*$/i)) {
4+
if (str != null && str.match(/^\s*true\s*$/i)) {
55
defValue.contents = true;
66
return true;
7-
} else if (str.match(/^\s*false\s*$/i)) {
7+
} else if (str != null && str.match(/^\s*false\s*$/i)) {
88
defValue.contents = false;
99
return true;
1010
}

tests/Js/Main/ConvertTests.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,21 @@ let tests =
9090
throwsAnyError (fun () -> Boolean.Parse "falsee")
9191

9292
testCase "System.Boolean.TryParse works" <| fun () ->
93+
// expect parse success
9394
Boolean.TryParse "true" |> equal (true, true)
9495
Boolean.TryParse "True" |> equal (true, true)
9596
Boolean.TryParse " true " |> equal (true, true)
9697
Boolean.TryParse "false" |> equal (true, false)
9798
Boolean.TryParse "False" |> equal (true, false)
9899
Boolean.TryParse " false " |> equal (true, false)
99100

101+
// expect parse failure
100102
Boolean.TryParse "tru" |> equal (false, false)
101103
Boolean.TryParse "falsee" |> equal (false, false)
104+
Boolean.TryParse "0" |> equal (false, false)
105+
Boolean.TryParse "" |> equal (false, false)
106+
Boolean.TryParse "1" |> equal (false, false)
107+
Boolean.TryParse null |> equal (false, false)
102108

103109
testCase "System.SByte.Parse works" <| fun () ->
104110
SByte.Parse("5") |> equal 5y

0 commit comments

Comments
 (0)