File Coverage

lib/Amon2/Setup/Asset/SprintfJS.pm
Criterion Covered Total %
statement 7 8 87.5
branch n/a
condition n/a
subroutine 3 4 75.0
pod 0 2 0.0
total 10 14 71.4


line stmt bran cond sub pod time code
1             # This file is generated by author/assets.pl. Do not edit manually.
2             use strict;
3 1     1   341 use warnings;
  1         2  
  1         28  
4 1     1   5  
  1         2  
  1         144  
5             <<',,,';
6             <script src="<: uri_for('/static/js/sprintf.js') :>"></script>
7 1     1 0 5 ,,,
8             }
9              
10             return {
11             'js/sprintf.js' => '/**
12             sprintf() for JavaScript 0.7-beta1
13             http://www.diveintojavascript.com/projects/javascript-sprintf
14 0     0 0    
15             Copyright (c) Alexandru Marasteanu <alexaholic [at) gmail (dot] com>
16             All rights reserved.
17              
18             Redistribution and use in source and binary forms, with or without
19             modification, are permitted provided that the following conditions are met:
20             * Redistributions of source code must retain the above copyright
21             notice, this list of conditions and the following disclaimer.
22             * Redistributions in binary form must reproduce the above copyright
23             notice, this list of conditions and the following disclaimer in the
24             documentation and/or other materials provided with the distribution.
25             * Neither the name of sprintf() for JavaScript nor the
26             names of its contributors may be used to endorse or promote products
27             derived from this software without specific prior written permission.
28              
29             THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
30             ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32             DISCLAIMED. IN NO EVENT SHALL Alexandru Marasteanu BE LIABLE FOR ANY
33             DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34             (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35             LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36             ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37             (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38             SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39              
40              
41             Changelog:
42             2010.09.06 - 0.7-beta1
43             - features: vsprintf, support for named placeholders
44             - enhancements: format cache, reduced global namespace pollution
45              
46             2010.05.22 - 0.6:
47             - reverted to 0.4 and fixed the bug regarding the sign of the number 0
48             Note:
49             Thanks to Raphael Pigulla <raph (at] n3rd [dot) org> (http://www.n3rd.org/)
50             who warned me about a bug in 0.5, I discovered that the last update was
51             a regress. I appologize for that.
52              
53             2010.05.09 - 0.5:
54             - bug fix: 0 is now preceeded with a + sign
55             - bug fix: the sign was not at the right position on padded results (Kamal Abdali)
56             - switched from GPL to BSD license
57              
58             2007.10.21 - 0.4:
59             - unit test and patch (David Baird)
60              
61             2007.09.17 - 0.3:
62             - bug fix: no longer throws exception on empty paramenters (Hans Pufal)
63              
64             2007.09.11 - 0.2:
65             - feature: added argument swapping
66              
67             2007.04.03 - 0.1:
68             - initial release
69             **/
70              
71             var sprintf = (function() {
72             function get_type(variable) {
73             return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase();
74             }
75             function str_repeat(input, multiplier) {
76             for (var output = []; multiplier > 0; output[--multiplier] = input) {/* do nothing */}
77             return output.join(\'\');
78             }
79              
80             var str_format = function() {
81             if (!str_format.cache.hasOwnProperty(arguments[0])) {
82             str_format.cache[arguments[0]] = str_format.parse(arguments[0]);
83             }
84             return str_format.format.call(null, str_format.cache[arguments[0]], arguments);
85             };
86              
87             str_format.format = function(parse_tree, argv) {
88             var cursor = 1, tree_length = parse_tree.length, node_type = \'\', arg, output = [], i, k, match, pad, pad_character, pad_length;
89             for (i = 0; i < tree_length; i++) {
90             node_type = get_type(parse_tree[i]);
91             if (node_type === \'string\') {
92             output.push(parse_tree[i]);
93             }
94             else if (node_type === \'array\') {
95             match = parse_tree[i]; // convenience purposes only
96             if (match[2]) { // keyword argument
97             arg = argv[cursor];
98             for (k = 0; k < match[2].length; k++) {
99             if (!arg.hasOwnProperty(match[2][k])) {
100             throw(sprintf(\'[sprintf] property "%s" does not exist\', match[2][k]));
101             }
102             arg = arg[match[2][k]];
103             }
104             }
105             else if (match[1]) { // positional argument (explicit)
106             arg = argv[match[1]];
107             }
108             else { // positional argument (implicit)
109             arg = argv[cursor++];
110             }
111              
112             if (/[^s]/.test(match[8]) && (get_type(arg) != \'number\')) {
113             throw(sprintf(\'[sprintf] expecting number but found %s\', get_type(arg)));
114             }
115             switch (match[8]) {
116             case \'b\': arg = arg.toString(2); break;
117             case \'c\': arg = String.fromCharCode(arg); break;
118             case \'d\': arg = parseInt(arg, 10); break;
119             case \'e\': arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential(); break;
120             case \'f\': arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg); break;
121             case \'o\': arg = arg.toString(8); break;
122             case \'s\': arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg); break;
123             case \'u\': arg = Math.abs(arg); break;
124             case \'x\': arg = arg.toString(16); break;
125             case \'X\': arg = arg.toString(16).toUpperCase(); break;
126             }
127             arg = (/[def]/.test(match[8]) && match[3] && arg >= 0 ? \'+\'+ arg : arg);
128             pad_character = match[4] ? match[4] == \'0\' ? \'0\' : match[4].charAt(1) : \' \';
129             pad_length = match[6] - String(arg).length;
130             pad = match[6] ? str_repeat(pad_character, pad_length) : \'\';
131             output.push(match[5] ? arg + pad : pad + arg);
132             }
133             }
134             return output.join(\'\');
135             };
136              
137             str_format.cache = {};
138              
139             str_format.parse = function(fmt) {
140             var _fmt = fmt, match = [], parse_tree = [], arg_names = 0;
141             while (_fmt) {
142             if ((match = /^[^\\x25]+/.exec(_fmt)) !== null) {
143             parse_tree.push(match[0]);
144             }
145             else if ((match = /^\\x25{2}/.exec(_fmt)) !== null) {
146             parse_tree.push(\'%\');
147             }
148             else if ((match = /^\\x25(?:([1-9]\\d*)\\$|\\(([^\\)]+)\\))?(\\+)?(0|\'[^$])?(-)?(\\d+)?(?:\\.(\\d+))?([b-fosuxX])/.exec(_fmt)) !== null) {
149             if (match[2]) {
150             arg_names |= 1;
151             var field_list = [], replacement_field = match[2], field_match = [];
152             if ((field_match = /^([a-z_][a-z_\\d]*)/i.exec(replacement_field)) !== null) {
153             field_list.push(field_match[1]);
154             while ((replacement_field = replacement_field.substring(field_match[0].length)) !== \'\') {
155             if ((field_match = /^\\.([a-z_][a-z_\\d]*)/i.exec(replacement_field)) !== null) {
156             field_list.push(field_match[1]);
157             }
158             else if ((field_match = /^\\[(\\d+)\\]/.exec(replacement_field)) !== null) {
159             field_list.push(field_match[1]);
160             }
161             else {
162             throw(\'[sprintf] huh?\');
163             }
164             }
165             }
166             else {
167             throw(\'[sprintf] huh?\');
168             }
169             match[2] = field_list;
170             }
171             else {
172             arg_names |= 2;
173             }
174             if (arg_names === 3) {
175             throw(\'[sprintf] mixing positional and named placeholders is not (yet) supported\');
176             }
177             parse_tree.push(match);
178             }
179             else {
180             throw(\'[sprintf] huh?\');
181             }
182             _fmt = _fmt.substring(match[0].length);
183             }
184             return parse_tree;
185             };
186              
187             return str_format;
188             })();
189              
190             var vsprintf = function(fmt, argv) {
191             argv.unshift(fmt);
192             return sprintf.apply(null, argv);
193             };
194             '
195             }
196             ;
197             }
198              
199             1;