proposal-33-compensate-rela.../scripts/utils.ts

38 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-10-23 20:02:23 +03:00
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 };