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