Skip to content

Commit cca52eb

Browse files
committed
Added missing handlebars helpers.
1 parent 349ecb3 commit cca52eb

File tree

9 files changed

+151
-95
lines changed

9 files changed

+151
-95
lines changed

ClassLibrary/DataWarehouseAutomation/DataWarehouseAutomation/DataWarehouseAutomation.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Handlebars.Net" Version="2.0.4" />
10+
<PackageReference Include="Handlebars.Net.1.8.0.Signed" Version="1.8.0" />
1111
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
1212
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.13" />
1313
</ItemGroup>

ClassLibrary/DataWarehouseAutomation/DataWarehouseAutomation/HandleBarsHelpers.cs

Lines changed: 141 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,118 @@
11
using System;
22
using System.IO;
3+
using System.Text;
34
using HandlebarsDotNet;
45

56
namespace DataWarehouseAutomation
67
{
78
public class HandleBarsHelpers
89
{
10+
public static int GetRandomNumber(int maxNumber)
11+
{
12+
if (maxNumber < 1)
13+
throw new Exception("The maxNumber value should be greater than 1");
14+
var b = new byte[4];
15+
new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
16+
var seed = (b[0] & 0x7f) << 24 | b[1] << 16 | b[2] << 8 | b[3];
17+
var r = new Random(seed);
18+
return r.Next(1, maxNumber);
19+
}
20+
21+
public static DateTime GetRandomDate(int startYear = 1995)
22+
{
23+
var start = new DateTime(startYear, 1, 1);
24+
var range = (DateTime.Today - start).Days;
25+
var b = new byte[4];
26+
new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
27+
var seed = (b[0] & 0x7f) << 24 | b[1] << 16 | b[2] << 8 | b[3];
28+
return start.AddDays(new Random(seed).Next(1, range)).AddSeconds(new Random(seed).Next(1, 86400));
29+
}
30+
931
public static void RegisterHandleBarsHelpers()
1032
{
1133
// Generation Date/Time functional helper
1234
Handlebars.RegisterHelper("now", (writer, context, parameters) => { writer.WriteSafeString(DateTime.Now); });
1335

14-
// Normal helper
15-
Handlebars.RegisterHelper("samecheck", (output, options, context, data) =>
36+
// Generation random date, based on an integer input value
37+
Handlebars.RegisterHelper("randomdate", (writer, context, arguments) =>
1638
{
17-
//if (data.Length > 10)
18-
// output.Write("More than 10!");
19-
//else
20-
// output.Write("Something else! "+data[0]);
39+
int length = 1995;
2140

22-
if (data[0].ToString() == data[1].ToString())
23-
output.Write("It's the same: " + (object)context);
24-
else
25-
output.Write("Something else! " + data[0]);
41+
if (arguments.Length > 1)
42+
{
43+
throw new HandlebarsException("The {{randomdate}} function requires either a single integer (year e.g. 1995) value or no parameters.");
44+
}
45+
46+
if (arguments.Length == 1)
47+
{
48+
bool evaluationResult = int.TryParse((string)arguments[0], out length);
49+
if (evaluationResult == false)
50+
{
51+
throw new HandlebarsException($"The {{randomdate}} functions failed because {arguments[0]} could not be converted to an integer.");
52+
}
53+
}
2654

55+
writer.WriteSafeString(GetRandomDate(length).Date);
2756
});
2857

29-
// Block helper
30-
Handlebars.RegisterHelper("TenOrMore", (output, options, context, data) =>
58+
// Generation random string, based on an integer input value
59+
Handlebars.RegisterHelper("randomnumber", (writer, context, arguments) =>
3160
{
32-
if (data[0].ToString() == data[1].ToString())
33-
options.Template(output, (object)context);
61+
int length = 10;
3462

35-
});
63+
if (arguments.Length > 1)
64+
{
65+
throw new HandlebarsException("The {{randomnumber}} function requires either a single integer value or no parameters.");
66+
}
3667

37-
// Character spacing not satisfactory? Do not panic, help is near! Make sure the character spacing is righteous using this Handlebars helper.
38-
// Usage {{space sourceDataObject.name}} will space out (!?) the name of the source to 30 characters and a few tabs for lots of white spaces.
39-
Handlebars.RegisterHelper("space", (writer, context, args) =>
40-
{
41-
string outputString = args[0].ToString();
42-
if (outputString.Length < 30)
68+
if (arguments.Length == 1)
4369
{
44-
outputString = outputString.PadRight(30);
70+
bool evaluationResult = int.TryParse((string)arguments[0], out length);
71+
if (evaluationResult == false)
72+
{
73+
throw new HandlebarsException($"The {{randomnumber}} functions failed because {arguments[0]} could not be converted to an integer.");
74+
}
4575
}
46-
writer.WriteSafeString(outputString + "\t\t\t\t");
4776

77+
writer.WriteSafeString(GetRandomNumber(length));
4878
});
4979

50-
Handlebars.RegisterHelper("StringReplace", (writer, context, args) =>
80+
// Generation random string, based on an integer input value
81+
Handlebars.RegisterHelper("randomstring", (writer, context, arguments) =>
5182
{
52-
if (args.Length < 3) throw new HandlebarsException("The {{StringReplace}} function requires at least three arguments.");
83+
int length = 10;
5384

54-
string expression = args[0] as string;
55-
56-
if (args[0] is Newtonsoft.Json.Linq.JValue)
85+
if (arguments.Length > 1)
5786
{
58-
expression = ((Newtonsoft.Json.Linq.JValue)args[0]).Value.ToString();
87+
throw new HandlebarsException("The {{randomstring}} function requires either a single integer value or no parameters.");
5988
}
6089

61-
string pattern = args[1] as string;
62-
string replacement = args[2] as string;
63-
64-
expression = expression.Replace(pattern, replacement);
65-
writer.WriteSafeString(expression);
90+
if (arguments.Length == 1)
91+
{
92+
bool evaluationResult = int.TryParse((string)arguments[0], out length);
93+
if (evaluationResult == false)
94+
{
95+
throw new HandlebarsException($"The {{randomstring}} functions failed because {arguments[0]} could not be converted to an integer.");
96+
}
97+
}
6698

99+
var array = new[]
100+
{
101+
"0","2","3","4","5","6","8","9",
102+
"a","b","c","d","e","f","g","h","j","k","m","n","p","q","r","s","t","u","v","w","x","y","z",
103+
"A","B","C","D","E","F","G","H","J","K","L","M","N","P","R","S","T","U","V","W","X","Y","Z"
104+
};
105+
var sb = new StringBuilder();
106+
for (var i = 0; i < length; i++) sb.Append(array[GetRandomNumber(53)]);
107+
108+
writer.WriteSafeString(sb.ToString());
67109
});
68110

69111

70-
// BLOCK HELPER
71-
//_handlebars.RegisterHelper("if_kpi", (writer, options, context, parameters) =>
72-
//{
73-
// string group = Convert.ToString(parameters[0]);
74-
75-
// if (group == Enum.GetName(typeof(KPICategoryGroupEnum), KPICategoryGroupEnum.KPI))
76-
// {
77-
// options.Template(writer, (object)context);
78-
// }
79-
// else
80-
// {
81-
// options.Inverse(writer, (object)context);
82-
// }
83-
//});
84-
85-
//{
86-
// {#if_equal x "my_string"}}
87-
// x is "my_string"
88-
// { {else} }
89-
// x isn't "my_string"
90-
// { {/ if_equal} }
91-
92-
//public void RegisterHandleBarsHelperEvaluateClassificationType()
93-
//{
94-
// Handlebars.RegisterHelper("ShowContactList", (output, context, parameters) =>
95-
// {
96-
// var contacts = string.Empty;
97-
// for (var i = 0; i < context.Buyers.Length; i++)
98-
// {
99-
// contacts += context.Buyers[i].FirstName + " " + context.Buyers[i].LastName + ",";
100-
// }
101-
102-
// output.WriteSafeString("Contacts: " + contacts);
103-
// });
104-
//}
105-
106-
107-
//Handlebars.registerHelper('if_equal', function(a, b, opts) {
108-
// if (a == b)
109-
// {
110-
// return opts.fn(this)
111-
// }
112-
// else
113-
// {
114-
// return opts.inverse(this)
115-
// }
116-
//})
117-
118-
119112
// Accept two values, and see if they are the same, use as block helper.
120113
// Usage {{#stringcompare string1 string2}} do something {{else}} do something else {{/stringcompare}}
121114
// Usage {{#stringcompare string1 string2}} do something {{/stringcompare}}
122-
Handlebars.RegisterHelper("stringequal", (output, options, context, arguments) =>
115+
Handlebars.RegisterHelper("stringcompare", (TextWriter output, HelperOptions options, dynamic context, object[] arguments) =>
123116
{
124117
if (arguments.Length != 2) throw new HandlebarsException("The {{stringcompare}} functions requires exactly two arguments.");
125118

@@ -139,7 +132,7 @@ public static void RegisterHandleBarsHelpers()
139132
// Accept two values, and do something if they are the different.
140133
// Usage {{#stringdiff string1 string2}} do something {{else}} do something else {{/stringdiff}}
141134
// Usage {{#stringdiff string1 string2}} do something {{/strinstringdiffgcompare}}
142-
Handlebars.RegisterHelper("stringdiff", (output, options, context, arguments) =>
135+
Handlebars.RegisterHelper("stringdiff", (TextWriter output, HelperOptions options, dynamic context, object[] arguments) =>
143136
{
144137
if (arguments.Length != 2) throw new HandlebarsException("The {{stringdiff}} functions requires exactly two arguments.");
145138

@@ -155,6 +148,69 @@ public static void RegisterHandleBarsHelpers()
155148
options.Inverse(output, (object)context);
156149
}
157150
});
151+
152+
153+
154+
Handlebars.RegisterHelper("replicate", (TextWriter output, HelperOptions options, dynamic context, object[] arguments) =>
155+
{
156+
if (arguments.Length != 1) throw new HandlebarsException("The {{replicate}} functions requires a single integer value as input.");
157+
158+
int length = 10;
159+
160+
if (arguments.Length != 1)
161+
{
162+
throw new HandlebarsException("The {{replicate}} function requires either a single integer value or no parameters.");
163+
}
164+
165+
if (arguments.Length == 1)
166+
{
167+
bool evaluationResult = int.TryParse((string)arguments[0], out length);
168+
if (evaluationResult == false)
169+
{
170+
throw new HandlebarsException($"The {{replicate}} functions failed because {arguments[0]} could not be converted to an integer.");
171+
}
172+
}
173+
174+
175+
for (var i = 0; i < length; ++i)
176+
{
177+
options.Template(output, (object)context);
178+
}
179+
180+
});
181+
182+
// Character spacing not satisfactory? Do not panic, help is near! Make sure the character spacing is righteous using this Handlebars helper.
183+
// Usage {{space sourceDataObject.name}} will space out (!?) the name of the source to 30 characters and a few tabs for lots of white spaces.
184+
Handlebars.RegisterHelper("space", (writer, context, args) =>
185+
{
186+
string outputString = args[0].ToString();
187+
if (outputString.Length < 30)
188+
{
189+
outputString = outputString.PadRight(30);
190+
}
191+
writer.WriteSafeString(outputString + "\t\t\t\t");
192+
193+
});
194+
195+
Handlebars.RegisterHelper("StringReplace", (writer, context, args) =>
196+
{
197+
if (args.Length < 3) throw new HandlebarsException("The {{StringReplace}} function requires at least three arguments.");
198+
199+
string expression = args[0] as string;
200+
201+
if (args[0] is Newtonsoft.Json.Linq.JValue)
202+
{
203+
expression = ((Newtonsoft.Json.Linq.JValue)args[0]).Value.ToString();
204+
}
205+
206+
string pattern = args[1] as string;
207+
string replacement = args[2] as string;
208+
209+
expression = expression.Replace(pattern, replacement);
210+
writer.WriteSafeString(expression);
211+
212+
});
213+
158214
}
159215

160216
}

ClassLibrary/DataWarehouseAutomation/Example_Project/Examples.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35-
<Reference Include="Handlebars, Version=2.0.4.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661, processorArchitecture=MSIL">
36-
<HintPath>..\packages\Handlebars.Net.2.0.4\lib\net46\Handlebars.dll</HintPath>
35+
<Reference Include="Handlebars, Version=1.0.0.0, Culture=neutral, PublicKeyToken=623b39d890c8829c, processorArchitecture=MSIL">
36+
<HintPath>..\packages\Handlebars.Net.1.8.0.Signed.1.8.0\lib\portable-net45+sl50+win+wpa81\Handlebars.dll</HintPath>
3737
</Reference>
3838
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
3939
<SpecificVersion>False</SpecificVersion>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Handlebars.Net" version="2.0.4" targetFramework="net461" />
3+
<package id="Handlebars.Net.1.8.0.Signed" version="1.8.0" targetFramework="net461" />
44
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net461" />
55
<package id="Newtonsoft.Json.Schema" version="3.0.13" targetFramework="net461" />
66
</packages>

ClassLibrary/DataWarehouseAutomation/RunDwhAutomation/RunDwhAutomation.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
<Reference Include="Costura, Version=4.1.0.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
5858
<HintPath>..\packages\Costura.Fody.4.1.0\lib\net40\Costura.dll</HintPath>
5959
</Reference>
60-
<Reference Include="Handlebars, Version=2.0.4.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661, processorArchitecture=MSIL">
61-
<HintPath>..\packages\Handlebars.Net.2.0.4\lib\net46\Handlebars.dll</HintPath>
60+
<Reference Include="Handlebars, Version=1.0.0.0, Culture=neutral, PublicKeyToken=623b39d890c8829c, processorArchitecture=MSIL">
61+
<HintPath>..\packages\Handlebars.Net.1.8.0.Signed.1.8.0\lib\portable-net45+sl50+win+wpa81\Handlebars.dll</HintPath>
6262
</Reference>
6363
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
6464
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>

ClassLibrary/DataWarehouseAutomation/RunDwhAutomation/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<package id="CommandLineParser" version="2.8.0" targetFramework="net472" />
44
<package id="Costura.Fody" version="4.1.0" targetFramework="net472" />
55
<package id="Fody" version="6.3.0" targetFramework="net472" developmentDependency="true" />
6-
<package id="Handlebars.Net" version="2.0.4" targetFramework="net472" />
6+
<package id="Handlebars.Net.1.8.0.Signed" version="1.8.0" targetFramework="net472" />
77
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net472" />
88
<package id="Newtonsoft.Json.Schema" version="3.0.13" targetFramework="net472" />
99
</packages>

ClassLibrary/DataWarehouseAutomation/Test_Project/Testing.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<WarningLevel>4</WarningLevel>
3434
</PropertyGroup>
3535
<ItemGroup>
36-
<Reference Include="Handlebars, Version=2.0.4.0, Culture=neutral, PublicKeyToken=22225d0bf33cd661, processorArchitecture=MSIL">
37-
<HintPath>..\packages\Handlebars.Net.2.0.4\lib\net46\Handlebars.dll</HintPath>
36+
<Reference Include="Handlebars, Version=1.0.0.0, Culture=neutral, PublicKeyToken=623b39d890c8829c, processorArchitecture=MSIL">
37+
<HintPath>..\packages\Handlebars.Net.1.8.0.Signed.1.8.0\lib\portable-net45+sl50+win+wpa81\Handlebars.dll</HintPath>
3838
</Reference>
3939
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
4040
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Handlebars.Net" version="2.0.4" targetFramework="net461" />
3+
<package id="Handlebars.Net.1.8.0.Signed" version="1.8.0" targetFramework="net461" />
44
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net461" />
55
<package id="Newtonsoft.Json.Schema" version="3.0.13" targetFramework="net461" />
66
</packages>

0 commit comments

Comments
 (0)