source: extensions/SmartAlbums/admin/template/sprintf.js @ 19446

Last change on this file since 19446 was 19446, checked in by mistic100, 11 years ago
  • add regex for phot name, author
  • add dimensions filter
  • rewrite javascript algorithms
  • add auto update on timeout (default 3 days)
  • display photos count on plugin albums list
File size: 2.2 KB
Line 
1function str_repeat(i, m) {
2        for (var o = []; m > 0; o[--m] = i);
3        return o.join('');
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                        switch (m[7]) {
23                                case 'b': a = a.toString(2); break;
24                                case 'c': a = String.fromCharCode(a); break;
25                                case 'd': a = parseInt(a); break;
26                                case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
27                                case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
28                                case 'o': a = a.toString(8); break;
29                                case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
30                                case 'u': a = Math.abs(a); break;
31                                case 'x': a = a.toString(16); break;
32                                case 'X': a = a.toString(16).toUpperCase(); break;
33                        }
34                        a = (/[def]/.test(m[7]) && m[2] && a >= 0 ? '+'+ a : a);
35                        c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
36                        x = m[5] - String(a).length - s.length;
37                        p = m[5] ? str_repeat(c, x) : '';
38                        o.push(s + (m[4] ? a + p : p + a));
39                }
40                else {
41                        throw('Huh ?!');
42                }
43                f = f.substring(m[0].length);
44        }
45        return o.join('');
46}
Note: See TracBrowser for help on using the repository browser.