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+)\]/,
17
var key = arguments[0], cache = sprintf.cache
18
if (!(cache[key] && cache.hasOwnProperty(key))) {
19
cache[key] = sprintf.parse(key)
21
return sprintf.format.call(null, cache[key], arguments)
24
sprintf.format = function(parse_tree, argv) {
25
var cursor = 1, tree_length = parse_tree.length, node_type = "", arg, output = [], i, k, match, pad, pad_character, pad_length, is_positive = true, sign = ""
26
for (i = 0; i < tree_length; i++) {
27
node_type = get_type(parse_tree[i])
28
if (node_type === "string") {
29
output[output.length] = parse_tree[i]
31
else if (node_type === "array") {
32
match = parse_tree[i] // convenience purposes only
33
if (match[2]) { // keyword argument
35
for (k = 0; k < match[2].length; k++) {
36
if (!arg.hasOwnProperty(match[2][k])) {
37
throw new Error(sprintf("[sprintf] property '%s' does not exist", match[2][k]))
39
arg = arg[match[2][k]]
42
else if (match[1]) { // positional argument (explicit)
45
else { // positional argument (implicit)
49
if (get_type(arg) == "function") {
53
if (re.not_string.test(match[8]) && re.not_json.test(match[8]) && (get_type(arg) != "number" && isNaN(arg))) {
54
throw new TypeError(sprintf("[sprintf] expecting number but found %s", get_type(arg)))
57
if (re.number.test(match[8])) {
58
is_positive = arg >= 0
66
arg = String.fromCharCode(arg)
70
arg = parseInt(arg, 10)
73
arg = JSON.stringify(arg, null, match[6] ? parseInt(match[6]) : 0)
76
arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential()
79
arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg)
82
arg = match[7] ? parseFloat(arg).toPrecision(match[7]) : parseFloat(arg)
88
arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg)
94
arg = arg.toString(16)
97
arg = arg.toString(16).toUpperCase()
100
if (re.json.test(match[8])) {
101
output[output.length] = arg
104
if (re.number.test(match[8]) && (!is_positive || match[3])) {
105
sign = is_positive ? "+" : "-"
106
arg = arg.toString().replace(re.sign, "")
111
pad_character = match[4] ? match[4] === "0" ? "0" : match[4].charAt(1) : " "
112
pad_length = match[6] - (sign + arg).length
113
pad = match[6] ? (pad_length > 0 ? str_repeat(pad_character, pad_length) : "") : ""
114
output[output.length] = match[5] ? sign + arg + pad : (pad_character === "0" ? sign + pad + arg : pad + sign + arg)
118
return output.join("")
123
sprintf.parse = function(fmt) {
124
var _fmt = fmt, match = [], parse_tree = [], arg_names = 0
126
if ((match = re.text.exec(_fmt)) !== null) {
127
parse_tree[parse_tree.length] = match[0]
129
else if ((match = re.modulo.exec(_fmt)) !== null) {
130
parse_tree[parse_tree.length] = "%"
132
else if ((match = re.placeholder.exec(_fmt)) !== null) {
135
var field_list = [], replacement_field = match[2], field_match = []
136
if ((field_match = re.key.exec(replacement_field)) !== null) {
137
field_list[field_list.length] = field_match[1]
138
while ((replacement_field = replacement_field.substring(field_match[0].length)) !== "") {
139
if ((field_match = re.key_access.exec(replacement_field)) !== null) {
140
field_list[field_list.length] = field_match[1]
142
else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
143
field_list[field_list.length] = field_match[1]
146
throw new SyntaxError("[sprintf] failed to parse named argument key")
151
throw new SyntaxError("[sprintf] failed to parse named argument key")
153
match[2] = field_list
158
if (arg_names === 3) {
159
throw new Error("[sprintf] mixing positional and named placeholders is not (yet) supported")
161
parse_tree[parse_tree.length] = match
164
throw new SyntaxError("[sprintf] unexpected placeholder")
166
_fmt = _fmt.substring(match[0].length)
171
var vsprintf = function(fmt, argv, _argv) {
172
_argv = (argv || []).slice(0)
173
_argv.splice(0, 0, fmt)
174
return sprintf.apply(null, _argv)
180
function get_type(variable) {
181
return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase()
184
function str_repeat(input, multiplier) {
185
return Array(multiplier + 1).join(input)
189
* export to either browser or node.js
191
if (typeof exports !== "undefined") {
192
exports.sprintf = sprintf
193
exports.vsprintf = vsprintf
196
window.sprintf = sprintf
197
window.vsprintf = vsprintf
199
if (typeof define === "function" && define.amd) {
208
})(typeof window === "undefined" ? this : window);