1
- /*! dustjs-helpers - v1.2 .0
1
+ /*! dustjs-helpers - v1.3 .0
2
2
* https://github.com/linkedin/dustjs-helpers
3
3
* Copyright (c) 2014 Aleksander Williams; Released under the MIT License */
4
4
( function ( dust ) {
5
5
6
- // Note: all error conditions are logged to console and failed silently
7
-
8
- /* make a safe version of console if it is not available
9
- * currently supporting:
10
- * _console.log
11
- * */
12
- var _console = ( typeof console !== 'undefined' ) ? console : {
13
- log : function ( ) {
14
- /* a noop*/
15
- }
16
- } ;
6
+ //using the built in logging method of dust when accessible
7
+ var _log = dust . log ? function ( mssg ) { dust . log ( mssg , "INFO" ) ; } : function ( ) { } ;
17
8
18
9
function isSelect ( context ) {
19
10
var value = context . current ( ) ;
@@ -57,7 +48,7 @@ function filter(chunk, context, bodies, params, filterOp) {
57
48
}
58
49
}
59
50
else {
60
- _console . log ( "No key specified for filter in:" + filterOpType + " helper " ) ;
51
+ _log ( "No key specified for filter in:" + filterOpType + " helper " ) ;
61
52
return chunk ;
62
53
}
63
54
expectedValue = dust . helpers . tap ( params . value , chunk , context ) ;
@@ -71,7 +62,7 @@ function filter(chunk, context, bodies, params, filterOp) {
71
62
return chunk . render ( body , context ) ;
72
63
}
73
64
else {
74
- _console . log ( "Missing body block in the " + filterOpType + " helper " ) ;
65
+ _log ( "No key specified for filter in: " + filterOpType + " helper " ) ;
75
66
return chunk ;
76
67
}
77
68
}
@@ -191,7 +182,7 @@ var helpers = {
191
182
dump = JSON . stringify ( context . stack . head , jsonFilter , 2 ) ;
192
183
}
193
184
if ( to === 'console' ) {
194
- _console . log ( dump ) ;
185
+ _log ( dump ) ;
195
186
return chunk ;
196
187
}
197
188
else {
@@ -226,7 +217,7 @@ var helpers = {
226
217
return chunk . render ( bodies . block , context ) ;
227
218
}
228
219
else {
229
- _console . log ( "Missing body block in the if helper!" ) ;
220
+ _log ( "Missing body block in the if helper!" ) ;
230
221
return chunk ;
231
222
}
232
223
}
@@ -236,7 +227,7 @@ var helpers = {
236
227
}
237
228
// no condition
238
229
else {
239
- _console . log ( "No condition given in the if helper!" ) ;
230
+ _log ( "No condition given in the if helper!" ) ;
240
231
}
241
232
return chunk ;
242
233
} ,
@@ -257,14 +248,17 @@ var helpers = {
257
248
operand = params . operand ,
258
249
round = params . round ,
259
250
mathOut = null ,
260
- operError = function ( ) { _console . log ( "operand is required for this math method" ) ; return null ; } ;
251
+ operError = function ( ) {
252
+ _log ( "operand is required for this math method" ) ;
253
+ return null ;
254
+ } ;
261
255
key = dust . helpers . tap ( key , chunk , context ) ;
262
256
operand = dust . helpers . tap ( operand , chunk , context ) ;
263
257
// TODO: handle and tests for negatives and floats in all math operations
264
258
switch ( method ) {
265
259
case "mod" :
266
260
if ( operand === 0 || operand === - 0 ) {
267
- _console . log ( "operand for divide operation is 0/-0: expect Nan!" ) ;
261
+ _log ( "operand for divide operation is 0/-0: expect Nan!" ) ;
268
262
}
269
263
mathOut = parseFloat ( key ) % parseFloat ( operand ) ;
270
264
break ;
@@ -279,7 +273,7 @@ var helpers = {
279
273
break ;
280
274
case "divide" :
281
275
if ( operand === 0 || operand === - 0 ) {
282
- _console . log ( "operand for divide operation is 0/-0: expect Nan/Infinity!" ) ;
276
+ _log ( "operand for divide operation is 0/-0: expect Nan/Infinity!" ) ;
283
277
}
284
278
mathOut = parseFloat ( key ) / parseFloat ( operand ) ;
285
279
break ;
@@ -296,7 +290,7 @@ var helpers = {
296
290
mathOut = Math . abs ( parseFloat ( key ) ) ;
297
291
break ;
298
292
default :
299
- _console . log ( "method passed is not supported" ) ;
293
+ _log ( "method passed is not supported" ) ;
300
294
}
301
295
302
296
if ( mathOut !== null ) {
@@ -317,7 +311,7 @@ var helpers = {
317
311
}
318
312
// no key parameter and no method
319
313
else {
320
- _console . log ( "Key is a required parameter for math helper along with method/operand!" ) ;
314
+ _log ( "Key is a required parameter for math helper along with method/operand!" ) ;
321
315
}
322
316
return chunk ;
323
317
} ,
@@ -340,13 +334,13 @@ var helpers = {
340
334
return chunk . render ( bodies . block , context . push ( { isSelect : true , isResolved : false , selectKey : key } ) ) ;
341
335
}
342
336
else {
343
- _console . log ( "Missing body block in the select helper " ) ;
337
+ _log ( "Missing body block in the select helper " ) ;
344
338
return chunk ;
345
339
}
346
340
}
347
341
// no key
348
342
else {
349
- _console . log ( "No key given in the select helper!" ) ;
343
+ _log ( "No key given in the select helper!" ) ;
350
344
}
351
345
return chunk ;
352
346
} ,
@@ -512,6 +506,12 @@ var helpers = {
512
506
513
507
} ;
514
508
515
- dust . helpers = helpers ;
509
+ for ( var key in helpers ) {
510
+ dust . helpers [ key ] = helpers [ key ] ;
511
+ }
512
+
513
+ if ( typeof exports !== 'undefined' ) {
514
+ module . exports = dust ;
515
+ }
516
516
517
- } ) ( typeof exports !== 'undefined' ? module . exports = require ( 'dustjs-linkedin' ) : dust ) ;
517
+ } ) ( typeof exports !== 'undefined' ? require ( 'dustjs-linkedin' ) : dust ) ;
0 commit comments