Skip to content

Commit 270e5a1

Browse files
author
Sébastien Geiser
committed
Add Function ArrayOfType, + Tests and doc
1 parent 13c616f commit 270e5a1

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ public void TypeTesting(string expression, Type type)
131131
[TestCase("\"Text()\".Replace(\"(\", \"x\")", TestOf = typeof(string), ExpectedResult = "Textx)", Category = "StringWithParenthisOrComaInFunctionsArgs")]
132132
[TestCase("\"Text()\".Replace(\"x\", \",\")", TestOf = typeof(string), ExpectedResult = "Te,t()", Category = "StringWithParenthisOrComaInFunctionsArgs")]
133133
[TestCase("\"Text()\".Replace(\"(\", \",\")", TestOf = typeof(string), ExpectedResult = "Text,)", Category = "StringWithParenthisOrComaInFunctionsArgs")]
134+
135+
[TestCase("\"Hello,Test,What\".Split(ArrayOfType(typeof(char), ',')).Length", ExpectedResult = 3, Category = "StringSplit,ArrayOfType")]
136+
[TestCase("\"Hello,Test,What\".Split(ArrayOfType(typeof(char), ','))[0]", ExpectedResult = "Hello", Category = "StringSplit,ArrayOfType")]
137+
[TestCase("\"Hello,Test,What\".Split(ArrayOfType(typeof(char), ','))[1]", ExpectedResult = "Test", Category = "StringSplit,ArrayOfType")]
138+
[TestCase("\"Hello,Test,What\".Split(ArrayOfType(typeof(char), ','))[2]", ExpectedResult = "What", Category = "StringSplit,ArrayOfType")]
134139
#endregion
135140

136141
#region char

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,15 @@ private enum TryBlockEvaluatedState
333333
private Dictionary<string, Func<ExpressionEvaluator, List<string>, object>> complexStandardFuncsDictionary = new Dictionary<string, Func<ExpressionEvaluator, List<string>, object>>(StringComparer.Ordinal)
334334
{
335335
{ "Array", (self, args) => args.ConvertAll(arg => self.Evaluate(arg)).ToArray() },
336+
{ "ArrayOfType", (self, args) =>
337+
{
338+
Array sourceArray = args.Skip(1).Select(arg => self.Evaluate(arg)).ToArray();
339+
Array typedArray = Array.CreateInstance((Type)self.Evaluate(args[0]), sourceArray.Length);
340+
Array.Copy(sourceArray, typedArray, sourceArray.Length);
341+
342+
return typedArray;
343+
}
344+
},
336345
{ "Avg", (self, args) => args.ConvertAll(arg => Convert.ToDouble(self.Evaluate(arg))).Sum() / args.Count },
337346
{ "default", (self, args) =>
338347
{

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ Enumerable.Repeat(3,6).Cast().ToList()[4]
148148
149149
"Hello"[2] == 'l'
150150
true
151+
152+
"Hello,Test,What".Split(ArrayOfType(typeof(char), ',')).Length
153+
3
154+
155+
"Hello,Test,What".Split(ArrayOfType(typeof(char), ','))[0]
156+
Hello
157+
158+
"Hello,Test,What".Split(ArrayOfType(typeof(char), ','))[1]
159+
Test
160+
161+
"Hello,Test,What".Split(ArrayOfType(typeof(char), ','))[2]
162+
What
151163
```
152164

153165
### Small scripts
@@ -345,6 +357,7 @@ The following functions are internally defined. (Most of these are [System.Math
345357
|**[Abs](https://msdn.microsoft.com/en-us/library/f2yzeea2(v=vs.110).aspx)**(double number)|Return a double that is the absolute value of number|`Abs(-3.2d)`|`3.2d`|
346358
|**[Acos](https://msdn.microsoft.com/en-us/library/system.math.acos(v=vs.110).aspx)**(double d)|Return a double value that is the angle in radian whose d is the cosine<br/>d must be betwteen -1 and 1|`Acos(-0.5d)`|`2.0943951023032d`|
347359
|**Array**(object obj1, object obj2 ,...)|Return a array (System.Object[]) of all given arguments|`Array(1, "Hello", true)`|`new object[]{1, "Hello", true}`|
360+
|**ArrayOfType**(Type typeOfTheArray, object obj1, object oj2, ...)|Return a array of the specified type|`ArrayOfType(typeof(char), ',',';')`|`new char[]{',', ';'}`
348361
|**[Asin](https://msdn.microsoft.com/en-us/library/system.math.asin(v=vs.110).aspx)**(double d)|Return a double value that is the angle in radian whose d is the sine<br/>d must be betwteen -1 and 1|`Asin(-0.2d)`|`0.304692654015398d`|
349362
|**[Atan](https://msdn.microsoft.com/en-us/library/system.math.atan(v=vs.110).aspx)**(double d)|Return a double value that is the angle in radian whose d is the tangent|`Atan(2.1)`|`1.1263771168938d`|
350363
|**[Atan2](https://msdn.microsoft.com/en-us/library/system.math.atan2(v=vs.110).aspx)**(double x, double y)|Return a double value that is the angle in radian whose the tangente is the quotient of x and y<br/>|`Atan2(2.1d, 3.4d)`|`0.553294325322293d`|

0 commit comments

Comments
 (0)