/** * Copyright (c) 2007-2016, Alexandru Marasteanu <hello [at) alexei (dot] ro> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of this software nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
*/
function sprintf() { var key = arguments[0], cache = sprintf.cache if (!(cache[key] && cache.hasOwnProperty(key))) {
cache[key] = sprintf.parse(key)
} return sprintf.format.call(null, cache[key], arguments)
}
sprintf.format = function(parse_tree, argv) { var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length, is_positive = true, sign = '' for (i = 0; i < tree_length; i++) {
node_type = typeof parse_tree[i] // The items of parse tree are either strings or results of a match() call. if (node_type === 'string') { // this is not a placeholder, this is just a string.
output[output.length] = parse_tree[i]
} else { // this is a placeholder, need to identify its type, options and replace // it with the appropriate argument.
match = parse_tree[i] // convenience purposes only if (match[2]) { // keyword argument
arg = argv[cursor] for (k = 0; k < match[2].length; k++) { if (!arg.hasOwnProperty(match[2][k])) { thrownew Error(sprintf('[sprintf] property "%s" does not exist', match[2][k]))
}
arg = arg[match[2][k]]
}
} elseif (match[1]) { // positional argument (explicit)
arg = argv[match[1]]
} else { // positional argument (implicit)
arg = argv[cursor++]
}
// The most commonly used placeholder in DevTools is the string (%S or %s). // We check it first to avoid unnecessary verifications.
let hasPadding = match[6];
let patternType = match[8]; if (!hasPadding && (patternType === "S" || patternType === "s")) { if (typeof arg === "function") {
arg = arg();
} if (typeof arg !== "string") {
arg = String(arg);
}
output[output.length] = match[7] ? arg.substring(0, match[7]) : arg; continue;
}
if (re.numeric_arg.test(match[8]) && (typeof arg != 'number' && isNaN(arg))) { thrownew TypeError(sprintf("[sprintf] expecting number but found %s", typeof arg))
}
if (re.number.test(match[8])) {
is_positive = arg >= 0
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.