Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MOD: the module that will be used in template.
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* ktemplate 模板公用函数 | ||
* @author: Knight | ||
* @version: 0.0.1 | ||
*/ | ||
define(function (require, exports, module) { | ||
var rAmp = /&/g, | ||
rLt = /</g, | ||
rGt = />/g, | ||
rApos = /\'/g, | ||
rQuot = /\"/g, | ||
hChars = /[&<>\"\']/; | ||
|
||
var coerceToString = function (val) { | ||
return String((val === null || val === undefined) ? '' : val); | ||
}; | ||
/** | ||
* 编码 html , KTemplate中用到 | ||
* @param str | ||
* @return {String|XML} | ||
*/ | ||
exports.escape = function (str) { | ||
str = coerceToString(str); | ||
return hChars.test(str) ? | ||
str | ||
.replace(rAmp, '&') | ||
.replace(rLt, '<') | ||
.replace(rGt, '>') | ||
.replace(rApos, ''') | ||
.replace(rQuot, '"') : | ||
str; | ||
}; | ||
|
||
var utils = {}; | ||
['forEach', 'some', 'every', 'filter', 'map'].forEach(function (fnName) { | ||
utils[fnName] = function (arr, fn, context) { | ||
if (!arr || typeof arr == 'string') return arr; | ||
context = context || this; | ||
if (arr[fnName]) { | ||
return arr[fnName](fn, context); | ||
} else { | ||
var keys = Object.keys(arr); | ||
return keys[fnName](function (key) { | ||
return fn.call(context, arr[key], key, arr); | ||
}, context); | ||
} | ||
}; | ||
}); | ||
exports.mixin = function mixin(to, from) { | ||
utils.forEach(from, function (val, key) { | ||
var toString = {}.toString.call(val); | ||
if (toString == '[object Array]' || toString == '[object Object]') { | ||
to[key] = mixin(val, to[key] || {}); | ||
} else { | ||
to[key] = val; | ||
} | ||
}); | ||
return to; | ||
}; | ||
}); |