Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add New Node GetNumber #12332

Merged
merged 11 commits into from
Dec 15, 2021
31 changes: 25 additions & 6 deletions src/Libraries/CoreNodes/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ public static object ToNumber(string @string)
throw new ArgumentException(Properties.Resources.StringToNumberInvalidNumberMessage, "str");
}

/// <summary>
/// Get all of the number strings from the target string as a string
chuongmep marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="string">Target string to be get</param>
/// <returns name="str">Number In string</returns>
/// <search>getnumber,tonumber,strtonumber,numberinstring,string2number,stringtonumber,int,double,cast</search>
public static string GetNumber(string @string)
{
string str = string.Empty;
if (string.IsNullOrEmpty(@string)) return str;
List<double> result = new List<double>();
for (int i = 0; i < @string.Length; i++)
{
if (char.IsDigit(@string[i]))
str += @string[i];
chuongmep marked this conversation as resolved.
Show resolved Hide resolved
}

return str;
}
/// <summary>
/// Concatenates multiple strings into a single string.
/// </summary>
Expand Down Expand Up @@ -74,7 +93,7 @@ public static string[] Split(string @string, params string[] separators)
{
separators = separators.Select(s => s == "\n" ? Environment.NewLine : s).ToArray(); // converts all \n in separater array to Environment Newline (i.e. \r\n)
@string = Regex.Replace(@string, "(?<!\r)\n", Environment.NewLine); // converts all \n in String str to Environment.NewLine (i.e. '\r\n')

return separators.Contains("")
? @string.ToCharArray().Select(char.ToString).ToArray()
: @string.Split(separators, StringSplitOptions.RemoveEmptyEntries);
Expand Down Expand Up @@ -401,7 +420,7 @@ public static string PadRight(string @string, int newLength, string padChars = "
/// <search>center align,center-align,centered,whitespace,expand string,surround</search>
public static string Center(string @string, int newLength, string padChars = " ")
{
var padHalf = (newLength - @string.Length)/2;
var padHalf = (newLength - @string.Length) / 2;

return
new string(
Expand Down Expand Up @@ -464,11 +483,11 @@ public static string Remove(string @string, int startIndex, int? count = null)
// However, the removal process in this function always operates rightwards (left to right).
// Therefore, a conversion for start index needs to be done in order to change
// from leftwards removal (right to left) to rightwards removal (left to right).
startIndex = startIndex + _count + 1;
startIndex = startIndex + _count + 1;
_count *= -1;
}
if(_count > @string.Length)

if (_count > @string.Length)
{
throw new ArgumentOutOfRangeException("count", Properties.Resources.StringRemoveCountOutOfRangeMessage);
}
Expand All @@ -478,7 +497,7 @@ public static string Remove(string @string, int startIndex, int? count = null)
return string.Empty;
}

if (startIndex >= @string.Length || startIndex < 0)
if (startIndex >= @string.Length || startIndex < 0)
{
// startIndex of an array must be within the string length.
// If after the conversion of negative startIndex, startIndex is still
Expand Down
3 changes: 3 additions & 0 deletions src/LibraryViewExtension/web/library/layoutSpecs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,9 @@
{
"path": "DSCoreNodes.DSCore.String.ToNumber"
},
{
"path": "DSCoreNodes.DSCore.String.GetNumber"
},
{
"path": "DSCoreNodes.DSCore.String.ChangeCase"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,9 @@
{
"path": "DSCoreNodes.DSCore.String.ToNumber"
},
{
"path": "DSCoreNodes.DSCore.String.GetNumber"
},
{
"path": "DSCoreNodes.DSCore.String.ChangeCase"
}
Expand Down
8 changes: 8 additions & 0 deletions test/DynamoCoreTests/Nodes/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ public void TestStringToNumberFileInput()
RunModel(testFilePath);
AssertPreviewValue("f8767579-f7c1-475f-980e-7cd6a42684c8", 123521);
}
[Test]
public void TestStringGetNumberFileInput()
{
string testFilePath = Path.Combine(localDynamoStringTestFolder, "TestStringGetNumber_normal.dyn");

RunModel(testFilePath);
AssertPreviewValue("09c8d5ba431a442e886fe234922c6e3c", "123521");
}

[Test]
public void TestStringToNumberFunctionInput()
Expand Down
8 changes: 8 additions & 0 deletions test/Libraries/CoreNodesTests/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public static void ToNumber()
Assert.AreEqual(10.0, String.ToNumber("10.0"));
}

[Test]
[Category("UnitTests")]
public static void GetNumber()
{
Assert.AreEqual("10", String.GetNumber("name 10 myname"));
Assert.AreEqual("100", String.GetNumber("name 10.0 my name"));
}

[Test]
[Category("UnitTests")]
public static void Concat()
Expand Down
8 changes: 8 additions & 0 deletions test/ViewExtensionLibraryTests/resources/libraryItems.json
Original file line number Diff line number Diff line change
Expand Up @@ -5232,6 +5232,14 @@
"itemType": "action",
"keywords": "2number, str2number, strtonumber, string2number, stringtonumber, int, double, cast"
},
{
"fullyQualifiedName": "DSCore.String.GetNumber",
"iconUrl": null,
"contextData": "DSCore.String.GetNumber@string",
"parameters": null,
"itemType": "action",
"keywords": "getnumber,tonumber,strtonumber,numberinstring,string2number,stringtonumber,int,double,cast"
},
{
"fullyQualifiedName": "DSCore.String.Concat",
"iconUrl": "http://localhost/icons/DSCore.String.Concat.Small?path=DSCoreNodes.dll",
Expand Down
191 changes: 191 additions & 0 deletions test/core/string/TestStringGetNumber_normal.dyn
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
{
"Uuid": "84e01bd6-b64f-4527-92b0-375c2a7717ff",
"IsCustomNode": false,
"Description": "",
"Name": "TestStringGetNumber_normal",
"ElementResolver": {
"ResolutionMap": {}
},
"Inputs": [],
"Outputs": [],
"Nodes": [
{
"ConcreteType": "Dynamo.Graph.Nodes.ZeroTouch.DSFunction, DynamoCore",
"NodeType": "FunctionNode",
"FunctionSignature": "DSCore.String.GetNumber@string",
"Id": "33960a2650b247138bde4a36cf455051",
"Inputs": [
{
"Id": "f3793d65381448f2863375f6f938f231",
"Name": "string",
"Description": "String to be get\n\nstring",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Outputs": [
{
"Id": "8ce1e5327029458c852d68a1c3b962d2",
"Name": "str",
"Description": "number get in string",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Replication": "Auto",
"Description": "Get Number In String\n\nString.GetNumber (string: string): string"
},
{
"ConcreteType": "CoreNodeModels.Watch, CoreNodeModels",
"NodeType": "ExtensionNode",
"Id": "09c8d5ba431a442e886fe234922c6e3c",
"Inputs": [
{
"Id": "54d51b27c7aa4b34bd66bb7bf7812500",
"Name": "",
"Description": "Node to show output from",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Outputs": [
{
"Id": "2629f11162bf48c8a103bb6e675d1919",
"Name": "",
"Description": "Node output",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Replication": "Disabled",
"Description": "Visualize the node's output"
},
{
"ConcreteType": "Dynamo.Graph.Nodes.CodeBlockNodeModel, DynamoCore",
"NodeType": "CodeBlockNode",
"Code": "\"test123521test\";",
"Id": "e90a23e6127e4b3aa3fd4ec11a2cc43a",
"Inputs": [],
"Outputs": [
{
"Id": "34435430976a44459a8fc8e0c8f9946f",
"Name": "",
"Description": "Value of expression at line 1",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Replication": "Disabled",
"Description": "Allows for DesignScript code to be authored directly"
}
],
"Connectors": [
{
"Start": "8ce1e5327029458c852d68a1c3b962d2",
"End": "54d51b27c7aa4b34bd66bb7bf7812500",
"Id": "58cb7905921d4897b7abbdd83cad85bf",
"IsHidden": "False"
},
{
"Start": "34435430976a44459a8fc8e0c8f9946f",
"End": "f3793d65381448f2863375f6f938f231",
"Id": "0c25384360314e7a9cd5b0f4a03d9e74",
"IsHidden": "False"
}
],
"Dependencies": [],
"NodeLibraryDependencies": [],
"Thumbnail": "",
"GraphDocumentationURL": null,
"ExtensionWorkspaceData": [
{
"ExtensionGuid": "28992e1d-abb9-417f-8b1b-05e053bee670",
"Name": "Properties",
"Version": "2.13",
"Data": {}
},
{
"ExtensionGuid": "DFBD9CC0-DB40-457A-939E-8C8555555A9D",
"Name": "Generative Design",
"Version": "1.10",
"Data": {}
}
],
"Author": "",
"Linting": {
"activeLinter": "None",
"activeLinterId": "7b75fb44-43fd-4631-a878-29f4d5d8399a",
"warningCount": 0,
"errorCount": 0
},
"Bindings": [],
"View": {
"Dynamo": {
"ScaleFactor": 1.0,
"HasRunWithoutCrash": true,
"IsVisibleInDynamoLibrary": true,
"Version": "2.13.0.3197",
"RunType": "Manual",
"RunPeriod": "1000"
},
"Camera": {
"Name": "Background Preview",
"EyeX": -17.0,
"EyeY": 24.0,
"EyeZ": 50.0,
"LookX": 12.0,
"LookY": -13.0,
"LookZ": -58.0,
"UpX": 0.0,
"UpY": 1.0,
"UpZ": 0.0
},
"ConnectorPins": [],
"NodeViews": [
{
"ShowGeometry": true,
"Name": "String.GetNumber",
"Id": "33960a2650b247138bde4a36cf455051",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Excluded": false,
"X": 364.0,
"Y": 330.0
},
{
"ShowGeometry": true,
"Name": "Watch",
"Id": "09c8d5ba431a442e886fe234922c6e3c",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Excluded": false,
"X": 654.92355114259954,
"Y": 385.97804004352759
},
{
"ShowGeometry": true,
"Name": "Code Block",
"Id": "e90a23e6127e4b3aa3fd4ec11a2cc43a",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Excluded": false,
"X": 49.0,
"Y": 367.0
}
],
"Annotations": [],
"X": -14.232848285531475,
"Y": -158.11358593217108,
"Zoom": 1.3076413563671876
}
}