You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sprintf.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. (function(window) {
  2. var re = {
  3. not_string: /[^s]/,
  4. number: /[diefg]/,
  5. json: /[j]/,
  6. not_json: /[^j]/,
  7. text: /^[^\x25]+/,
  8. modulo: /^\x25{2}/,
  9. placeholder: /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijosuxX])/,
  10. key: /^([a-z_][a-z_\d]*)/i,
  11. key_access: /^\.([a-z_][a-z_\d]*)/i,
  12. index_access: /^\[(\d+)\]/,
  13. sign: /^[\+\-]/
  14. }
  15. function sprintf() {
  16. var key = arguments[0], cache = sprintf.cache
  17. if (!(cache[key] && cache.hasOwnProperty(key))) {
  18. cache[key] = sprintf.parse(key)
  19. }
  20. return sprintf.format.call(null, cache[key], arguments)
  21. }
  22. sprintf.format = function(parse_tree, argv) {
  23. var cursor = 1, tree_length = parse_tree.length, node_type = "", arg, output = [], i, k, match, pad, pad_character, pad_length, is_positive = true, sign = ""
  24. for (i = 0; i < tree_length; i++) {
  25. node_type = get_type(parse_tree[i])
  26. if (node_type === "string") {
  27. output[output.length] = parse_tree[i]
  28. }
  29. else if (node_type === "array") {
  30. match = parse_tree[i] // convenience purposes only
  31. if (match[2]) { // keyword argument
  32. arg = argv[cursor]
  33. for (k = 0; k < match[2].length; k++) {
  34. if (!arg.hasOwnProperty(match[2][k])) {
  35. throw new Error(sprintf("[sprintf] property '%s' does not exist", match[2][k]))
  36. }
  37. arg = arg[match[2][k]]
  38. }
  39. }
  40. else if (match[1]) { // positional argument (explicit)
  41. arg = argv[match[1]]
  42. }
  43. else { // positional argument (implicit)
  44. arg = argv[cursor++]
  45. }
  46. if (get_type(arg) == "function") {
  47. arg = arg()
  48. }
  49. if (re.not_string.test(match[8]) && re.not_json.test(match[8]) && (get_type(arg) != "number" && isNaN(arg))) {
  50. throw new TypeError(sprintf("[sprintf] expecting number but found %s", get_type(arg)))
  51. }
  52. if (re.number.test(match[8])) {
  53. is_positive = arg >= 0
  54. }
  55. switch (match[8]) {
  56. case "b":
  57. arg = arg.toString(2)
  58. break
  59. case "c":
  60. arg = String.fromCharCode(arg)
  61. break
  62. case "d":
  63. case "i":
  64. arg = parseInt(arg, 10)
  65. break
  66. case "j":
  67. arg = JSON.stringify(arg, null, match[6] ? parseInt(match[6]) : 0)
  68. break
  69. case "e":
  70. arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential()
  71. break
  72. case "f":
  73. arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg)
  74. break
  75. case "g":
  76. arg = match[7] ? parseFloat(arg).toPrecision(match[7]) : parseFloat(arg)
  77. break
  78. case "o":
  79. arg = arg.toString(8)
  80. break
  81. case "s":
  82. arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg)
  83. break
  84. case "u":
  85. arg = arg >>> 0
  86. break
  87. case "x":
  88. arg = arg.toString(16)
  89. break
  90. case "X":
  91. arg = arg.toString(16).toUpperCase()
  92. break
  93. }
  94. if (re.json.test(match[8])) {
  95. output[output.length] = arg
  96. }
  97. else {
  98. if (re.number.test(match[8]) && (!is_positive || match[3])) {
  99. sign = is_positive ? "+" : "-"
  100. arg = arg.toString().replace(re.sign, "")
  101. }
  102. else {
  103. sign = ""
  104. }
  105. pad_character = match[4] ? match[4] === "0" ? "0" : match[4].charAt(1) : " "
  106. pad_length = match[6] - (sign + arg).length
  107. pad = match[6] ? (pad_length > 0 ? str_repeat(pad_character, pad_length) : "") : ""
  108. output[output.length] = match[5] ? sign + arg + pad : (pad_character === "0" ? sign + pad + arg : pad + sign + arg)
  109. }
  110. }
  111. }
  112. return output.join("")
  113. }
  114. sprintf.cache = {}
  115. sprintf.parse = function(fmt) {
  116. var _fmt = fmt, match = [], parse_tree = [], arg_names = 0
  117. while (_fmt) {
  118. if ((match = re.text.exec(_fmt)) !== null) {
  119. parse_tree[parse_tree.length] = match[0]
  120. }
  121. else if ((match = re.modulo.exec(_fmt)) !== null) {
  122. parse_tree[parse_tree.length] = "%"
  123. }
  124. else if ((match = re.placeholder.exec(_fmt)) !== null) {
  125. if (match[2]) {
  126. arg_names |= 1
  127. var field_list = [], replacement_field = match[2], field_match = []
  128. if ((field_match = re.key.exec(replacement_field)) !== null) {
  129. field_list[field_list.length] = field_match[1]
  130. while ((replacement_field = replacement_field.substring(field_match[0].length)) !== "") {
  131. if ((field_match = re.key_access.exec(replacement_field)) !== null) {
  132. field_list[field_list.length] = field_match[1]
  133. }
  134. else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
  135. field_list[field_list.length] = field_match[1]
  136. }
  137. else {
  138. throw new SyntaxError("[sprintf] failed to parse named argument key")
  139. }
  140. }
  141. }
  142. else {
  143. throw new SyntaxError("[sprintf] failed to parse named argument key")
  144. }
  145. match[2] = field_list
  146. }
  147. else {
  148. arg_names |= 2
  149. }
  150. if (arg_names === 3) {
  151. throw new Error("[sprintf] mixing positional and named placeholders is not (yet) supported")
  152. }
  153. parse_tree[parse_tree.length] = match
  154. }
  155. else {
  156. throw new SyntaxError("[sprintf] unexpected placeholder")
  157. }
  158. _fmt = _fmt.substring(match[0].length)
  159. }
  160. return parse_tree
  161. }
  162. var vsprintf = function(fmt, argv, _argv) {
  163. _argv = (argv || []).slice(0)
  164. _argv.splice(0, 0, fmt)
  165. return sprintf.apply(null, _argv)
  166. }
  167. /**
  168. * helpers
  169. */
  170. function get_type(variable) {
  171. return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase()
  172. }
  173. function str_repeat(input, multiplier) {
  174. return Array(multiplier + 1).join(input)
  175. }
  176. /**
  177. * export to either browser or node.js
  178. */
  179. if (typeof exports !== "undefined") {
  180. exports.sprintf = sprintf
  181. exports.vsprintf = vsprintf
  182. }
  183. else {
  184. window.sprintf = sprintf
  185. window.vsprintf = vsprintf
  186. if (typeof define === "function" && define.amd) {
  187. define(function() {
  188. return {
  189. sprintf: sprintf,
  190. vsprintf: vsprintf
  191. }
  192. })
  193. }
  194. }
  195. })(typeof window === "undefined" ? this : window);