1
0

Improve util script to generate code declarations

This commit is contained in:
Theo 2023-10-23 10:29:36 -07:00
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 solidityCodePadding = " ".repeat(8);
const pad = (decl: string, padding: string = solidityCodePadding) => padding + decl + "\n"; const pad = (decl: string, padding: string = solidityCodePadding) => padding + decl + "\n";
class DeclCalculator { class DeclCalculator {
declType!: string | number;
padding!: string;
transformator!: Function;
public constructor( public constructor(
declType: string | number, private declType: string,
padding: string = solidityCodePadding, private padding: string = solidityCodePadding,
transformator: Function = ( private transformator: Function = (
() => (x: any) =>
x
)(),
private variableNameChanger: Function = (
() => (x: any) => () => (x: any) =>
x x
)() )()
) { ) {}
this.declType = declType;
this.padding = padding || solidityCodePadding;
this.transformator = transformator;
}
private displayVariableName(varObj: NodeVarObject) { private displayVariableName(varObj: NodeVarObject) {
return Object.keys(varObj)[0]; return Object.keys(varObj)[0];
} }
public calculateDecl = (varObj: NodeVarObject, type: string = "bytes32") => { public calculateDecl = (varInfo: NodeVarObject | NodeVarArray, type: string = "bytes32") => {
const solidityVariableName = this.displayVariableName(varObj); const solidityVariableName = this.variableNameChanger(Array.isArray(varInfo) ? varInfo[0] : this.displayVariableName(varInfo));
const solidityVariableValue = this.transformator(Object.values(varObj)[0]); const solidityVariableValue = this.transformator(Array.isArray(varInfo) ? varInfo[1] : Object.values(varInfo)[0]);
const solidityDeclaration = `${this.declType || type} ${solidityVariableName} = ${solidityVariableValue.toString()};`; const solidityDeclaration = `${this.declType || type} ${solidityVariableName} = ${solidityVariableValue};`;
return pad(solidityDeclaration, this.padding); return pad(solidityDeclaration, this.padding);
}; };