diff --git a/scripts/utils.ts b/scripts/utils.ts index dd1477d..ff27200 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -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); };