sprintf.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. function sprintf() {
  2. /*
  3. * Copyright (c) 2013 Kevin van Zonneveld (http://kvz.io)
  4. * and Contributors (http://phpjs.org/authors)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10. * of the Software, and to permit persons to whom the Software is furnished to do
  11. * so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. // discuss at: http://phpjs.org/functions/sprintf/
  25. // original by: Ash Searle (http://hexmen.com/blog/)
  26. // improved by: Michael White (http://getsprink.com)
  27. // improved by: Jack
  28. // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  29. // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  30. // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  31. // improved by: Dj
  32. // improved by: Allidylls
  33. // input by: Paulo Freitas
  34. // input by: Brett Zamir (http://brett-zamir.me)
  35. // example 1: sprintf("%01.2f", 123.1);
  36. // returns 1: 123.10
  37. // example 2: sprintf("[%10s]", 'monkey');
  38. // returns 2: '[ monkey]'
  39. // example 3: sprintf("[%'#10s]", 'monkey');
  40. // returns 3: '[####monkey]'
  41. // example 4: sprintf("%d", 123456789012345);
  42. // returns 4: '123456789012345'
  43. // example 5: sprintf('%-03s', 'E');
  44. // returns 5: 'E00'
  45. var regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuideEfFgG])/g;
  46. var a = arguments;
  47. var i = 0;
  48. var format = a[i++];
  49. // pad()
  50. var pad = function (str, len, chr, leftJustify) {
  51. if (!chr) {
  52. chr = ' ';
  53. }
  54. var padding = (str.length >= len) ? '' : new Array(1 + len - str.length >>> 0)
  55. .join(chr);
  56. return leftJustify ? str + padding : padding + str;
  57. };
  58. // justify()
  59. var justify = function (value, prefix, leftJustify, minWidth, zeroPad, customPadChar) {
  60. var diff = minWidth - value.length;
  61. if (diff > 0) {
  62. if (leftJustify || !zeroPad) {
  63. value = pad(value, minWidth, customPadChar, leftJustify);
  64. } else {
  65. value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length);
  66. }
  67. }
  68. return value;
  69. };
  70. // formatBaseX()
  71. var formatBaseX = function (value, base, prefix, leftJustify, minWidth, precision, zeroPad) {
  72. // Note: casts negative numbers to positive ones
  73. var number = value >>> 0;
  74. prefix = prefix && number && {
  75. '2': '0b',
  76. '8': '0',
  77. '16': '0x'
  78. }[base] || '';
  79. value = prefix + pad(number.toString(base), precision || 0, '0', false);
  80. return justify(value, prefix, leftJustify, minWidth, zeroPad);
  81. };
  82. // formatString()
  83. var formatString = function (value, leftJustify, minWidth, precision, zeroPad, customPadChar) {
  84. if (precision != null) {
  85. value = value.slice(0, precision);
  86. }
  87. return justify(value, '', leftJustify, minWidth, zeroPad, customPadChar);
  88. };
  89. // doFormat()
  90. var doFormat = function (substring, valueIndex, flags, minWidth, _, precision, type) {
  91. var number, prefix, method, textTransform, value;
  92. if (substring === '%%') {
  93. return '%';
  94. }
  95. // parse flags
  96. var leftJustify = false;
  97. var positivePrefix = '';
  98. var zeroPad = false;
  99. var prefixBaseX = false;
  100. var customPadChar = ' ';
  101. var flagsl = flags.length;
  102. for (var j = 0; flags && j < flagsl; j++) {
  103. switch (flags.charAt(j)) {
  104. case ' ':
  105. positivePrefix = ' ';
  106. break;
  107. case '+':
  108. positivePrefix = '+';
  109. break;
  110. case '-':
  111. leftJustify = true;
  112. break;
  113. case "'":
  114. customPadChar = flags.charAt(j + 1);
  115. break;
  116. case '0':
  117. zeroPad = true;
  118. customPadChar = '0';
  119. break;
  120. case '#':
  121. prefixBaseX = true;
  122. break;
  123. }
  124. }
  125. // parameters may be null, undefined, empty-string or real valued
  126. // we want to ignore null, undefined and empty-string values
  127. if (!minWidth) {
  128. minWidth = 0;
  129. } else if (minWidth === '*') {
  130. minWidth = +a[i++];
  131. } else if (minWidth.charAt(0) == '*') {
  132. minWidth = +a[minWidth.slice(1, -1)];
  133. } else {
  134. minWidth = +minWidth;
  135. }
  136. // Note: undocumented perl feature:
  137. if (minWidth < 0) {
  138. minWidth = -minWidth;
  139. leftJustify = true;
  140. }
  141. if (!isFinite(minWidth)) {
  142. throw new Error('sprintf: (minimum-)width must be finite');
  143. }
  144. if (!precision) {
  145. precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type === 'd') ? 0 : undefined;
  146. } else if (precision === '*') {
  147. precision = +a[i++];
  148. } else if (precision.charAt(0) == '*') {
  149. precision = +a[precision.slice(1, -1)];
  150. } else {
  151. precision = +precision;
  152. }
  153. // grab value using valueIndex if required?
  154. value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];
  155. switch (type) {
  156. case 's':
  157. return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar);
  158. case 'c':
  159. return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);
  160. case 'b':
  161. return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  162. case 'o':
  163. return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  164. case 'x':
  165. return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  166. case 'X':
  167. return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad)
  168. .toUpperCase();
  169. case 'u':
  170. return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  171. case 'i':
  172. case 'd':
  173. number = +value || 0;
  174. // Plain Math.round doesn't just truncate
  175. number = Math.round(number - number % 1);
  176. prefix = number < 0 ? '-' : positivePrefix;
  177. value = prefix + pad(String(Math.abs(number)), precision, '0', false);
  178. return justify(value, prefix, leftJustify, minWidth, zeroPad);
  179. case 'e':
  180. case 'E':
  181. case 'f': // Should handle locales (as per setlocale)
  182. case 'F':
  183. case 'g':
  184. case 'G':
  185. number = +value;
  186. prefix = number < 0 ? '-' : positivePrefix;
  187. method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];
  188. textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];
  189. value = prefix + Math.abs(number)[method](precision);
  190. return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform]();
  191. default:
  192. return substring;
  193. }
  194. };
  195. return format.replace(regex, doFormat);
  196. }