proposal-29-update-ipfs/scripts/utils.ts

38 lines
1.1 KiB
TypeScript
Raw Normal View History

type NodeVarObject = { [key: string]: string };
2023-07-28 15:43:53 +03:00
const solidityCodePadding = " ".repeat(8);
const pad = (decl: string, padding: string = solidityCodePadding) => padding + decl + "\n";
class DeclCalculator {
declType!: string;
padding!: string;
transformator!: Function;
public constructor(
declType: string,
padding: string = solidityCodePadding,
transformator: Function = (
() => (x: any) =>
x
)()
) {
this.declType = declType;
this.padding = padding;
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};`;
return pad(solidityDeclaration, this.padding);
};
}
export { DeclCalculator };