Several programming languages implement a sprintf function, to output a formatted string. It originated from the C programming language, printf function. Its a string manipulation function.
This is limited
Javascript sprintf implementation. Function returns a string formatted by the usual printf conventions.
More code snippets you can find on
free code and tutorials website.
1
2 /**
3 *
4 * Javascript sprintf
5 * http://www.webtoolkit.info/
6 *
7 *
8 **/
9
10 sprintfWrapper = {
11
12 init : function () {
13
14 if (typeof arguments == "undefined") { return null; }
15 if (arguments.length < 1) { return null; }
16 if (typeof arguments[0] != "string") { return null; }
17 if (typeof RegExp == "undefined") { return null; }
18
19 var string = arguments[0];
20 var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
21 var matches = new Array();
22 var strings = new Array();
23 var convCount = 0;
24 var stringPosStart = 0;
25 var stringPosEnd = 0;
26 var matchPosEnd = 0;
27 var newString = '';
28 var match = null;
29
30 while (match = exp.exec(string)) {
31 if (match[9]) { convCount += 1; }
32
33 stringPosStart = matchPosEnd;
34 stringPosEnd = exp.lastIndex - match[0].length;
35 strings[strings.length] = string.substring(stringPosStart, stringPosEnd);
36
37 matchPosEnd = exp.lastIndex;
38 matches[matches.length] = {
39 match: match[0],
40 left: match[3] ? true : false,
41 sign: match[4] || '',
42 pad: match[5] || ' ',
43 min: match[6] || 0,
44 precision: match[8],
45 code: match[9] || '%',
46 negative: parseInt(arguments[convCount]) < 0 ? true : false,
47 argument: String(arguments[convCount])
48 };
49 }
50 strings[strings.length] = string.substring(matchPosEnd);
51
52 if (matches.length == 0) { return string; }
53 if ((arguments.length - 1) < convCount) { return null; }
54
55 var code = null;
56 var match = null;
57 var i = null;
58
59 for (i=0; i<matches.length; i++) {
60
61 if (matches[i].code == '%') { substitution = '%' }
62 else if (matches[i].code == 'b') {
63 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
64 substitution = sprintfWrapper.convert(matches[i], true);
65 }
66 else if (matches[i].code == 'c') {
67 matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
68 substitution = sprintfWrapper.convert(matches[i], true);
69 }
70 else if (matches[i].code == 'd') {
71 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
72 substitution = sprintfWrapper.convert(matches[i]);
73 }
74 else if (matches[i].code == 'f') {
75 matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
76 substitution = sprintfWrapper.convert(matches[i]);
77 }
78 else if (matches[i].code == 'o') {
79 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
80 substitution = sprintfWrapper.convert(matches[i]);
81 }
82 else if (matches[i].code == 's') {
83 matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
84 substitution = sprintfWrapper.convert(matches[i], true);
85 }
86 else if (matches[i].code == 'x') {
87 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
88 substitution = sprintfWrapper.convert(matches[i]);
89 }
90 else if (matches[i].code == 'X') {
91 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
92 substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
93 }
94 else {
95 substitution = matches[i].match;
96 }
97
98 newString += strings[i];
99 newString += substitution;
100
101 }
102 newString += strings[i];
103
104 return newString;
105
106 },
107
108 convert : function(match, nosign){
109 if (nosign) {
110 match.sign = '';
111 } else {
112 match.sign = match.negative ? '-' : match.sign;
113 }
114 var l = match.min - match.argument.length + 1 - match.sign.length;
115 var pad = new Array(l < 0 ? 0 : l).join(match.pad);
116 if (!match.left) {
117 if (match.pad == "0" || nosign) {
118 return match.sign + pad + match.argument;
119 } else {
120 return pad + match.sign + match.argument;
121 }
122 } else {
123 if (match.pad == "0" || nosign) {
124 return match.sign + match.argument + pad.replace(/0/g, ' ');
125 } else {
126 return match.sign + match.argument + pad;
127 }
128 }
129 }
130 }
131
132 sprintf = sprintfWrapper.init;