-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
46 lines (40 loc) · 1.43 KB
/
utils.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
// --------------------------------
// JavaScript Utilities
//
// JavaScript augmenting. Code improved from (and committed back to) SproutCore .fmt method.
String.prototype.fmt = function() {
var args = arguments,
data,
string = this.toString();
if (args.length === 1 && typeof args[0] === 'object') {
data = args[0];
return string.replace(/%\{(.*?)\}/g, function(match, propertyPath) {
var ret = (function(key, data, /* for debugging purposes: */ string) {
var arg, value, formatter, argsplit = key.indexOf(':');
if (argsplit > -1) {
arg = key.substr(argsplit + 1);
key = key.substr(0, argsplit);
}
value = data[key];
formatter = data[key + 'Formatter'];
// formatters are optional
if (formatter) value = formatter(value, arg);
else if (arg) {
throw "String.fmt was given a formatting string, but key `" + key + "` has no formatter! String: " + string;
}
return value;
})(propertyPath, data, string);
// If ret returns nothing, just spit the match back out.
if (ret === null || ret === undefined) return match;
else return ret;
})
}
else {
i = 0;
return string.replace(/%@([0-9]+)?/g, function(match, index) {
index = index ? parseInt(index, 10) - 1 : i++;
if(args[index]!==undefined) return args[index];
else return "";
});
}
}