dojox.string.sprintf.Formatter.format
dojo.require("dojox.string.sprintf");
defined in dojox/string/sprintf.js
Usage
function (...filler) (view source)
if(this._mapped && typeof filler != "object"){ throw new Error("format requires a mapping"); } var str = ""; var position = 0; for(var i = 0, token; i < this._tokens.length; i++){ token = this._tokens[i]; if(typeof token == "string"){ str += token; }else{ if(this._mapped){ if(typeof filler[token.mapping] == "undefined"){ throw new Error("missing key " + token.mapping); } token.arg = filler[token.mapping]; }else{ if(token.intmapping){ var position = parseInt(token.intmapping) - 1; } if(position >= arguments.length){ throw new Error("got " + arguments.length + " printf arguments, insufficient for '" + this._format + "'"); } token.arg = arguments[position++]; } if(!token.compiled){ token.compiled = true; token.sign = ""; token.zeroPad = false; token.rightJustify = false; token.alternative = false; var flags = {}; for(var fi = token.flags.length; fi--;){ var flag = token.flags.charAt(fi); flags[flag] = true; switch(flag){ case " ": token.sign = " "; break; case "+": token.sign = "+"; break; case "0": token.zeroPad = (flags["-"]) ? false : true; break; case "-": token.rightJustify = true; token.zeroPad = false; break; case "\#": token.alternative = true; break; default: throw Error("bad formatting flag '" + token.flags.charAt(fi) + "'"); } } token.minWidth = (token._minWidth) ? parseInt(token._minWidth) : 0; token.maxWidth = -1; token.toUpper = false; token.isUnsigned = false; token.isInt = false; token.isDouble = false; token.precision = 1; if(token.period == '.'){ if(token._precision){ token.precision = parseInt(token._precision); }else{ token.precision = 0; } } var mixins = this._specifiers[token.specifier]; if(typeof mixins == "undefined"){ throw new Error("unexpected specifier '" + token.specifier + "'"); } if(mixins.extend){ dojo.mixin(mixins, this._specifiers[mixins.extend]); delete mixins.extend; } dojo.mixin(token, mixins); } if(typeof token.setArg == "function"){ token.setArg(token); } if(typeof token.setMaxWidth == "function"){ token.setMaxWidth(token); } if(token._minWidth == "*"){ if(this._mapped){ throw new Error("* width not supported in mapped formats"); } token.minWidth = parseInt(arguments[position++]); if(isNaN(token.minWidth)){ throw new Error("the argument for * width at position " + position + " is not a number in " + this._format); } // negative width means rightJustify if (token.minWidth < 0) { token.rightJustify = true; token.minWidth = -token.minWidth; } } if(token._precision == "*" && token.period == "."){ if(this._mapped){ throw new Error("* precision not supported in mapped formats"); } token.precision = parseInt(arguments[position++]); if(isNaN(token.precision)){ throw Error("the argument for * precision at position " + position + " is not a number in " + this._format); } // negative precision means unspecified if (token.precision < 0) { token.precision = 1; token.period = ''; } } if(token.isInt){ // a specified precision means no zero padding if(token.period == '.'){ token.zeroPad = false; } this.formatInt(token); }else if(token.isDouble){ if(token.period != '.'){ token.precision = 6; } this.formatDouble(token); } this.fitField(token); str += "" + token.arg; } } return str;
parameter | type | description |
---|---|---|
filler | Repeating. |