-
-
Notifications
You must be signed in to change notification settings - Fork 0
Avoid NaN numeric field emissions #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -227,13 +227,33 @@ function renderChoiceCardLabel(choice: FieldsChoice) { | |
| ); | ||
| } | ||
|
|
||
| function readInputValue(event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) { | ||
| export function parseNumericInput(value: string, type: "number" | "integer") { | ||
| if (value.trim() === "") { | ||
| return undefined; | ||
| } | ||
|
|
||
| const numericValue = Number(value); | ||
| if (!Number.isFinite(numericValue)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (type === "integer" && !Number.isInteger(numericValue)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return numericValue; | ||
| } | ||
|
|
||
| function readInputValue( | ||
| event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>, | ||
| type: FieldsSubField["type"], | ||
| ) { | ||
| const target = event.currentTarget; | ||
| if (target instanceof HTMLInputElement && target.type === "checkbox") { | ||
| return target.checked; | ||
| } | ||
| if (target instanceof HTMLInputElement && target.type === "number") { | ||
| return target.value === "" ? undefined : Number(target.value); | ||
| return parseNumericInput(target.value, type === "integer" ? "integer" : "number"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because Severity: medium 🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage. |
||
| } | ||
| return target.value; | ||
| } | ||
|
|
@@ -253,7 +273,7 @@ function renderSubField( | |
| placeholder: field.placeholder, | ||
| value: typeof value === "string" || typeof value === "number" ? value : "", | ||
| onChange: (event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => | ||
| onChange(readInputValue(event)), | ||
| onChange(readInputValue(event, type)), | ||
| }; | ||
|
|
||
| const selectChoices = choices(field.options); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { test } from "node:test"; | ||
| import { parseNumericInput } from "../dist/admin.mjs"; | ||
|
|
||
| test("numeric input emits undefined for empty values", () => { | ||
| assert.equal(parseNumericInput("", "number"), undefined); | ||
| assert.equal(parseNumericInput(" ", "integer"), undefined); | ||
| }); | ||
|
|
||
| test("numeric input emits undefined for invalid values instead of NaN", () => { | ||
| assert.equal(parseNumericInput("not-a-number", "number"), undefined); | ||
| assert.equal(parseNumericInput("123abc", "integer"), undefined); | ||
| assert.equal(parseNumericInput("Infinity", "number"), undefined); | ||
| }); | ||
|
|
||
| test("number input accepts integer, decimal, and boundary finite values", () => { | ||
| assert.equal(parseNumericInput("42", "number"), 42); | ||
| assert.equal(parseNumericInput("3.14", "number"), 3.14); | ||
| assert.equal(parseNumericInput(String(Number.MAX_SAFE_INTEGER), "number"), Number.MAX_SAFE_INTEGER); | ||
| assert.equal(parseNumericInput(String(Number.MIN_SAFE_INTEGER), "number"), Number.MIN_SAFE_INTEGER); | ||
| }); | ||
|
|
||
| test("integer input accepts integers and rejects decimals", () => { | ||
| assert.equal(parseNumericInput("42", "integer"), 42); | ||
| assert.equal(parseNumericInput("-9007199254740991", "integer"), Number.MIN_SAFE_INTEGER); | ||
| assert.equal(parseNumericInput("3.14", "integer"), undefined); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
node --test tests/*.test.mjsargument relies on shell glob expansion, which can fail on Windows/npm environments where*isn’t expanded, causing tests not to run (or to error on a literal path).Severity: medium
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.