Improve util script to generate code declarations
This commit is contained in:
parent
7a13d64e1b
commit
c019450222
@ -1,34 +1,31 @@
|
||||
type NodeVarObject = { [key: string]: string | number };
|
||||
type NodeVarObject = { [key: string]: string };
|
||||
type NodeVarArray = [string, string];
|
||||
|
||||
const solidityCodePadding = " ".repeat(8);
|
||||
const pad = (decl: string, padding: string = solidityCodePadding) => padding + decl + "\n";
|
||||
|
||||
class DeclCalculator {
|
||||
declType!: string | number;
|
||||
padding!: string;
|
||||
transformator!: Function;
|
||||
|
||||
public constructor(
|
||||
declType: string | number,
|
||||
padding: string = solidityCodePadding,
|
||||
transformator: Function = (
|
||||
private declType: string,
|
||||
private padding: string = solidityCodePadding,
|
||||
private transformator: Function = (
|
||||
() => (x: any) =>
|
||||
x
|
||||
)(),
|
||||
private variableNameChanger: Function = (
|
||||
() => (x: any) =>
|
||||
x
|
||||
)()
|
||||
) {
|
||||
this.declType = declType;
|
||||
this.padding = padding || solidityCodePadding;
|
||||
this.transformator = transformator;
|
||||
}
|
||||
) {}
|
||||
|
||||
private displayVariableName(varObj: NodeVarObject) {
|
||||
return Object.keys(varObj)[0];
|
||||
}
|
||||
|
||||
public calculateDecl = (varObj: NodeVarObject, type: string = "bytes32") => {
|
||||
const solidityVariableName = this.displayVariableName(varObj);
|
||||
const solidityVariableValue = this.transformator(Object.values(varObj)[0]);
|
||||
const solidityDeclaration = `${this.declType || type} ${solidityVariableName} = ${solidityVariableValue.toString()};`;
|
||||
public calculateDecl = (varInfo: NodeVarObject | NodeVarArray, type: string = "bytes32") => {
|
||||
const solidityVariableName = this.variableNameChanger(Array.isArray(varInfo) ? varInfo[0] : this.displayVariableName(varInfo));
|
||||
const solidityVariableValue = this.transformator(Array.isArray(varInfo) ? varInfo[1] : Object.values(varInfo)[0]);
|
||||
const solidityDeclaration = `${this.declType || type} ${solidityVariableName} = ${solidityVariableValue};`;
|
||||
|
||||
return pad(solidityDeclaration, this.padding);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user