@protobufjs/codegen
Advanced tools
Comparing version
58
index.js
@@ -32,26 +32,43 @@ "use strict"; | ||
function Codegen(formatStringOrScope) { | ||
// note that explicit array handling below makes this ~50% faster | ||
// finish the function | ||
if (typeof formatStringOrScope !== "string") { | ||
var scopeParams = [], | ||
scopeValues = []; | ||
if (formatStringOrScope) | ||
for (var i = 0, keys = Object.keys(formatStringOrScope); i < keys.length; ++i) { | ||
scopeParams.push(keys[i]); | ||
scopeValues.push(formatStringOrScope[keys[i]]); | ||
} | ||
var source = Codegen.toString(); | ||
var source = toString(); | ||
if (codegen.verbose) | ||
console.log("codegen: " + source); // eslint-disable-line no-console | ||
scopeParams.push("return " + source); | ||
return Function.apply(null, scopeParams).apply(null, scopeValues); | ||
source = "return " + source; | ||
if (formatStringOrScope) { | ||
var scopeKeys = Object.keys(formatStringOrScope), | ||
scopeParams = new Array(scopeKeys.length + 1), | ||
scopeValues = new Array(scopeKeys.length), | ||
scopeOffset = 0; | ||
while (scopeOffset < scopeKeys.length) { | ||
scopeParams[scopeOffset] = scopeKeys[scopeOffset]; | ||
scopeValues[scopeOffset] = formatStringOrScope[scopeKeys[scopeOffset++]]; | ||
} | ||
scopeParams[scopeOffset] = source; | ||
return Function.apply(null, scopeParams) | ||
.apply(null, scopeValues); | ||
} | ||
return Function(source)(); | ||
} | ||
var formatParams = Array.prototype.slice.call(arguments, 1), | ||
formatParamsIndex = 0; | ||
formatStringOrScope = formatStringOrScope.replace(/%([dfjs])/g, function($0, $1) { | ||
var value = formatParams[formatParamsIndex++]; | ||
return $1 === "d" ? Math.floor(value) | ||
: $1 === "f" ? Number(value) | ||
: $1 === "j" ? JSON.stringify(value) | ||
: value; | ||
// otherwise append to body | ||
var formatParams = new Array(arguments.length - 1), | ||
formatOffset = 0; | ||
while (formatOffset < formatParams.length) | ||
formatParams[formatOffset] = arguments[++formatOffset]; | ||
formatOffset = 0; | ||
formatStringOrScope = formatStringOrScope.replace(/%([%dfijs])/g, function replace($0, $1) { | ||
var value = formatParams[formatOffset++]; | ||
switch ($1) { | ||
case "d": case "f": return String(Number(value)); | ||
case "i": return String(Math.floor(value)); | ||
case "j": return JSON.stringify(value); | ||
case "s": return String(value); | ||
} | ||
return "%"; | ||
}); | ||
if (formatParamsIndex !== formatParams.length) | ||
if (formatOffset !== formatParams.length) | ||
throw Error("parameter count mismatch"); | ||
@@ -62,6 +79,7 @@ body.push(formatStringOrScope); | ||
Codegen.toString = function(functionNameOverride) { | ||
function toString(functionNameOverride) { | ||
return "function " + (functionNameOverride || functionName || "") + "(" + (functionParams && functionParams.join(",") || "") + "){\n " + body.join("\n ") + "\n}"; | ||
}; | ||
Codegen.toString = toString; | ||
return Codegen; | ||
@@ -68,0 +86,0 @@ } |
{ | ||
"name": "@protobufjs/codegen", | ||
"description": "Minimalistic code generation utility.", | ||
"version": "2.0.2", | ||
"description": "A minimalistic code generation utility.", | ||
"version": "2.0.3", | ||
"author": "Daniel Wirtz <dcode+protobufjs@dcode.io>", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -5,3 +5,3 @@ @protobufjs/codegen | ||
Minimalistic code generation utility. | ||
A minimalistic code generation utility. | ||
@@ -22,6 +22,8 @@ API | ||
* `%d`: Integer | ||
* `%f`: Floating point number | ||
* `%d`: Number (integer or floating point value) | ||
* `%f`: Floating point value | ||
* `%i`: Integer value | ||
* `%j`: JSON.stringify'ed value | ||
* `%s`: Raw string<br /> | ||
* `%s`: String value | ||
* `%%`: Percent sign<br /> | ||
@@ -28,0 +30,0 @@ * **Codegen([scope: `Object.<string,*>`]): `Function`**<br /> |
var codegen = require(".."); | ||
// new require("benchmark").Suite().add("add", function() { | ||
var add = codegen(["a", "b"], "add") | ||
@@ -10,1 +12,3 @@ ("// awesome comment") | ||
throw Error("failed"); | ||
// }).on("cycle", function(event) { process.stdout.write(String(event.target) + "\n"); }).run(); |
9092
12.15%124
16.98%50
4.17%