4 // Return a formatted string
7 // discuss at: http://phpjs.org/functions/sprintf
8 // + original by: Ash Searle (http://hexmen.com/blog/)
9 // + namespaced by: Michael White (http://getsprink.com)
11 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
12 // + input by: Paulo Ricardo F. Santos
13 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
14 // + input by: Brett Zamir (http://brettz9.blogspot.com)
15 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
16 // * example 1: sprintf("%01.2f", 123.1);
17 // * returns 1: 123.10
18 // * example 2: sprintf("[%10s]", 'monkey');
19 // * returns 2: '[ monkey]'
20 // * example 3: sprintf("[%'#10s]", 'monkey');
21 // * returns 3: '[####monkey]'
22 var regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuidfegEG])/g;
23 var a = arguments, i = 0, format = a[i++];
26 var pad = function(str, len, chr, leftJustify) {
28 var padding = (str.length >= len) ? '' : Array(1 + len - str.length >>> 0).join(chr);
29 return leftJustify ? str + padding : padding + str;
33 var justify = function(value, prefix, leftJustify, minWidth, zeroPad, customPadChar) {
34 var diff = minWidth - value.length;
36 if (leftJustify || !zeroPad) {
37 value = pad(value, minWidth, customPadChar, leftJustify);
39 value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length);
46 var formatBaseX = function(value, base, prefix, leftJustify, minWidth, precision, zeroPad) {
47 // Note: casts negative numbers to positive ones
48 var number = value >>> 0;
49 prefix = prefix && number && {'2': '0b', '8': '0', '16': '0x'}[base] || '';
50 value = prefix + pad(number.toString(base), precision || 0, '0', false);
51 return justify(value, prefix, leftJustify, minWidth, zeroPad);
55 var formatString = function(value, leftJustify, minWidth, precision, zeroPad, customPadChar) {
56 if (precision != null) {
57 value = value.slice(0, precision);
59 return justify(value, '', leftJustify, minWidth, zeroPad, customPadChar);
63 var doFormat = function(substring, valueIndex, flags, minWidth, _, precision, type) {
70 if (substring == '%%') return '%';
73 var leftJustify = false, positivePrefix = '', zeroPad = false, prefixBaseX = false, customPadChar = ' ';
74 var flagsl = flags.length;
75 for (var j = 0; flags && j < flagsl; j++) switch (flags.charAt(j)) {
76 case ' ': positivePrefix = ' '; break;
77 case '+': positivePrefix = '+'; break;
78 case '-': leftJustify = true; break;
79 case "'": customPadChar = flags.charAt(j+1); break;
80 case '0': zeroPad = true; break;
81 case '#': prefixBaseX = true; break;
84 // parameters may be null, undefined, empty-string or real valued
85 // we want to ignore null, undefined and empty-string values
88 } else if (minWidth == '*') {
90 } else if (minWidth.charAt(0) == '*') {
91 minWidth = +a[minWidth.slice(1, -1)];
96 // Note: undocumented perl feature:
102 if (!isFinite(minWidth)) {
103 throw new Error('sprintf: (minimum-)width must be finite');
107 precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type == 'd') ? 0 : void(0);
108 } else if (precision == '*') {
110 } else if (precision.charAt(0) == '*') {
111 precision = +a[precision.slice(1, -1)];
113 precision = +precision;
116 // grab value using valueIndex if required?
117 value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];
120 case 's': return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar);
121 case 'c': return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);
122 case 'b': return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
123 case 'o': return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
124 case 'x': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
125 case 'X': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad).toUpperCase();
126 case 'u': return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
129 number = parseInt(+value);
130 prefix = number < 0 ? '-' : positivePrefix;
131 value = prefix + pad(String(Math.abs(number)), precision, '0', false);
132 return justify(value, prefix, leftJustify, minWidth, zeroPad);
141 prefix = number < 0 ? '-' : positivePrefix;
142 method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];
143 textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];
144 value = prefix + Math.abs(number)[method](precision);
145 return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform]();
147 default: return substring;
151 return format.replace(regex, doFormat);