sprintf.js 6.8 KB

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