Skip to content

Commit

Permalink
Make numbers nullable by allowing null values to be echoed from asNum…
Browse files Browse the repository at this point in the history
…ber() (#1269)

* Make numbers nullable by allowing null values to be echoed from asNumber()

* fix: revert package-lock changes
  • Loading branch information
glompix authored and epicfaace committed May 7, 2019
1 parent fba5bee commit 1ee343d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion playground/samples/nullable.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
title: "Last name",
},
age: {
type: "integer",
type: ["integer", "null"],
title: "Age",
},
bio: {
Expand Down Expand Up @@ -43,6 +43,7 @@ module.exports = {
"ui:widget": "updown",
"ui:title": "Age of person",
"ui:description": "(earthian year)",
"ui:emptyValue": null,
},
bio: {
"ui:widget": "textarea",
Expand Down
3 changes: 3 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ export function asNumber(value) {
if (value === "") {
return undefined;
}
if (value === null) {
return null;
}
if (/\.$/.test(value)) {
// "3." can't really be considered a number even if it parses in js. The
// user is most likely entering a float.
Expand Down
4 changes: 4 additions & 0 deletions test/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ describe("utils", () => {
it("should return undefined if the input is empty", () => {
expect(asNumber("")).eql(undefined);
});

it("should return null if the input is null", () => {
expect(asNumber(null)).eql(null);
});
});

describe("orderProperties()", () => {
Expand Down

0 comments on commit 1ee343d

Please sign in to comment.