Source Code Documentation


s2member\includes\jquery\jquery.sprintf\jquery.sprintf.js


  1. /**
  2. * jQuery extension for PHP-equiv sprintf().
  3. *
  4. * This is the development version of the code.
  5. * Which ultimately produces `jquery.sprintf-min.js`.
  6. *
  7. * Based on: {@link http://phpjs.org/functions/sprintf:522} see: {@link http://phpjs.org/pages/license}
  8. * Modified to work via: `$.sprintf()` using jQuery.
  9. *
  10. * Distributed with WordPress® themes/plugins by WebSharks, Inc.
  11. *
  12. * Copyright: © 2009-2011
  13. * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
  14. * (coded in the USA)
  15. *
  16. * Released under the terms of the GNU General Public License.
  17. * You should have received a copy of the GNU General Public License,
  18. * along with this software. In the main directory, see: /licensing/
  19. * If not, see: {@link http://www.gnu.org/licenses/}.
  20. *
  21. * @package WebSharks\jQuery\Extensions
  22. * @since x.xx
  23. */
  24. (function($)
  25. {
  26. if (typeof $.sprintf !== 'function')
  27. {
  28. $.sprintf = function () {
  29. // http://kevin.vanzonneveld.net
  30. // + original by: Ash Searle (http://hexmen.com/blog/)
  31. // + namespaced by: Michael White (http://getsprink.com)
  32. // + tweaked by: Jack
  33. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  34. // + input by: Paulo Freitas
  35. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  36. // + input by: Brett Zamir (http://brett-zamir.me)
  37. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  38. // * example 1: sprintf("%01.2f", 123.1);
  39. // * returns 1: 123.10
  40. // * example 2: sprintf("[%10s]", 'monkey');
  41. // * returns 2: '[ monkey]'
  42. // * example 3: sprintf("[%'#10s]", 'monkey');
  43. // * returns 3: '[####monkey]'
  44. var regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuidfegEG])/g;
  45. var a = arguments,
  46. i = 0,
  47. format = a[i++];
  48.  
  49. // pad()
  50. var pad = function (str, len, chr, leftJustify) {
  51. if (!chr) {
  52. chr = ' ';
  53. }
  54. var padding = (str.length >= len) ? '' : Array(1 + len - str.length >>> 0).join(chr);
  55. return leftJustify ? str + padding : padding + str;
  56. };
  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.  
  71. // formatBaseX()
  72. var formatBaseX = function (value, base, prefix, leftJustify, minWidth, precision, zeroPad) {
  73. // Note: casts negative numbers to positive ones
  74. var number = value >>> 0;
  75. prefix = prefix && number && {
  76. '2': '0b',
  77. '8': '0',
  78. '16': '0x'
  79. }[base] || '';
  80. value = prefix + pad(number.toString(base), precision || 0, '0', false);
  81. return justify(value, prefix, leftJustify, minWidth, zeroPad);
  82. };
  83.  
  84. // formatString()
  85. var formatString = function (value, leftJustify, minWidth, precision, zeroPad, customPadChar) {
  86. if (precision != null) {
  87. value = value.slice(0, precision);
  88. }
  89. return justify(value, '', leftJustify, minWidth, zeroPad, customPadChar);
  90. };
  91.  
  92. // doFormat()
  93. var doFormat = function (substring, valueIndex, flags, minWidth, _, precision, type) {
  94. var number;
  95. var prefix;
  96. var method;
  97. var textTransform;
  98. var value;
  99.  
  100. if (substring == '%%') {
  101. return '%';
  102. }
  103.  
  104. // parse flags
  105. var leftJustify = false,
  106. positivePrefix = '',
  107. zeroPad = false,
  108. prefixBaseX = false,
  109. customPadChar = ' ';
  110. var flagsl = flags.length;
  111. for (var j = 0; flags && j < flagsl; j++) {
  112. switch (flags.charAt(j)) {
  113. case ' ':
  114. positivePrefix = ' ';
  115. break;
  116. case '+':
  117. positivePrefix = '+';
  118. break;
  119. case '-':
  120. leftJustify = true;
  121. break;
  122. case "'":
  123. customPadChar = flags.charAt(j + 1);
  124. break;
  125. case '0':
  126. zeroPad = true;
  127. break;
  128. case '#':
  129. prefixBaseX = true;
  130. break;
  131. }
  132. }
  133.  
  134. // parameters may be null, undefined, empty-string or real valued
  135. // we want to ignore null, undefined and empty-string values
  136. if (!minWidth) {
  137. minWidth = 0;
  138. } else if (minWidth == '*') {
  139. minWidth = +a[i++];
  140. } else if (minWidth.charAt(0) == '*') {
  141. minWidth = +a[minWidth.slice(1, -1)];
  142. } else {
  143. minWidth = +minWidth;
  144. }
  145.  
  146. // Note: undocumented perl feature:
  147. if (minWidth < 0) {
  148. minWidth = -minWidth;
  149. leftJustify = true;
  150. }
  151.  
  152. if (!isFinite(minWidth)) {
  153. throw new Error('sprintf: (minimum-)width must be finite');
  154. }
  155.  
  156. if (!precision) {
  157. precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type == 'd') ? 0 : undefined;
  158. } else if (precision == '*') {
  159. precision = +a[i++];
  160. } else if (precision.charAt(0) == '*') {
  161. precision = +a[precision.slice(1, -1)];
  162. } else {
  163. precision = +precision;
  164. }
  165.  
  166. // grab value using valueIndex if required?
  167. value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];
  168.  
  169. switch (type) {
  170. case 's':
  171. return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar);
  172. case 'c':
  173. return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);
  174. case 'b':
  175. return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  176. case 'o':
  177. return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  178. case 'x':
  179. return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  180. case 'X':
  181. return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad).toUpperCase();
  182. case 'u':
  183. return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  184. case 'i':
  185. case 'd':
  186. number = (+value) | 0;
  187. prefix = number < 0 ? '-' : positivePrefix;
  188. value = prefix + pad(String(Math.abs(number)), precision, '0', false);
  189. return justify(value, prefix, leftJustify, minWidth, zeroPad);
  190. case 'e':
  191. case 'E':
  192. case 'f':
  193. case 'F':
  194. case 'g':
  195. case 'G':
  196. number = +value;
  197. prefix = number < 0 ? '-' : positivePrefix;
  198. method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];
  199. textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];
  200. value = prefix + Math.abs(number)[method](precision);
  201. return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform]();
  202. default:
  203. return substring;
  204. }
  205. };
  206.  
  207. return format.replace(regex, doFormat);
  208. };
  209. }
  210. })(jQuery);

s2member\includes\jquery\jquery.sprintf\jquery.sprintf.js

Source Code Documentation