comparison static/sprintf.js @ 13:a0213f0e707b

- Update for v12.10 - Plot raw data to show more up to date data - Average/peak winds so they don't spam the display. - Display the time of the most recent data.
author Daniel O'Connor <darius@dons.net.au>
date Thu, 10 Jan 2013 16:38:36 +1030
parents
children
comparison
equal deleted inserted replaced
12:bfe6ed563ffc 13:a0213f0e707b
1 /**
2 *
3 * Javascript sprintf
4 * http://www.webtoolkit.info/
5 *
6 *
7 **/
8
9 sprintfWrapper = {
10 init : function () {
11 if (typeof arguments == "undefined") { return null; }
12 if (arguments.length < 1) { return null; }
13 if (typeof arguments[0] != "string") { return null; }
14 if (typeof RegExp == "undefined") { return null; }
15
16 var string = arguments[0];
17 var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
18 var matches = new Array();
19 var strings = new Array();
20 var convCount = 0;
21 var stringPosStart = 0;
22 var stringPosEnd = 0;
23 var matchPosEnd = 0;
24 var newString = '';
25 var match = null;
26
27 while (match = exp.exec(string)) {
28 if (match[9]) { convCount += 1; }
29
30 stringPosStart = matchPosEnd;
31 stringPosEnd = exp.lastIndex - match[0].length;
32 strings[strings.length] = string.substring(stringPosStart, stringPosEnd);
33
34 matchPosEnd = exp.lastIndex;
35 matches[matches.length] = {
36 match: match[0],
37 left: match[3] ? true : false,
38 sign: match[4] || '',
39 pad: match[5] || ' ',
40 min: match[6] || 0,
41 precision: match[8],
42 code: match[9] || '%',
43 negative: parseInt(arguments[convCount]) < 0 ? true : false,
44 argument: String(arguments[convCount])
45 };
46 }
47 strings[strings.length] = string.substring(matchPosEnd);
48
49 if (matches.length == 0) { return string; }
50 if ((arguments.length - 1) < convCount) { return null; }
51
52 var code = null;
53 var match = null;
54 var i = null;
55
56 for (i=0; i<matches.length; i++) {
57
58 if (matches[i].code == '%') { substitution = '%' }
59 else if (matches[i].code == 'b') {
60 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
61 substitution = sprintfWrapper.convert(matches[i], true);
62 }
63 else if (matches[i].code == 'c') {
64 matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
65 substitution = sprintfWrapper.convert(matches[i], true);
66 }
67 else if (matches[i].code == 'd') {
68 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
69 substitution = sprintfWrapper.convert(matches[i]);
70 }
71 else if (matches[i].code == 'f') {
72 matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
73 substitution = sprintfWrapper.convert(matches[i]);
74 }
75 else if (matches[i].code == 'o') {
76 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
77 substitution = sprintfWrapper.convert(matches[i]);
78 }
79 else if (matches[i].code == 's') {
80 matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
81 substitution = sprintfWrapper.convert(matches[i], true);
82 }
83 else if (matches[i].code == 'x') {
84 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
85 substitution = sprintfWrapper.convert(matches[i]);
86 }
87 else if (matches[i].code == 'X') {
88 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
89 substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
90 }
91 else {
92 substitution = matches[i].match;
93 }
94
95 newString += strings[i];
96 newString += substitution;
97
98 }
99 newString += strings[i];
100
101 return newString;
102
103 },
104
105 convert : function(match, nosign){
106 if (nosign) {
107 match.sign = '';
108 } else {
109 match.sign = match.negative ? '-' : match.sign;
110 }
111 var l = match.min - match.argument.length + 1 - match.sign.length;
112 var pad = new Array(l < 0 ? 0 : l).join(match.pad);
113 if (!match.left) {
114 if (match.pad == "0" || nosign) {
115 return match.sign + pad + match.argument;
116 } else {
117 return pad + match.sign + match.argument;
118 }
119 } else {
120 if (match.pad == "0" || nosign) {
121 return match.sign + match.argument + pad.replace(/0/g, ' ');
122 } else {
123 return match.sign + match.argument + pad;
124 }
125 }
126 }
127 }
128
129 sprintf = sprintfWrapper.init;
130