-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmultiline.js
More file actions
46 lines (45 loc) · 1.23 KB
/
multiline.js
File metadata and controls
46 lines (45 loc) · 1.23 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
/**
* [multiline.js]
* @author lsong
* @homepage https://lsong.org
* @github https://github.com/song940/multiline.js
*/
;(function(exports, undefined){
/**
* [multiline]
* @param {[function]} func [function(){//}]
* @param {[object]} args [{ name: 'lsong' }}]
* @return {[string]} str
*/
var multiline = function(func,args){
if(!(typeof func === 'function')){
throw new Error('The arguments must passed and they be a function .');
}
var funcStr = func.toString();
var regexpComment = /\/\*([\s\S]*?)\*\//;
var regexpVariable = /#{([\s\S]*?)}/g;
if(!regexpComment.test(funcStr)){
throw new Error("Comment not found.");
}
var match = regexpComment.exec(funcStr);
var str = match[1];
if(regexpVariable.test(str) && !args){
throw new Error('args not passed .');
}
return str.replace(regexpVariable, function(match, key, index, str){
key = key.trim();
if(!(key in args)){
throw new Error(key + ' is not passed from arguments.', key);
return '';
}else{
return args[key];
}
});
};
//exports.
if(typeof window == 'object'){
window.multiline = multiline;
}else{
module.exports = multiline;
}
})();