-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
parseBool
utility function (#39)
Resolves #8
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { parseBool } from "./boolean"; | ||
|
||
describe("boolean tests", () => { | ||
test.each([ | ||
// Test cases like in C# | ||
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/BooleanTests.cs | ||
[null as unknown as string, false], | ||
[undefined as unknown as string, false], | ||
[0 as unknown as string, false], | ||
[1 as unknown as string, false], | ||
[10 as unknown as string, false], | ||
[new Date() as unknown as string, false], | ||
["True", true], | ||
["true", true], | ||
["TRUE", true], | ||
["tRuE", true], | ||
[" True ", true], | ||
["True\0", true], | ||
[" \0 \0 True \0 ", true], | ||
["False", false], | ||
["false", false], | ||
["FALSE", false], | ||
["fAlSe", false], | ||
["False ", false], | ||
["False\0", false], | ||
[" False \0\0\0 ", false], | ||
["", false], | ||
[" ", false], | ||
["Garbage", false], | ||
["True\0Garbage", false], | ||
["True\0True", false], | ||
["True True", false], | ||
["True False", false], | ||
["False True", false], | ||
["Fa lse", false], | ||
["T", false], | ||
["0", false], | ||
["1", false], | ||
])('parseBool("%s")', (value, expected) => { | ||
expect(parseBool(value)).toBe(expected); | ||
}); | ||
}); |
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,12 @@ | ||
import { isNullOrEmpty } from "./string"; | ||
|
||
/** | ||
* Converts a string to a boolean | ||
* @param value The string to convert | ||
* @returns The parsed boolean | ||
*/ | ||
export function parseBool(value?: string): boolean { | ||
// Similar to C# implementation that trims also null characters | ||
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Boolean.cs#L277 | ||
return !isNullOrEmpty(value) && value?.replace(/[\s\0]/g, "").toLowerCase() === "true"; | ||
} |