sprintf.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * Copyright (c) 2007-2016, Alexandru Marasteanu <hello [at) alexei (dot] ro>
  3. * All rights reserved.
  4. *
  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 this software nor the names of its contributors may be
  13. * used to endorse or promote products derived from this software without
  14. * specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. /* globals window, exports, define */
  29. (function(window) {
  30. 'use strict'
  31. var re = {
  32. not_string: /[^s]/,
  33. not_bool: /[^t]/,
  34. not_type: /[^T]/,
  35. not_primitive: /[^v]/,
  36. number: /[diefg]/,
  37. numeric_arg: /bcdiefguxX/,
  38. json: /[j]/,
  39. not_json: /[^j]/,
  40. text: /^[^\x25]+/,
  41. modulo: /^\x25{2}/,
  42. placeholder: /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijosStTuvxX])/,
  43. key: /^([a-z_][a-z_\d]*)/i,
  44. key_access: /^\.([a-z_][a-z_\d]*)/i,
  45. index_access: /^\[(\d+)\]/,
  46. sign: /^[\+\-]/
  47. }
  48. function sprintf() {
  49. var key = arguments[0], cache = sprintf.cache
  50. if (!(cache[key] && cache.hasOwnProperty(key))) {
  51. cache[key] = sprintf.parse(key)
  52. }
  53. return sprintf.format.call(null, cache[key], arguments)
  54. }
  55. sprintf.format = function(parse_tree, argv) {
  56. var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length, is_positive = true, sign = ''
  57. for (i = 0; i < tree_length; i++) {
  58. node_type = get_type(parse_tree[i])
  59. if (node_type === 'string') {
  60. output[output.length] = parse_tree[i]
  61. }
  62. else if (node_type === 'array') {
  63. match = parse_tree[i] // convenience purposes only
  64. if (match[2]) { // keyword argument
  65. arg = argv[cursor]
  66. for (k = 0; k < match[2].length; k++) {
  67. if (!arg.hasOwnProperty(match[2][k])) {
  68. throw new Error(sprintf('[sprintf] property "%s" does not exist', match[2][k]))
  69. }
  70. arg = arg[match[2][k]]
  71. }
  72. }
  73. else if (match[1]) { // positional argument (explicit)
  74. arg = argv[match[1]]
  75. }
  76. else { // positional argument (implicit)
  77. arg = argv[cursor++]
  78. }
  79. if (re.not_type.test(match[8]) && re.not_primitive.test(match[8]) && get_type(arg) == 'function') {
  80. arg = arg()
  81. }
  82. if (re.numeric_arg.test(match[8]) && (get_type(arg) != 'number' && isNaN(arg))) {
  83. throw new TypeError(sprintf("[sprintf] expecting number but found %s", get_type(arg)))
  84. }
  85. if (re.number.test(match[8])) {
  86. is_positive = arg >= 0
  87. }
  88. switch (match[8]) {
  89. case 'b':
  90. arg = parseInt(arg, 10).toString(2)
  91. break
  92. case 'c':
  93. arg = String.fromCharCode(parseInt(arg, 10))
  94. break
  95. case 'd':
  96. case 'i':
  97. arg = parseInt(arg, 10)
  98. break
  99. case 'j':
  100. arg = JSON.stringify(arg, null, match[6] ? parseInt(match[6]) : 0)
  101. break
  102. case 'e':
  103. arg = match[7] ? parseFloat(arg).toExponential(match[7]) : parseFloat(arg).toExponential()
  104. break
  105. case 'f':
  106. arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg)
  107. break
  108. case 'g':
  109. arg = match[7] ? parseFloat(arg).toPrecision(match[7]) : parseFloat(arg)
  110. break
  111. case 'o':
  112. arg = arg.toString(8)
  113. break
  114. case 's':
  115. case 'S':
  116. arg = String(arg)
  117. arg = (match[7] ? arg.substring(0, match[7]) : arg)
  118. break
  119. case 't':
  120. arg = String(!!arg)
  121. arg = (match[7] ? arg.substring(0, match[7]) : arg)
  122. break
  123. case 'T':
  124. arg = get_type(arg)
  125. arg = (match[7] ? arg.substring(0, match[7]) : arg)
  126. break
  127. case 'u':
  128. arg = parseInt(arg, 10) >>> 0
  129. break
  130. case 'v':
  131. arg = arg.valueOf()
  132. arg = (match[7] ? arg.substring(0, match[7]) : arg)
  133. break
  134. case 'x':
  135. arg = parseInt(arg, 10).toString(16)
  136. break
  137. case 'X':
  138. arg = parseInt(arg, 10).toString(16).toUpperCase()
  139. break
  140. }
  141. if (re.json.test(match[8])) {
  142. output[output.length] = arg
  143. }
  144. else {
  145. if (re.number.test(match[8]) && (!is_positive || match[3])) {
  146. sign = is_positive ? '+' : '-'
  147. arg = arg.toString().replace(re.sign, '')
  148. }
  149. else {
  150. sign = ''
  151. }
  152. pad_character = match[4] ? match[4] === '0' ? '0' : match[4].charAt(1) : ' '
  153. pad_length = match[6] - (sign + arg).length
  154. pad = match[6] ? (pad_length > 0 ? str_repeat(pad_character, pad_length) : '') : ''
  155. output[output.length] = match[5] ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg)
  156. }
  157. }
  158. }
  159. return output.join('')
  160. }
  161. sprintf.cache = {}
  162. sprintf.parse = function(fmt) {
  163. var _fmt = fmt, match = [], parse_tree = [], arg_names = 0
  164. while (_fmt) {
  165. if ((match = re.text.exec(_fmt)) !== null) {
  166. parse_tree[parse_tree.length] = match[0]
  167. }
  168. else if ((match = re.modulo.exec(_fmt)) !== null) {
  169. parse_tree[parse_tree.length] = '%'
  170. }
  171. else if ((match = re.placeholder.exec(_fmt)) !== null) {
  172. if (match[2]) {
  173. arg_names |= 1
  174. var field_list = [], replacement_field = match[2], field_match = []
  175. if ((field_match = re.key.exec(replacement_field)) !== null) {
  176. field_list[field_list.length] = field_match[1]
  177. while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
  178. if ((field_match = re.key_access.exec(replacement_field)) !== null) {
  179. field_list[field_list.length] = field_match[1]
  180. }
  181. else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
  182. field_list[field_list.length] = field_match[1]
  183. }
  184. else {
  185. throw new SyntaxError("[sprintf] failed to parse named argument key")
  186. }
  187. }
  188. }
  189. else {
  190. throw new SyntaxError("[sprintf] failed to parse named argument key")
  191. }
  192. match[2] = field_list
  193. }
  194. else {
  195. arg_names |= 2
  196. }
  197. if (arg_names === 3) {
  198. throw new Error("[sprintf] mixing positional and named placeholders is not (yet) supported")
  199. }
  200. parse_tree[parse_tree.length] = match
  201. }
  202. else {
  203. throw new SyntaxError("[sprintf] unexpected placeholder")
  204. }
  205. _fmt = _fmt.substring(match[0].length)
  206. }
  207. return parse_tree
  208. }
  209. var vsprintf = function(fmt, argv, _argv) {
  210. _argv = (argv || []).slice(0)
  211. _argv.splice(0, 0, fmt)
  212. return sprintf.apply(null, _argv)
  213. }
  214. /**
  215. * helpers
  216. */
  217. function get_type(variable) {
  218. if (typeof variable === 'number') {
  219. return 'number'
  220. }
  221. else if (typeof variable === 'string') {
  222. return 'string'
  223. }
  224. else {
  225. return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase()
  226. }
  227. }
  228. var preformattedPadding = {
  229. '0': ['', '0', '00', '000', '0000', '00000', '000000', '0000000'],
  230. ' ': ['', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
  231. '_': ['', '_', '__', '___', '____', '_____', '______', '_______'],
  232. }
  233. function str_repeat(input, multiplier) {
  234. if (multiplier >= 0 && multiplier <= 7 && preformattedPadding[input]) {
  235. return preformattedPadding[input][multiplier]
  236. }
  237. return Array(multiplier + 1).join(input)
  238. }
  239. /**
  240. * export to either browser or node.js
  241. */
  242. if (typeof exports !== 'undefined') {
  243. exports.sprintf = sprintf
  244. exports.vsprintf = vsprintf
  245. }
  246. else {
  247. window.sprintf = sprintf
  248. window.vsprintf = vsprintf
  249. if (typeof define === 'function' && define.amd) {
  250. define(function() {
  251. return {
  252. sprintf: sprintf,
  253. vsprintf: vsprintf
  254. }
  255. })
  256. }
  257. }
  258. })(typeof window === 'undefined' ? this : window);