proposal-31-finalize-decent.../scripts/utils.ts
2023-10-18 08:11:34 -07:00

35 lines
1.2 KiB
TypeScript

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 {
public constructor(
private declType: string,
private padding: string = solidityCodePadding,
private transformator: Function = (
() => (x: any) =>
x
)(),
private variableNameChanger: Function = (
() => (x: any) =>
x
)()
) {}
private displayVariableName(varObj: NodeVarObject) {
return Object.keys(varObj)[0];
}
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);
};
}
export { DeclCalculator };