edouard@3360: /* https://github.com/alexei/sprintf.js/blob/master/src/sprintf.js */ edouard@3360: /* global window, exports, define */ edouard@3360: edouard@3360: !function() { edouard@3360: 'use strict' edouard@3360: edouard@3360: var re = { edouard@3360: not_string: /[^s]/, edouard@3360: not_bool: /[^t]/, edouard@3360: not_type: /[^T]/, edouard@3360: not_primitive: /[^v]/, edouard@3360: number: /[diefg]/, edouard@3360: numeric_arg: /[bcdiefguxX]/, edouard@3360: json: /[j]/, edouard@3360: not_json: /[^j]/, edouard@3360: text: /^[^\x25]+/, edouard@3360: modulo: /^\x25{2}/, edouard@3360: placeholder: /^\x25(?:([1-9]\d*)\$|\(([^)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijostTuvxX])/, edouard@3360: key: /^([a-z_][a-z_\d]*)/i, edouard@3360: key_access: /^\.([a-z_][a-z_\d]*)/i, edouard@3360: index_access: /^\[(\d+)\]/, edouard@3360: sign: /^[+-]/ edouard@3360: } edouard@3360: edouard@3360: function sprintf(key) { edouard@3360: // arguments is not an array, but should be fine for this call edouard@3360: return sprintf_format(sprintf_parse(key), arguments) edouard@3360: } edouard@3360: edouard@3360: function vsprintf(fmt, argv) { edouard@3360: return sprintf.apply(null, [fmt].concat(argv || [])) edouard@3360: } edouard@3360: edouard@3360: function sprintf_format(parse_tree, argv) { edouard@3360: var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign edouard@3360: for (i = 0; i < tree_length; i++) { edouard@3360: if (typeof parse_tree[i] === 'string') { edouard@3360: output += parse_tree[i] edouard@3360: } edouard@3360: else if (typeof parse_tree[i] === 'object') { edouard@3360: ph = parse_tree[i] // convenience purposes only edouard@3360: if (ph.keys) { // keyword argument edouard@3360: arg = argv[cursor] edouard@3360: for (k = 0; k < ph.keys.length; k++) { edouard@3360: if (arg == undefined) { edouard@3360: throw new Error(sprintf('[sprintf] Cannot access property "%s" of undefined value "%s"', ph.keys[k], ph.keys[k-1])) edouard@3360: } edouard@3360: arg = arg[ph.keys[k]] edouard@3360: } edouard@3360: } edouard@3360: else if (ph.param_no) { // positional argument (explicit) edouard@3360: arg = argv[ph.param_no] edouard@3360: } edouard@3360: else { // positional argument (implicit) edouard@3360: arg = argv[cursor++] edouard@3360: } edouard@3360: edouard@3360: if (re.not_type.test(ph.type) && re.not_primitive.test(ph.type) && arg instanceof Function) { edouard@3360: arg = arg() edouard@3360: } edouard@3360: edouard@3360: if (re.numeric_arg.test(ph.type) && (typeof arg !== 'number' && isNaN(arg))) { edouard@3360: throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg)) edouard@3360: } edouard@3360: edouard@3360: if (re.number.test(ph.type)) { edouard@3360: is_positive = arg >= 0 edouard@3360: } edouard@3360: edouard@3360: switch (ph.type) { edouard@3360: case 'b': edouard@3360: arg = parseInt(arg, 10).toString(2) edouard@3360: break edouard@3360: case 'c': edouard@3360: arg = String.fromCharCode(parseInt(arg, 10)) edouard@3360: break edouard@3360: case 'd': edouard@3360: case 'i': edouard@3360: arg = parseInt(arg, 10) edouard@3360: break edouard@3360: case 'j': edouard@3360: arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0) edouard@3360: break edouard@3360: case 'e': edouard@3360: arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential() edouard@3360: break edouard@3360: case 'f': edouard@3360: arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg) edouard@3360: break edouard@3360: case 'g': edouard@3360: arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg) edouard@3360: break edouard@3360: case 'o': edouard@3360: arg = (parseInt(arg, 10) >>> 0).toString(8) edouard@3360: break edouard@3360: case 's': edouard@3360: arg = String(arg) edouard@3360: arg = (ph.precision ? arg.substring(0, ph.precision) : arg) edouard@3360: break edouard@3360: case 't': edouard@3360: arg = String(!!arg) edouard@3360: arg = (ph.precision ? arg.substring(0, ph.precision) : arg) edouard@3360: break edouard@3360: case 'T': edouard@3360: arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase() edouard@3360: arg = (ph.precision ? arg.substring(0, ph.precision) : arg) edouard@3360: break edouard@3360: case 'u': edouard@3360: arg = parseInt(arg, 10) >>> 0 edouard@3360: break edouard@3360: case 'v': edouard@3360: arg = arg.valueOf() edouard@3360: arg = (ph.precision ? arg.substring(0, ph.precision) : arg) edouard@3360: break edouard@3360: case 'x': edouard@3360: arg = (parseInt(arg, 10) >>> 0).toString(16) edouard@3360: break edouard@3360: case 'X': edouard@3360: arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase() edouard@3360: break edouard@3360: } edouard@3360: if (re.json.test(ph.type)) { edouard@3360: output += arg edouard@3360: } edouard@3360: else { edouard@3360: if (re.number.test(ph.type) && (!is_positive || ph.sign)) { edouard@3360: sign = is_positive ? '+' : '-' edouard@3360: arg = arg.toString().replace(re.sign, '') edouard@3360: } edouard@3360: else { edouard@3360: sign = '' edouard@3360: } edouard@3360: pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' ' edouard@3360: pad_length = ph.width - (sign + arg).length edouard@3360: pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : '' edouard@3360: output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg) edouard@3360: } edouard@3360: } edouard@3360: } edouard@3360: return output edouard@3360: } edouard@3360: edouard@3360: var sprintf_cache = Object.create(null) edouard@3360: edouard@3360: function sprintf_parse(fmt) { edouard@3360: if (sprintf_cache[fmt]) { edouard@3360: return sprintf_cache[fmt] edouard@3360: } edouard@3360: edouard@3360: var _fmt = fmt, match, parse_tree = [], arg_names = 0 edouard@3360: while (_fmt) { edouard@3360: if ((match = re.text.exec(_fmt)) !== null) { edouard@3360: parse_tree.push(match[0]) edouard@3360: } edouard@3360: else if ((match = re.modulo.exec(_fmt)) !== null) { edouard@3360: parse_tree.push('%') edouard@3360: } edouard@3360: else if ((match = re.placeholder.exec(_fmt)) !== null) { edouard@3360: if (match[2]) { edouard@3360: arg_names |= 1 edouard@3360: var field_list = [], replacement_field = match[2], field_match = [] edouard@3360: if ((field_match = re.key.exec(replacement_field)) !== null) { edouard@3360: field_list.push(field_match[1]) edouard@3360: while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') { edouard@3360: if ((field_match = re.key_access.exec(replacement_field)) !== null) { edouard@3360: field_list.push(field_match[1]) edouard@3360: } edouard@3360: else if ((field_match = re.index_access.exec(replacement_field)) !== null) { edouard@3360: field_list.push(field_match[1]) edouard@3360: } edouard@3360: else { edouard@3360: throw new SyntaxError('[sprintf] failed to parse named argument key') edouard@3360: } edouard@3360: } edouard@3360: } edouard@3360: else { edouard@3360: throw new SyntaxError('[sprintf] failed to parse named argument key') edouard@3360: } edouard@3360: match[2] = field_list edouard@3360: } edouard@3360: else { edouard@3360: arg_names |= 2 edouard@3360: } edouard@3360: if (arg_names === 3) { edouard@3360: throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported') edouard@3360: } edouard@3360: edouard@3360: parse_tree.push( edouard@3360: { edouard@3360: placeholder: match[0], edouard@3360: param_no: match[1], edouard@3360: keys: match[2], edouard@3360: sign: match[3], edouard@3360: pad_char: match[4], edouard@3360: align: match[5], edouard@3360: width: match[6], edouard@3360: precision: match[7], edouard@3360: type: match[8] edouard@3360: } edouard@3360: ) edouard@3360: } edouard@3360: else { edouard@3360: throw new SyntaxError('[sprintf] unexpected placeholder') edouard@3360: } edouard@3360: _fmt = _fmt.substring(match[0].length) edouard@3360: } edouard@3360: return sprintf_cache[fmt] = parse_tree edouard@3360: } edouard@3360: edouard@3360: /** edouard@3360: * export to either browser or node.js edouard@3360: */ edouard@3360: /* eslint-disable quote-props */ edouard@3360: if (typeof exports !== 'undefined') { edouard@3360: exports['sprintf'] = sprintf edouard@3360: exports['vsprintf'] = vsprintf edouard@3360: } edouard@3360: if (typeof window !== 'undefined') { edouard@3360: window['sprintf'] = sprintf edouard@3360: window['vsprintf'] = vsprintf edouard@3360: edouard@3360: if (typeof define === 'function' && define['amd']) { edouard@3360: define(function() { edouard@3360: return { edouard@3360: 'sprintf': sprintf, edouard@3360: 'vsprintf': vsprintf edouard@3360: } edouard@3360: }) edouard@3360: } edouard@3360: } edouard@3360: /* eslint-enable quote-props */ edouard@3360: }(); // eslint-disable-line