proposal-30-decentralize-so.../scripts/utils.ts

35 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

type NodeVarObject = { [key: string]: string };
type NodeVarArray = [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 {
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 };