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