38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
|
type NodeVarObject = { [key: string]: string | number };
|
||
|
|
||
|
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 = (
|
||
|
() => (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()};`;
|
||
|
|
||
|
return pad(solidityDeclaration, this.padding);
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export { DeclCalculator };
|