forked from wikimedia/wikimedia-portals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hbs-helpers.global.js
131 lines (121 loc) · 3.5 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
130
131
/* jshint strict:false */
/* globals require */
/* globals module */
/* globals console */
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-converttitle-hans', 'data-convert-hans' ],
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 {
/* eslint-disable no-console */
console.log( '\x1b[31m' );
console.log( 'Warning: the attr "' + lowercase + '" was rejected by the printAttrs helper.' );
console.log( '\x1b[0m' );
/* eslint-enable no-console */
}
}
}
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 );
};
/**
* Converts the number into a string and adds a space to separate each group of
* 3 digits.
*
* Inspired from http://stackoverflow.com/a/2254896
*
* @param {number} number
* @return {string}
* @private
*/
function thousandFloor( number ) {
return number.toString().replace( /(\d)(?=(\d{3})+(?!\d))/g, '$1 ' );
}
/**
*
* @param {number} number
* @param {Object} options Handlebars options object.
* @param {Object} options.hash
* @param {boolean} [options.hash.thousandSeparator=false] Numbers are run through
* {@link #thousandFloor}.
* @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;
}
if ( options.hash.thousandSeparator ) {
number = thousandFloor( number );
}
if ( options.hash.nbsp ) {
number = number.toString().split( ' ' ).join( ' ' );
}
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;