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 und die Messung sind noch experimentell.