sprintf.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // MIT-licensed
  2. function sprintf ( ) {
  3. // Return a formatted string
  4. //
  5. // version: 909.322
  6. // discuss at: http://phpjs.org/functions/sprintf
  7. // + original by: Ash Searle (http://hexmen.com/blog/)
  8. // + namespaced by: Michael White (http://getsprink.com)
  9. // + tweaked by: Jack
  10. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  11. // + input by: Paulo Ricardo F. Santos
  12. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  13. // + input by: Brett Zamir (http://brett-zamir.me)
  14. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  15. // * example 1: sprintf("%01.2f", 123.1);
  16. // * returns 1: 123.10
  17. // * example 2: sprintf("[%10s]", 'monkey');
  18. // * returns 2: '[ monkey]'
  19. // * example 3: sprintf("[%'#10s]", 'monkey');
  20. // * returns 3: '[####monkey]'
  21. var regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuidfegEG])/g;
  22. var a = arguments, i = 0, format = a[i++];
  23. // pad()
  24. var pad = function (str, len, chr, leftJustify) {
  25. if (!chr) {chr = ' ';}
  26. var padding = (str.length >= len) ? '' : Array(1 + len - str.length >>> 0).join(chr);
  27. return leftJustify ? str + padding : padding + str;
  28. };
  29. // justify()
  30. var justify = function (value, prefix, leftJustify, minWidth, zeroPad, customPadChar) {
  31. var diff = minWidth - value.length;
  32. if (diff > 0) {
  33. if (leftJustify || !zeroPad) {
  34. value = pad(value, minWidth, customPadChar, leftJustify);
  35. } else {
  36. value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length);
  37. }
  38. }
  39. return value;
  40. };
  41. // formatBaseX()
  42. var formatBaseX = function (value, base, prefix, leftJustify, minWidth, precision, zeroPad) {
  43. // Note: casts negative numbers to positive ones
  44. var number = value >>> 0;
  45. prefix = prefix && number && {'2': '0b', '8': '0', '16': '0x'}[base] || '';
  46. value = prefix + pad(number.toString(base), precision || 0, '0', false);
  47. return justify(value, prefix, leftJustify, minWidth, zeroPad);
  48. };
  49. // formatString()
  50. var formatString = function (value, leftJustify, minWidth, precision, zeroPad, customPadChar) {
  51. if (precision != null) {
  52. value = value.slice(0, precision);
  53. }
  54. return justify(value, '', leftJustify, minWidth, zeroPad, customPadChar);
  55. };
  56. // doFormat()
  57. var doFormat = function (substring, valueIndex, flags, minWidth, _, precision, type) {
  58. var number;
  59. var prefix;
  60. var method;
  61. var textTransform;
  62. var value;
  63. if (substring == '%%') {return '%';}
  64. // parse flags
  65. var leftJustify = false, positivePrefix = '', zeroPad = false, prefixBaseX = false, customPadChar = ' ';
  66. var flagsl = flags.length;
  67. for (var j = 0; flags && j < flagsl; j++) {
  68. switch (flags.charAt(j)) {
  69. case ' ': positivePrefix = ' '; break;
  70. case '+': positivePrefix = '+'; break;
  71. case '-': leftJustify = true; break;
  72. case "'": customPadChar = flags.charAt(j+1); break;
  73. case '0': zeroPad = true; break;
  74. case '#': prefixBaseX = true; break;
  75. }
  76. }
  77. // parameters may be null, undefined, empty-string or real valued
  78. // we want to ignore null, undefined and empty-string values
  79. if (!minWidth) {
  80. minWidth = 0;
  81. } else if (minWidth == '*') {
  82. minWidth = +a[i++];
  83. } else if (minWidth.charAt(0) == '*') {
  84. minWidth = +a[minWidth.slice(1, -1)];
  85. } else {
  86. minWidth = +minWidth;
  87. }
  88. // Note: undocumented perl feature:
  89. if (minWidth < 0) {
  90. minWidth = -minWidth;
  91. leftJustify = true;
  92. }
  93. if (!isFinite(minWidth)) {
  94. throw new Error('sprintf: (minimum-)width must be finite');
  95. }
  96. if (!precision) {
  97. precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type == 'd') ? 0 : undefined;
  98. } else if (precision == '*') {
  99. precision = +a[i++];
  100. } else if (precision.charAt(0) == '*') {
  101. precision = +a[precision.slice(1, -1)];
  102. } else {
  103. precision = +precision;
  104. }
  105. // grab value using valueIndex if required?
  106. value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];
  107. switch (type) {
  108. case 's': return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar);
  109. case 'c': return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);
  110. case 'b': return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  111. case 'o': return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  112. case 'x': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  113. case 'X': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad).toUpperCase();
  114. case 'u': return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  115. case 'i':
  116. case 'd':
  117. number = parseInt(+value, 10);
  118. prefix = number < 0 ? '-' : positivePrefix;
  119. value = prefix + pad(String(Math.abs(number)), precision, '0', false);
  120. return justify(value, prefix, leftJustify, minWidth, zeroPad);
  121. case 'e':
  122. case 'E':
  123. case 'f':
  124. case 'F':
  125. case 'g':
  126. case 'G':
  127. number = +value;
  128. prefix = number < 0 ? '-' : positivePrefix;
  129. method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];
  130. textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];
  131. value = prefix + Math.abs(number)[method](precision);
  132. return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform]();
  133. default: return substring;
  134. }
  135. };
  136. return format.replace(regex, doFormat);
  137. }