Skip to content

Commit

Permalink
Added jArray()
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnmbond committed Jun 21, 2024
1 parent 5c32662 commit f066e48
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
23 changes: 23 additions & 0 deletions PanoramicData.NCalcExtensions.Test/JArrayTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace PanoramicData.NCalcExtensions.Test;

public class JArrayTests
{
[Fact]
public void JArray_CreatesJArray()
{
var expression = new ExtendedExpression("jArray(jObject('a', 1, 'b', null), jObject('a', 2, 'b', 'woo'), null)");
var result = expression.Evaluate() as JArray;
result.Should().BeOfType<JArray>();
result.Should().NotBeNull();
result.Should().HaveCount(3);
result![0]["a"].Should().BeOfType<JValue>();
result![0]["a"].Should().BeEquivalentTo(JToken.FromObject(1));
result![0]["b"].Should().BeOfType<JValue>();
result![0]["b"].Should().BeEquivalentTo(JValue.CreateNull());
result![1]["a"].Should().BeOfType<JValue>();
result![1]["a"].Should().BeEquivalentTo(JToken.FromObject(2));
result![1]["b"].Should().BeOfType<JValue>();
result![1]["b"].Should().BeEquivalentTo(JToken.FromObject("woo"));
result![2].Should().BeEquivalentTo(JValue.CreateNull());
}
}
2 changes: 1 addition & 1 deletion PanoramicData.NCalcExtensions.Test/JObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ public void JObject_CreatesJObject()
result!["b"].Should().BeOfType<JValue>();
result!["b"].Should().BeEquivalentTo(JValue.CreateNull());
}
}
}
3 changes: 3 additions & 0 deletions PanoramicData.NCalcExtensions/ExtendedExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ internal void Extend(string functionName, FunctionArgs functionArgs)
case ExtensionFunction.NullCoalesce:
NullCoalesce.Evaluate(functionArgs);
return;
case ExtensionFunction.NewJArray:
NewJArray.Evaluate(functionArgs);
return;
case ExtensionFunction.NewJObject:
NewJObject.Evaluate(functionArgs);
return;
Expand Down
1 change: 1 addition & 0 deletions PanoramicData.NCalcExtensions/ExtensionFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static class ExtensionFunction
public const string MaxValue = "maxValue";
public const string Min = "min";
public const string MinValue = "minValue";
public const string NewJArray = "jArray";
public const string NewJObject = "jObject";
public const string NullCoalesce = "nullCoalesce";
public const string OrderBy = "orderBy";
Expand Down
20 changes: 20 additions & 0 deletions PanoramicData.NCalcExtensions/Extensions/NewJArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace PanoramicData.NCalcExtensions.Extensions;

internal static class NewJArray
{
internal static void Evaluate(FunctionArgs functionArgs)
{
var parameterIndex = 0;

// Create an empty JObject
var jArray = new JArray();
while (parameterIndex < functionArgs.Parameters.Length)
{
var item = functionArgs.Parameters[parameterIndex++].Evaluate();
jArray.Add(item is null ? JValue.CreateNull() : JObject.FromObject(item));
}

functionArgs.Result = jArray;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<DebugType>portable</DebugType>

<!-- Release Notes -->
<PackageReleaseNotes>Now uses CoreCLR-NCalc version 3.1. Version updated accordingly.</PackageReleaseNotes>
<PackageReleaseNotes>Fixes to min() and max(). Added jArray().</PackageReleaseNotes>

<SymbolPackageFormat>snupkg</SymbolPackageFormat>

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,21 @@ Determines the item at the given index. The first index is 0.
#### Examples
* itemAtIndex(split('a b c', ' '), 1) : 'b'

---

### jArray()

#### Purpose
Creates a Newtonsoft JArray from input values.

#### Parameters
* items[]

#### Examples
* jArray(jObject('a', 1, 'b', null), jObject('a', 2, 'b', #2024-06-21#)) : JArray [ { "a": 1, "b": null}, { "a": 2, "b": "2024-06-21T00:00:00"} ]
* jArray(1, null, 'woo') : JArray [ 1, null, "woo" ]


---

### jObject()
Expand Down

0 comments on commit f066e48

Please sign in to comment.