Skip to content

Commit 2e189c9

Browse files
committed
empty strings numberify to zero
1 parent ca1fba7 commit 2e189c9

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

JsonLogic.Tests/Files/more-tests.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
[ {"<" : ["123", "456", "789"]}, {}, true ],
4747
[ {"<" : ["123", 456, "789"]}, {}, true ],
4848
[ {"<" : ["123a", 456, "789"]}, {}, false ],
49+
[ {"<" : ["", "4", 5]}, {}, true ],
4950
[ {"cat" : ["abc", [123,456]]}, {}, "abc123,456" ],
5051
[ {"in" : [123, "abcd"]}, {}, false ],
5152
[ {"in" : ["bcd", 123]}, {}, false ],

JsonLogic/JsonLogic.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
<PackageTags>json logic json-logic jsonlogic</PackageTags>
1616
<PackageReleaseNotes>Release notes can be found on [GitHub](https://github.com/gregsdennis/json-everything/blob/master/json-everything.net/wwwroot/md/release-notes/json-logic.md) and https://json-everything.net/json-logic</PackageReleaseNotes>
1717
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
18-
<Version>4.0.0</Version>
18+
<Version>4.0.1</Version>
1919
<AssemblyVersion>4.0.0.0</AssemblyVersion>
20-
<FileVersion>4.0.0.0</FileVersion>
20+
<FileVersion>4.0.1.0</FileVersion>
2121
<DocumentationFile>JsonLogic.xml</DocumentationFile>
2222
<IncludeSymbols>true</IncludeSymbols>
2323
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

JsonLogic/JsonNodeExtensions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public static bool IsTruthy(this JsonNode? node)
8282
/// <returns>
8383
/// A string representation of the value as follows:
8484
///
85-
/// - strings try to parse a number from the value
85+
/// - empty string returns 0
86+
/// - all other strings try to parse a number from the value
8687
/// - true returns 1
8788
/// - false returns 0
8889
/// - numbers are unchanged
@@ -94,7 +95,12 @@ public static bool IsTruthy(this JsonNode? node)
9495

9596
if (node is not JsonValue value) return null;
9697

97-
if (value.TryGetValue(out string? s)) return decimal.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var d) ? d : null;
98+
if (value.TryGetValue(out string? s))
99+
return s == string.Empty
100+
? 0
101+
: decimal.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var d)
102+
? d
103+
: null;
98104

99105
if (value.TryGetValue(out bool b)) return b ? 1 : 0;
100106

json-everything.net/wwwroot/md/release-notes/json-logic.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 4.0.1 (no PR) {#release-logic-4.0.0}
2+
3+
Empty strings "numberify" to 0.
4+
15
# [4.0.0](https://github.com/gregsdennis/json-everything/pull/395) {#release-logic-4.0.0}
26

37
[#377](https://github.com/gregsdennis/json-everything/issues/377) - Alignment with JS version available online at https://jsonlogic.com/.

0 commit comments

Comments
 (0)