Path: blob/master/node_modules/@protobufjs/codegen/index.js
1126 views
"use strict";1module.exports = codegen;23/**4* Begins generating a function.5* @memberof util6* @param {string[]} functionParams Function parameter names7* @param {string} [functionName] Function name if not anonymous8* @returns {Codegen} Appender that appends code to the function's body9*/10function codegen(functionParams, functionName) {1112/* istanbul ignore if */13if (typeof functionParams === "string") {14functionName = functionParams;15functionParams = undefined;16}1718var body = [];1920/**21* Appends code to the function's body or finishes generation.22* @typedef Codegen23* @type {function}24* @param {string|Object.<string,*>} [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any25* @param {...*} [formatParams] Format parameters26* @returns {Codegen|Function} Itself or the generated function if finished27* @throws {Error} If format parameter counts do not match28*/2930function Codegen(formatStringOrScope) {31// note that explicit array handling below makes this ~50% faster3233// finish the function34if (typeof formatStringOrScope !== "string") {35var source = toString();36if (codegen.verbose)37console.log("codegen: " + source); // eslint-disable-line no-console38source = "return " + source;39if (formatStringOrScope) {40var scopeKeys = Object.keys(formatStringOrScope),41scopeParams = new Array(scopeKeys.length + 1),42scopeValues = new Array(scopeKeys.length),43scopeOffset = 0;44while (scopeOffset < scopeKeys.length) {45scopeParams[scopeOffset] = scopeKeys[scopeOffset];46scopeValues[scopeOffset] = formatStringOrScope[scopeKeys[scopeOffset++]];47}48scopeParams[scopeOffset] = source;49return Function.apply(null, scopeParams).apply(null, scopeValues); // eslint-disable-line no-new-func50}51return Function(source)(); // eslint-disable-line no-new-func52}5354// otherwise append to body55var formatParams = new Array(arguments.length - 1),56formatOffset = 0;57while (formatOffset < formatParams.length)58formatParams[formatOffset] = arguments[++formatOffset];59formatOffset = 0;60formatStringOrScope = formatStringOrScope.replace(/%([%dfijs])/g, function replace($0, $1) {61var value = formatParams[formatOffset++];62switch ($1) {63case "d": case "f": return String(Number(value));64case "i": return String(Math.floor(value));65case "j": return JSON.stringify(value);66case "s": return String(value);67}68return "%";69});70if (formatOffset !== formatParams.length)71throw Error("parameter count mismatch");72body.push(formatStringOrScope);73return Codegen;74}7576function toString(functionNameOverride) {77return "function " + (functionNameOverride || functionName || "") + "(" + (functionParams && functionParams.join(",") || "") + "){\n " + body.join("\n ") + "\n}";78}7980Codegen.toString = toString;81return Codegen;82}8384/**85* Begins generating a function.86* @memberof util87* @function codegen88* @param {string} [functionName] Function name if not anonymous89* @returns {Codegen} Appender that appends code to the function's body90* @variation 291*/9293/**94* When set to `true`, codegen will log generated code to console. Useful for debugging.95* @name util.codegen.verbose96* @type {boolean}97*/98codegen.verbose = false;99100101