-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhate.js
More file actions
71 lines (64 loc) · 2.94 KB
/
hate.js
File metadata and controls
71 lines (64 loc) · 2.94 KB
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
/** @author pcthomatos
* @license: MIT
**/
var hate = (function(){
var _compiledStr = undefined,
_chainable = false,
_openCloseDelimiters = ['\\{\\{', '}}'],
//##################################################################################################################
_init = function(){
return {
$: _compiledStr,
options: _setOptions,
compile: _compile
}
},
//##################################################################################################################
_setOptions = function(userOptionsObj){
var userOptionsObj = userOptionsObj,
chainable, openCloseDelimiters;
if(userOptionsObj.hasOwnProperty('openCloseDelimiters')){
openCloseDelimiters = userOptionsObj.openCloseDelimiters;
if(openCloseDelimiters.length &&
openCloseDelimiters.length === 2 &&
typeof openCloseDelimiters[0] === 'string' &&
typeof openCloseDelimiters[1] === 'string') _openCloseDelimiters = openCloseDelimiters;
}
if(userOptionsObj.hasOwnProperty('chainable')){
chainable = userOptionsObj.chainable;
_chainable = (chainable === true || chainable === 1 || (/^true|1$/).test(chainable))? true : false;
if(_chainable) return this;
}
},
//##################################################################################################################
_process = function(collection, tempalteStr){
var collection = collection,
tempalteStr = typeof tempalteStr !== 'string'? _compiledStr : tempalteStr,
openDmtr = _openCloseDelimiters[0],
closeDmtr = _openCloseDelimiters[1],
collectionLength, i, j;
if(!collection.length){
for(i in collection) tempalteStr = tempalteStr.replace(new RegExp(openDmtr + i + closeDmtr, 'g'), collection[i]);
}else{
collectionLength = collection.length;
for(i = 0; i < collectionLength; i+=1)
for(j in collection[i])
tempalteStr = tempalteStr.replace(regEx.compile(openDmtr + j + closeDmtr, 'g'), collection[i][j]);
}
return tempalteStr;
},
//##################################################################################################################
_compile = function(objectArrOrObj, tempalteStr){
var collection = objectArrOrObj,
tempalteStr = tempalteStr;
this.$ = _compiledStr = (/^\[object Array]|\[object Object]$/).test(Object.prototype.toString.call(collection)) &&
(!collection.length || collection.length && typeof collection[0] === 'object') &&
(typeof tempalteStr === 'string' || _compiledStr !== undefined)?
_process(collection, tempalteStr)
: 'Error: Invalid hate.jquery.js params. Expecting Object or Array of Objects.';
return _chainable? this : _compiledStr;
};
//##################################################################################################################
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
return _init();
}());