File tree Expand file tree Collapse file tree 3 files changed +56
-0
lines changed
Expand file tree Collapse file tree 3 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
88## [ Unreleased]
99
10+ - added ` parseBool ` utility function
11+
1012## [ 0.6.0] - 2024-06-26
1113
1214### dependabot: \# 37 Bump braces from 3.0.2 to 3.0.3
Original file line number Diff line number Diff line change 1+ import { parseBool } from "./boolean" ;
2+
3+ describe ( "boolean tests" , ( ) => {
4+ test . each ( [
5+ // Test cases like in C#
6+ // https://github.com/dotnet/runtime/blob/main/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/BooleanTests.cs
7+ [ null as unknown as string , false ] ,
8+ [ undefined as unknown as string , false ] ,
9+ [ 0 as unknown as string , false ] ,
10+ [ 1 as unknown as string , false ] ,
11+ [ 10 as unknown as string , false ] ,
12+ [ new Date ( ) as unknown as string , false ] ,
13+ [ "True" , true ] ,
14+ [ "true" , true ] ,
15+ [ "TRUE" , true ] ,
16+ [ "tRuE" , true ] ,
17+ [ " True " , true ] ,
18+ [ "True\0" , true ] ,
19+ [ " \0 \0 True \0 " , true ] ,
20+ [ "False" , false ] ,
21+ [ "false" , false ] ,
22+ [ "FALSE" , false ] ,
23+ [ "fAlSe" , false ] ,
24+ [ "False " , false ] ,
25+ [ "False\0" , false ] ,
26+ [ " False \0\0\0 " , false ] ,
27+ [ "" , false ] ,
28+ [ " " , false ] ,
29+ [ "Garbage" , false ] ,
30+ [ "True\0Garbage" , false ] ,
31+ [ "True\0True" , false ] ,
32+ [ "True True" , false ] ,
33+ [ "True False" , false ] ,
34+ [ "False True" , false ] ,
35+ [ "Fa lse" , false ] ,
36+ [ "T" , false ] ,
37+ [ "0" , false ] ,
38+ [ "1" , false ] ,
39+ ] ) ( 'parseBool("%s")' , ( value , expected ) => {
40+ expect ( parseBool ( value ) ) . toBe ( expected ) ;
41+ } ) ;
42+ } ) ;
Original file line number Diff line number Diff line change 1+ import { isNullOrEmpty } from "./string" ;
2+
3+ /**
4+ * Converts a string to a boolean
5+ * @param value The string to convert
6+ * @returns The parsed boolean
7+ */
8+ export function parseBool ( value ?: string ) : boolean {
9+ // Similar to C# implementation that trims also null characters
10+ // https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Boolean.cs#L277
11+ return ! isNullOrEmpty ( value ) && value ?. replace ( / [ \s \0 ] / g, "" ) . toLowerCase ( ) === "true" ;
12+ }
You can’t perform that action at this time.
0 commit comments