Skip to content

Commit

Permalink
Check number before parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
zabil committed Oct 10, 2024
1 parent a34fb72 commit c12eb22
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions e2e/specs/parameters.spec
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
## Custom Parameters in steps

* This step uses a custom parameter of type Person and value "{\"name\":\"John\",\"age\":30}"
* This step checks if strings with numbers for example "3 % 4" is correct
6 changes: 6 additions & 0 deletions e2e/tests/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ export default class Parameter {
assert.strictEqual(person.age, 30);
assert.ok(person.isAdult());
}
@Step(
"This step checks if strings with numbers for example <value> is correct",
)
async setFilter(value: string) {
assert.strictEqual(value, "3 % 4");
}
}
8 changes: 6 additions & 2 deletions gauge-ts/src/processors/params/PrimitiveParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ export class PrimitiveParser implements ParameterParser {
}

private convertToNumber(value: string): number | undefined {
const num = Number.parseFloat(value);
return Number.isNaN(num) ? undefined : num;
const trimmedValue = value.trim();
if (/^-?\d+(\.\d+)?$/.test(trimmedValue)) {
const num = Number.parseFloat(trimmedValue);
return Number.isFinite(num) ? num : undefined;
}
return undefined;
}

private convertToBoolean(value: string): boolean | undefined {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c12eb22

Please sign in to comment.