-
Notifications
You must be signed in to change notification settings - Fork 29
/
hbs-helpers.global.js
129 lines (116 loc) · 3.42 KB
/
hbs-helpers.global.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* eslint-env node */
var Handlebars = require( 'handlebars' ),
helpers = {};
/**
* If `attrs` is an object in the current context,
* it will print out the properties and their values
* as if they were element attributes.
*
* // if this is:
* { 'id': 'elt_id', 'class': 'elt_class' }
*
* // it will output:
* ' id="elt_id" class="elt_class"'
*
* For security reasons, we only support whitelisted attributes.
* A warning message will be prompted (for review) when an attribute is
* rejected.
*
* @param {string} attrs
* @return {Handlebars.SafeString}
*/
helpers.printAttrs = function ( attrs ) {
var output = '',
whiteList = [ 'id', 'class', 'lang', 'data-title-hans', 'data-title-hant', 'data-hans', 'data-hant' ],
lowercase, attr;
if ( this[ attrs ] ) {
for ( attr in this[ attrs ] ) {
lowercase = attr.toLowerCase();
if ( whiteList.indexOf( lowercase ) > -1 ) {
output += ' ' + Handlebars.escapeExpression( lowercase ) + '="' + Handlebars.escapeExpression( this[ attrs ][ attr ] ) + '"';
} else {
console.log( '\x1b[31m' );
console.log( 'Warning: the attr "' + lowercase + '" was rejected by the printAttrs helper.' );
console.log( '\x1b[0m' );
}
}
}
return new Handlebars.SafeString( output );
};
/**
* If `classes` is an array in the current context,
* it will print out its value by concatenating all of the elements in
* it, separated by spaces.
*
* // if this is:
* [ 'class1', 'class2' ]
*
* // it will output:
* ' class1 class2'
*
* @param {string} classes
* @return {Handlebars.SafeString}
*/
helpers.printClasses = function ( classes ) {
var output = '';
if ( this[ classes ] && Array.isArray( this[ classes ] ) ) {
output = Handlebars.escapeExpression( ' ' + this[ classes ].join( ' ' ) );
}
return new Handlebars.SafeString( output );
};
/**
* Equal to helper
*
* @param {Mixed} a
* @param {Mixed} b
* @param {Object} options Handlebars options object.
* @return {Function}
*/
helpers.eq = function ( a, b, options ) {
return ( a === b ) ? options.fn( this ) : options.inverse( this );
};
/**
* @param {number} number
* @param {Object} options Handlebars options object.
* @param {Object} options.hash
* @param {boolean} [options.hash.rounded=false] Rounds numbers downwards to
* the nearest power of ten, up to a thousand.
* @param {boolean} [options.hash.nbsp=false] The separating space is replaced by
* a ` `.
* @return {Handlebars.SafeString}
*/
helpers.formatNumber = function ( number, options ) {
var numberLength, powerOfTen;
if ( options.hash.rounded ) {
numberLength = Math.min( number.toString().length - 1, 3 );
powerOfTen = Math.pow( 10, numberLength );
number = Math.floor( number / powerOfTen ) * powerOfTen;
}
number = number.toLocaleString( options.hash.locale );
return new Handlebars.SafeString( number );
};
/**
* Wraps a block to allow you to put `~` on this helper
* in order to clean whitespaces.
*
* @param {Object} options Handlebars options object.
* @return {Function}
*/
helpers.trim = function ( options ) {
return options.fn( this );
};
/**
* Checks if an array contains an element.
*
* @param {Mixed} list
* @param {Array} elem
* @param {Object} options Handlebars options object.
* @return {Function}
*/
helpers.has = function ( list, elem, options ) {
if ( list.indexOf( elem ) > -1 ) {
return options.fn( this );
}
return options.inverse( this );
};
module.exports = helpers;