Skip to content

Commit

Permalink
make empty form value translate to empty string (not 0) (#1)
Browse files Browse the repository at this point in the history
* make empty form value translate to empty string

Before this patch empty form values were converted to 0.
Values are coerced to a number with `+value`, and `+"" === 0`.
This commit adds an explicit check for `value === ""`.

* correct line ending in editorconfig

Files were formatted with LF EOL, but editorconfig had CRLF
  • Loading branch information
ishehadeh authored Apr 17, 2023
1 parent 42063db commit 5195d1e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ root = true
[*]
indent_style = space
indent_size = 4
end_of_line = crlf
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
2 changes: 2 additions & 0 deletions src/utilities/convertValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export function convertValue(value: any): number | boolean | string {

if (value === "false") return false;

if (value === "") return "";

if (!isNaN(+value)) return +value;

return value;
Expand Down
11 changes: 11 additions & 0 deletions tests/utilities/convertValue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ Deno.test("convertValue - Number string converts to number", () => {
assert(!isNaN(+actualValue));
});

Deno.test("convertValue - Empty string converts to empty string", () => {
// Assign
const value = "";

// Act
const actualValue = convertValue(value);

// Assert
assert(actualValue === "");
});

Deno.test("convertValue - Fractal string does not convert to number", () => {
// Assign
const value = "1/5";
Expand Down

0 comments on commit 5195d1e

Please sign in to comment.