source: trunk/admin/themes/default/js/common.js @ 25194

Last change on this file since 25194 was 25194, checked in by plg, 10 years ago

feature 1668, in progress: redesign user manager (jQuery datatables, AJAX calls)

File size: 1.6 KB
Line 
1function array_delete(arr, item) {
2  var i = arr.indexOf(item);
3  if (i != -1) arr.splice(i, 1);
4}
5
6function sprintf() {
7  var i = 0, a, f = arguments[i++], o = [], m, p, c, x, s = '';
8  while (f) {
9    if (m = /^[^\x25]+/.exec(f)) {
10      o.push(m[0]);
11    }
12    else if (m = /^\x25{2}/.exec(f)) {
13      o.push('%');
14    }
15    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
16      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) {
17        throw('Too few arguments.');
18      }
19      if (/[^s]/.test(m[7]) && (typeof(a) != 'number')) {
20        throw('Expecting number but found ' + typeof(a));
21      }
22
23      switch (m[7]) {
24        case 'b': a = a.toString(2); break;
25        case 'c': a = String.fromCharCode(a); break;
26        case 'd': a = parseInt(a); break;
27        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
28        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
29        case 'o': a = a.toString(8); break;
30        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
31        case 'u': a = Math.abs(a); break;
32        case 'x': a = a.toString(16); break;
33        case 'X': a = a.toString(16).toUpperCase(); break;
34      }
35
36      a = (/[def]/.test(m[7]) && m[2] && a >= 0 ? '+'+ a : a);
37      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
38      x = m[5] - String(a).length - s.length;
39      p = m[5] ? str_repeat(c, x) : '';
40      o.push(s + (m[4] ? a + p : p + a));
41    }
42    else {
43      throw('Huh ?!');
44    }
45
46    f = f.substring(m[0].length);
47  }
48
49  return o.join('');
50}
Note: See TracBrowser for help on using the repository browser.