/
thunder.js
128 lines (107 loc) · 3.3 KB
/
thunder.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
/*!
* thunder
* Copyright(c) 2012 dreamerslab <ben@dreamerslab.com>
* MIT Licensed
*
* @fileoverview
* A lightning fast JavaScript template engine.
*/
;( function ( root ){
var cache = {};
// split `escape` into another obj speeds up the `no escape` parsing
var escape = {
rules : {
'&' : '&',
'<' : '<',
'>' : '>',
'"' : '"'
},
fn : function ( input ){
return typeof( input ) != 'string' ?
input : input.replace( /[&<>"]/g, function ( match ){
return escape.rules[ match ];
});
}
};
var thunder = {
// for client-side
html_to_text : function ( input ){
return input.
replace( /\"/g, '\\\"' ).
replace( /\n/g, '\\n\\\n' );
},
compiled_text : function ( input, options ){
var arr = ( options && options.compress === true ?
// compress
input.replace( /\n\r\t|\s+/g, ' ' ) :
// chage the new line to unix version
input.replace( /\n\r|\r/g, '\n' )).
split( '<?' ).join( '?>\x1b' ).split( '?>' );
var str = '';
var i = 0;
var j = arr.length;
var tmp;
// string concat is faster than array `push`
for(; i < j; i++ ){
tmp = arr[ i ];
str += tmp.charAt( 0 ) != '\x1b' ?
// `\t` (tab) is ok, we need to handle with `\n` (line feed)
"__t__+='" + tmp.replace( /\'|\\/g, '\\$&' ).replace( /\n/g, '\\n\\\n' ) + "'" :
( tmp.charAt( 1 ) == '=' ?
';__t__+=' + tmp.substr( 2 ) + ';' :
( tmp.charAt( 1 ) == '-' ?
';__t__+=e(' + tmp.substr( 2 ) + ");" :
';' + tmp.substr( 1 )));
}
// `replace` is faster than `split` -> `join`
return ( 'var __t__="";' + str + ';return __t__;' ).
replace( /__t__\+\=\'\'\;/g, '' ).
replace( /var __t__\=\"\"\;__t__\+\=/g, 'var __t__=' );
},
compile : function ( input, options ){
var str = this.compiled_text( input, options );
var fn;
try{
// must save this new Function to fn,
// do not just invoke in the return function, it's slow.
// ex. return new Function( 'it', 'e', str )( locals, escape.fn ); <-- never do that
fn = new Function( 'it', 'e', str );
}catch( err ){
console.log( '[thunder] Having trouble with creating a template function: \n' + str );
throw err;
}
return function ( locals ){
return fn( locals, escape.fn );
};
},
cached : function ( input, options ){
if( !cache[ input ]){
cache[ input ] = this.compile( input, options );
}
return cache[ input ];
},
render : function ( input, locals, options ){
var method = options && options.cached === true ?
'cached' : 'compile';
return this[ method ]( input, options )( locals );
}
};
// browser support
if( typeof exports === 'undefined' ) return root.call( thunder );
// requirejs
if( typeof define !== 'undefined' ){
return define( function ( require, exports, module ){
module.exports = thunder;
});
}
/**
* @public
*/
thunder.version = JSON.parse(
require( 'fs' ).readFileSync( __dirname + '/../package.json', 'utf8' )
).version;
/**
* Exports module.
*/
module.exports = thunder;
})( this );