This repository was archived by the owner on Jun 9, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
98 lines (86 loc) · 2.46 KB
/
index.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
(function(g) {
var FORMAT_RE = /{({*)([^:{\|}]+)(\|([^:}]+))?(:(-?\d*)([ij]?))?(}*)}/g,
defined = function(x) { return typeof x !== 'undefined' },
format = function() {
var args = arguments,
str = this
return this.replace(FORMAT_RE, function() {
var m = arguments,
match = m[0],
braces_in = m[1],
id = m[2],
// id not an int? named!
named = (id % 1 !== 0),
// if no property chain exist, get the name instead
property_chain = m[4] || (named && id),
padding = m[6],
fill = " ",
modifier = m[7],
braces_out = m[8],
property,
obj = {},
arg = named ? args[0] : args[id],
res = []
// escaped?
if (braces_in.length > 0 && braces_out.length > 0)
return match.slice(1, match.length - 1)
// visit the properties
property_chain = property_chain && property_chain.split('.') || []
while (defined(property = property_chain.shift()) && defined(arg)) {
// evaluate any intermediate function
if (typeof arg === 'function') arg = arg.apply(obj, [])
obj = arg
arg = arg[property]
}
// Evaluate the leaf function
if (typeof arg === 'function') arg = arg.apply(obj, [])
// Integer modifier
if ('i' === modifier) arg = Math.round(arg)
// JSON modifier
else if ('j' === modifier) arg = JSON.stringify(arg)
// Nothing found :-(
if (!defined(arg)) return match
// Apply padding and return
arg = String(arg)
if (!padding) padding = 0
if (padding.length > 1 &&
padding.charAt(0) == "0" &&
!isNaN(parseFloat(arg)))
{
fill = "0"
}
if (padding < 0) {
res.push(arg)
padding = 1 - padding
}
if (padding > arg.length) res.length = padding - arg.length
if (!defined(res[0])) res[res.length] = arg
return [braces_in,res.join(fill),braces_out].join("")
})
},
main = function() {
// Call as a method
return format.apply(arguments[0], Array.prototype.slice.call(arguments, 1))
}
// Install as a method
main.extendString = function(methodName) {
String.prototype[methodName || 'format'] = format
}
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
{
// CommonJS module (e.g. Node)
module.exports = main;
}
else if (typeof define === 'function' && define.amd)
{
// Asynchronous Module Definition module (e.g. RequireJS)
define([], function() {
return main;
});
}
else
{
// Browser
g.stringformat = main;
}
})(this)