PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/js/vendor/sprintf.js

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