Skip to content

Commit 0f5453a

Browse files
committed
Add parameter parsing
1 parent 9bc3a6c commit 0f5453a

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

v-next/core/src/internal/parameters.ts

+100
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import type { ParameterValue } from "../types/common.js";
2+
3+
import { HardhatError } from "@nomicfoundation/hardhat-errors";
4+
15
import { ParameterType } from "../types/common.js";
26

37
/**
@@ -54,3 +58,99 @@ const parameterTypeValidators: Record<
5458
[ParameterType.FLOAT]: (value): value is number => typeof value === "number",
5559
[ParameterType.FILE]: (value): value is string => typeof value === "string",
5660
};
61+
62+
/**
63+
* Parses a parameter value from a string to the corresponding type.
64+
*/
65+
// TODO: this code is duplicated in v-next/hardhat/src/internal/cli/main.ts
66+
// we should move it to a shared place and add tests
67+
export function parseParameterValue(
68+
value: string,
69+
type: ParameterType,
70+
name: string,
71+
): ParameterValue {
72+
switch (type) {
73+
case ParameterType.STRING:
74+
case ParameterType.FILE:
75+
return value;
76+
case ParameterType.INT:
77+
return validateAndParseInt(name, value);
78+
case ParameterType.FLOAT:
79+
return validateAndParseFloat(name, value);
80+
case ParameterType.BIGINT:
81+
return validateAndParseBigInt(name, value);
82+
case ParameterType.BOOLEAN:
83+
return validateAndParseBoolean(name, value);
84+
}
85+
}
86+
87+
function validateAndParseInt(name: string, value: string): number {
88+
const decimalPattern = /^\d+(?:[eE]\d+)?$/;
89+
const hexPattern = /^0[xX][\dABCDEabcde]+$/;
90+
91+
if (!decimalPattern.test(value) && !hexPattern.test(value)) {
92+
throw new HardhatError(
93+
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
94+
{
95+
value,
96+
name,
97+
type: ParameterType.INT,
98+
},
99+
);
100+
}
101+
102+
return Number(value);
103+
}
104+
105+
function validateAndParseFloat(name: string, value: string): number {
106+
const decimalPattern = /^(?:\d+(?:\.\d*)?|\.\d+)(?:[eE]\d+)?$/;
107+
const hexPattern = /^0[xX][\dABCDEabcde]+$/;
108+
109+
if (!decimalPattern.test(value) && !hexPattern.test(value)) {
110+
throw new HardhatError(
111+
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
112+
{
113+
value,
114+
name,
115+
type: ParameterType.FLOAT,
116+
},
117+
);
118+
}
119+
120+
return Number(value);
121+
}
122+
123+
function validateAndParseBigInt(name: string, value: string): bigint {
124+
const decimalPattern = /^\d+(?:n)?$/;
125+
const hexPattern = /^0[xX][\dABCDEabcde]+$/;
126+
127+
if (!decimalPattern.test(value) && !hexPattern.test(value)) {
128+
throw new HardhatError(
129+
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
130+
{
131+
value,
132+
name,
133+
type: ParameterType.BIGINT,
134+
},
135+
);
136+
}
137+
138+
return BigInt(value.replace("n", ""));
139+
}
140+
141+
function validateAndParseBoolean(name: string, value: string): boolean {
142+
const normalizedValue = value.toLowerCase();
143+
144+
if (normalizedValue !== "true" && normalizedValue !== "false") {
145+
throw new HardhatError(
146+
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
147+
{
148+
value,
149+
name,
150+
type: ParameterType.BOOLEAN,
151+
},
152+
);
153+
}
154+
155+
return normalizedValue === "true";
156+
}

0 commit comments

Comments
 (0)