react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / node_modules / sprintf-js / src / sprintf.js
80742 views(function(window) {1var re = {2not_string: /[^s]/,3number: /[dief]/,4text: /^[^\x25]+/,5modulo: /^\x25{2}/,6placeholder: /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fiosuxX])/,7key: /^([a-z_][a-z_\d]*)/i,8key_access: /^\.([a-z_][a-z_\d]*)/i,9index_access: /^\[(\d+)\]/,10sign: /^[\+\-]/11}1213function sprintf() {14var key = arguments[0], cache = sprintf.cache15if (!(cache[key] && cache.hasOwnProperty(key))) {16cache[key] = sprintf.parse(key)17}18return sprintf.format.call(null, cache[key], arguments)19}2021sprintf.format = function(parse_tree, argv) {22var cursor = 1, tree_length = parse_tree.length, node_type = "", arg, output = [], i, k, match, pad, pad_character, pad_length, is_positive = true, sign = ""23for (i = 0; i < tree_length; i++) {24node_type = get_type(parse_tree[i])25if (node_type === "string") {26output[output.length] = parse_tree[i]27}28else if (node_type === "array") {29match = parse_tree[i] // convenience purposes only30if (match[2]) { // keyword argument31arg = argv[cursor]32for (k = 0; k < match[2].length; k++) {33if (!arg.hasOwnProperty(match[2][k])) {34throw new Error(sprintf("[sprintf] property '%s' does not exist", match[2][k]))35}36arg = arg[match[2][k]]37}38}39else if (match[1]) { // positional argument (explicit)40arg = argv[match[1]]41}42else { // positional argument (implicit)43arg = argv[cursor++]44}4546if (get_type(arg) == "function") {47arg = arg()48}4950if (re.not_string.test(match[8]) && (get_type(arg) != "number" && isNaN(arg))) {51throw new TypeError(sprintf("[sprintf] expecting number but found %s", get_type(arg)))52}5354if (re.number.test(match[8])) {55is_positive = arg >= 056}5758switch (match[8]) {59case "b":60arg = arg.toString(2)61break62case "c":63arg = String.fromCharCode(arg)64break65case "d":66case "i":67arg = parseInt(arg, 10)68break69case "e":70arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential()71break72case "f":73arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg)74break75case "o":76arg = arg.toString(8)77break78case "s":79arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg)80break81case "u":82arg = arg >>> 083break84case "x":85arg = arg.toString(16)86break87case "X":88arg = arg.toString(16).toUpperCase()89break90}91if (re.number.test(match[8]) && (!is_positive || match[3])) {92sign = is_positive ? "+" : "-"93arg = arg.toString().replace(re.sign, "")94}95else {96sign = ""97}98pad_character = match[4] ? match[4] === "0" ? "0" : match[4].charAt(1) : " "99pad_length = match[6] - (sign + arg).length100pad = match[6] ? (pad_length > 0 ? str_repeat(pad_character, pad_length) : "") : ""101output[output.length] = match[5] ? sign + arg + pad : (pad_character === "0" ? sign + pad + arg : pad + sign + arg)102}103}104return output.join("")105}106107sprintf.cache = {}108109sprintf.parse = function(fmt) {110var _fmt = fmt, match = [], parse_tree = [], arg_names = 0111while (_fmt) {112if ((match = re.text.exec(_fmt)) !== null) {113parse_tree[parse_tree.length] = match[0]114}115else if ((match = re.modulo.exec(_fmt)) !== null) {116parse_tree[parse_tree.length] = "%"117}118else if ((match = re.placeholder.exec(_fmt)) !== null) {119if (match[2]) {120arg_names |= 1121var field_list = [], replacement_field = match[2], field_match = []122if ((field_match = re.key.exec(replacement_field)) !== null) {123field_list[field_list.length] = field_match[1]124while ((replacement_field = replacement_field.substring(field_match[0].length)) !== "") {125if ((field_match = re.key_access.exec(replacement_field)) !== null) {126field_list[field_list.length] = field_match[1]127}128else if ((field_match = re.index_access.exec(replacement_field)) !== null) {129field_list[field_list.length] = field_match[1]130}131else {132throw new SyntaxError("[sprintf] failed to parse named argument key")133}134}135}136else {137throw new SyntaxError("[sprintf] failed to parse named argument key")138}139match[2] = field_list140}141else {142arg_names |= 2143}144if (arg_names === 3) {145throw new Error("[sprintf] mixing positional and named placeholders is not (yet) supported")146}147parse_tree[parse_tree.length] = match148}149else {150throw new SyntaxError("[sprintf] unexpected placeholder")151}152_fmt = _fmt.substring(match[0].length)153}154return parse_tree155}156157var vsprintf = function(fmt, argv, _argv) {158_argv = (argv || []).slice(0)159_argv.splice(0, 0, fmt)160return sprintf.apply(null, _argv)161}162163/**164* helpers165*/166function get_type(variable) {167return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase()168}169170function str_repeat(input, multiplier) {171return Array(multiplier + 1).join(input)172}173174/**175* export to either browser or node.js176*/177if (typeof exports !== "undefined") {178exports.sprintf = sprintf179exports.vsprintf = vsprintf180}181else {182window.sprintf = sprintf183window.vsprintf = vsprintf184185if (typeof define === "function" && define.amd) {186define(function() {187return {188sprintf: sprintf,189vsprintf: vsprintf190}191})192}193}194})(typeof window === "undefined" ? this : window);195196197