1
1
using System ;
2
2
using System . IO ;
3
+ using System . Text ;
3
4
using HandlebarsDotNet ;
4
5
5
6
namespace DataWarehouseAutomation
6
7
{
7
8
public class HandleBarsHelpers
8
9
{
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
+
9
31
public static void RegisterHandleBarsHelpers ( )
10
32
{
11
33
// Generation Date/Time functional helper
12
34
Handlebars . RegisterHelper ( "now" , ( writer , context , parameters ) => { writer . WriteSafeString ( DateTime . Now ) ; } ) ;
13
35
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 ) =>
16
38
{
17
- //if (data.Length > 10)
18
- // output.Write("More than 10!");
19
- //else
20
- // output.Write("Something else! "+data[0]);
39
+ int length = 1995 ;
21
40
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
+ }
26
54
55
+ writer . WriteSafeString ( GetRandomDate ( length ) . Date ) ;
27
56
} ) ;
28
57
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 ) =>
31
60
{
32
- if ( data [ 0 ] . ToString ( ) == data [ 1 ] . ToString ( ) )
33
- options . Template ( output , ( object ) context ) ;
61
+ int length = 10 ;
34
62
35
- } ) ;
63
+ if ( arguments . Length > 1 )
64
+ {
65
+ throw new HandlebarsException ( "The {{randomnumber}} function requires either a single integer value or no parameters." ) ;
66
+ }
36
67
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 )
43
69
{
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
+ }
45
75
}
46
- writer . WriteSafeString ( outputString + "\t \t \t \t " ) ;
47
76
77
+ writer . WriteSafeString ( GetRandomNumber ( length ) ) ;
48
78
} ) ;
49
79
50
- Handlebars . RegisterHelper ( "StringReplace" , ( writer , context , args ) =>
80
+ // Generation random string, based on an integer input value
81
+ Handlebars . RegisterHelper ( "randomstring" , ( writer , context , arguments ) =>
51
82
{
52
- if ( args . Length < 3 ) throw new HandlebarsException ( "The {{StringReplace}} function requires at least three arguments." ) ;
83
+ int length = 10 ;
53
84
54
- string expression = args [ 0 ] as string ;
55
-
56
- if ( args [ 0 ] is Newtonsoft . Json . Linq . JValue )
85
+ if ( arguments . Length > 1 )
57
86
{
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." ) ;
59
88
}
60
89
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
+ }
66
98
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 ( ) ) ;
67
109
} ) ;
68
110
69
111
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
-
119
112
// Accept two values, and see if they are the same, use as block helper.
120
113
// Usage {{#stringcompare string1 string2}} do something {{else}} do something else {{/stringcompare}}
121
114
// 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 ) =>
123
116
{
124
117
if ( arguments . Length != 2 ) throw new HandlebarsException ( "The {{stringcompare}} functions requires exactly two arguments." ) ;
125
118
@@ -139,7 +132,7 @@ public static void RegisterHandleBarsHelpers()
139
132
// Accept two values, and do something if they are the different.
140
133
// Usage {{#stringdiff string1 string2}} do something {{else}} do something else {{/stringdiff}}
141
134
// 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 ) =>
143
136
{
144
137
if ( arguments . Length != 2 ) throw new HandlebarsException ( "The {{stringdiff}} functions requires exactly two arguments." ) ;
145
138
@@ -155,6 +148,69 @@ public static void RegisterHandleBarsHelpers()
155
148
options . Inverse ( output , ( object ) context ) ;
156
149
}
157
150
} ) ;
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
+
158
214
}
159
215
160
216
}
0 commit comments