This repository has been archived by the owner on Feb 19, 2018. It is now read-only.
/
str-sprintf.js
148 lines (106 loc) · 3.78 KB
/
str-sprintf.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Format a string using one or more values.
*
* Replicates PHP's own implementation of the sprintf function, albeit with slightly more lenient error handling.
* @return {String}
*/
String.prototype.sprintf = function(){
var m, argValue, argMod,
output = "",
argIndex = 0,
lastIndex = 0,
tokens = /%(?=[-+%\d\$bcdeEfFgGosuxX]|(?: |0|'.))(\d+\$)?([-+]*)( |0|'.)?([-+]*)(\d+)?(\.\d+)?([%bcdeEfFgGosuxX])?/g;
while(m = tokens.exec(this)){
var matchedToken = m[0],
argument = m[1],
signs = (m[2] || "") + (m[4] || ""),
plus = signs.indexOf("+") >= 0,
alignLeft = signs.indexOf("-") >= 0,
padding = m[3] || " ",
width = m[5] || 0,
precision = m[6],
type = m[7],
argMod = "";
/** Check for a custom padding character. */
if("'" === padding.charAt(0))
padding = padding.charAt(1);
/** If a precision specifier was supplied, coerce it to a scalar value */
if(precision && precision.length > 1)
precision = precision.substr(1);
/** Append the previous string segment to our formatted output. */
if(m.index) output += this.substring(lastIndex, m.index);
lastIndex = m.index + matchedToken.length;
/** Argument indexing */
if(undefined === argument){
argValue = arguments[argIndex];
++argIndex;
}
else{
argValue = arguments[parseInt(argument)-1];
if(undefined === argValue) argValue = "";
}
/** Percentage literal */
if("%" === type) output += "%";
/** String */
else if("s" === type){
argMod = String(argValue);
if(precision)
argMod = argMod.substr(0, precision);
}
/** Integer-type specifiers */
else if(/[ducoxXb]/.test(type)){
argMod = parseInt(argValue) || 0;
/** If aligning left and padding with zeroes, pad with spaces instead. Weird PHP behaviour, whatever. */
if(alignLeft && "0" === padding)
padding = " ";
/** Decimal (as Integer) */
if("d" === type) argMod = (plus && argMod >= 0 ? "+" : "") + argMod.toString();
/** Unsigned integer */
else if("u" === type) argMod = (parseInt(argMod) >>> 0);
/** ASCII codepoint (as Integer) */
else if("c" === type){
output += String.fromCharCode(parseInt(argMod));
continue; /** Skip padding, etc */
}
/** Octal */
else if("o" === type) argMod = argMod.toString(8);
/** Hexadecimal value */
else if("x" === type || "X" === type){
argMod = parseInt(argMod).toString(16);
if("X" === type)
argMod = argMod.toUpperCase();
}
/** Binary (as Integer) */
else if("b" === type) argMod = (argMod >>> 0).toString(2);
}
/** Assume it's a double-type specifier */
else{
argMod = parseFloat(argValue) || 0.0,
precision = precision || 6,
plus = plus && argMod >= 0 ? "+" : "";
/** Float */
if("f" === type || "F" === type)
argMod = argMod.toFixed(precision);
/** Scientific notation */
else if("e" === type || "E" === type)
argMod = argMod.toExponential(precision),
argMod = ("E" === type) ? argMod.toUpperCase() : argMod.toLowerCase();
/** "Shorter of %e and %f". Which is just a weird way of saying "Yeah, it's almost the same as %f". */
else if("g" === type || "G" === type)
argMod = argMod.toPrecision(precision)
/** Drop any trailing zeroes (as long as it's not exponential notation). */
.replace(/^(\d+(?=[\.\d]+))((?:\.0+)|(?:(\.\d*[1-9]+)(0+)))$/m, "$1$3");
argMod = plus + argMod;
}
/** Check for padding */
argMod = String(argMod);
if(argMod.length < width){
argMod = [Array((width - argMod.length)+1).join(padding), argMod],
argMod = (alignLeft ? argMod.reverse() : argMod).join("");
}
output += argMod;
}
if(lastIndex < this.length)
output += this.substr(lastIndex);
return output;
};