Skip to content

Commit e7b2c4d

Browse files
sjhewittn1k0
authored andcommitted
asNumber: return undefined when value is empty string (#369)
Also * add eslint rule for spacing
1 parent b09b0d8 commit e7b2c4d

File tree

7 files changed

+14
-6
lines changed

7 files changed

+14
-6
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"comma-dangle": [0],
1616
"no-unused-vars": [2, {"vars": "all", "args": "none"}],
1717
"no-console": [0],
18-
"object-curly-spacing": [1, "never"]
18+
"object-curly-spacing": [1, "never"],
19+
"keyword-spacing": ["error"]
1920
},
2021
"env": {
2122
"es6": true,

src/components/fields/ObjectField.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class ObjectField extends Component {
101101
try {
102102
const properties = Object.keys(schema.properties);
103103
orderedProperties = orderProperties(properties, uiSchema["ui:order"]);
104-
} catch(err) {
104+
} catch (err) {
105105
return (
106106
<div>
107107
<p className="config-error" style={{color: "red"}}>

src/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ export function mergeObjects(obj1, obj2, concatArrays = false) {
223223
}
224224

225225
export function asNumber(value) {
226+
if (value === "") {
227+
return undefined;
228+
}
226229
if (/\.$/.test(value)) {
227230
// "3." can't really be considered a number even if it parses in js. The
228231
// user is most likely entering a float.
@@ -500,7 +503,7 @@ export function dataURItoBlob(dataURI) {
500503
// Built the Uint8Array Blob parameter from the base64 string.
501504
const binary = atob(splitted[1]);
502505
const array = [];
503-
for(let i = 0; i < binary.length; i++) {
506+
for (let i = 0; i < binary.length; i++) {
504507
array.push(binary.charCodeAt(i));
505508
}
506509
// Create the blob object

test/ArrayField_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ describe("ArrayField", () => {
189189

190190
try {
191191
Simulate.submit(node);
192-
} catch(e) {
192+
} catch (e) {
193193
// Silencing error thrown as failure is expected here
194194
}
195195

test/FormContext_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe("FormContext", () => {
6060

6161
it("should be passed to TemplateField", () => {
6262
function CustomTemplateField({formContext}) {
63-
return <div id={formContext.foo} />;
63+
return <div id={formContext.foo}/>;
6464
}
6565

6666
const {node} = createFormComponent({

test/Form_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ describe("Form", () => {
634634
function submit(node) {
635635
try {
636636
Simulate.submit(node);
637-
} catch(err) {
637+
} catch (err) {
638638
// Validation is expected to fail and call console.error, which is
639639
// stubbed to actually throw in createSandbox().
640640
}

test/utils_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ describe("utils", () => {
255255
it("should allow numbers with a 0 in the first decimal place", () => {
256256
expect(asNumber("3.07")).eql(3.07);
257257
});
258+
259+
it("should return undefined if the input is empty", () => {
260+
expect(asNumber("")).eql(undefined);
261+
});
258262
});
259263

260264
describe("isMultiSelect()", () => {

0 commit comments

Comments
 (0)