sprintf-0.6.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. sprintf() for JavaScript 0.6
  3. Copyright (c) Alexandru Marasteanu <alexaholic [at) gmail (dot] com>
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of sprintf() for JavaScript nor the
  13. names of its contributors may be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL Alexandru Marasteanu BE LIABLE FOR ANY
  19. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. Changelog:
  26. 2007.04.03 - 0.1:
  27. - initial release
  28. 2007.09.11 - 0.2:
  29. - feature: added argument swapping
  30. 2007.09.17 - 0.3:
  31. - bug fix: no longer throws exception on empty paramenters (Hans Pufal)
  32. 2007.10.21 - 0.4:
  33. - unit test and patch (David Baird)
  34. 2010.05.09 - 0.5:
  35. - bug fix: 0 is now preceeded with a + sign
  36. - bug fix: the sign was not at the right position on padded results (Kamal Abdali)
  37. - switched from GPL to BSD license
  38. 2010.05.22 - 0.6:
  39. - reverted to 0.4 and fixed the bug regarding the sign of the number 0
  40. Note:
  41. Thanks to Raphael Pigulla <raph (at] n3rd [dot) org> (http://www.n3rd.org/)
  42. who warned me about a bug in 0.5, I discovered that the last update was
  43. a regress. I appologize for that.
  44. **/
  45. function str_repeat(i, m) {
  46. for (var o = []; m > 0; o[--m] = i);
  47. return o.join('');
  48. }
  49. function sprintf() {
  50. var i = 0, a, f = arguments[i++], o = [], m, p, c, x, s = '';
  51. while (f) {
  52. if (m = /^[^\x25]+/.exec(f)) {
  53. o.push(m[0]);
  54. }
  55. else if (m = /^\x25{2}/.exec(f)) {
  56. o.push('%');
  57. }
  58. else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
  59. if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) {
  60. throw('Too few arguments.');
  61. }
  62. if (/[^s]/.test(m[7]) && (typeof(a) != 'number')) {
  63. throw('Expecting number but found ' + typeof(a));
  64. }
  65. switch (m[7]) {
  66. case 'b': a = a.toString(2); break;
  67. case 'c': a = String.fromCharCode(a); break;
  68. case 'd': a = parseInt(a); break;
  69. case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
  70. case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
  71. case 'o': a = a.toString(8); break;
  72. case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
  73. case 'u': a = Math.abs(a); break;
  74. case 'x': a = a.toString(16); break;
  75. case 'X': a = a.toString(16).toUpperCase(); break;
  76. }
  77. a = (/[def]/.test(m[7]) && m[2] && a >= 0 ? '+'+ a : a);
  78. c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
  79. x = m[5] - String(a).length - s.length;
  80. p = m[5] ? str_repeat(c, x) : '';
  81. o.push(s + (m[4] ? a + p : p + a));
  82. }
  83. else {
  84. throw('Huh ?!');
  85. }
  86. f = f.substring(m[0].length);
  87. }
  88. return o.join('');
  89. }