Skip to content

Commit

Permalink
asNumber: return undefined when value is empty string (#369)
Browse files Browse the repository at this point in the history
Also

* add eslint rule for spacing
  • Loading branch information
sjhewitt authored and n1k0 committed Nov 2, 2016
1 parent b09b0d8 commit e7b2c4d
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"comma-dangle": [0],
"no-unused-vars": [2, {"vars": "all", "args": "none"}],
"no-console": [0],
"object-curly-spacing": [1, "never"]
"object-curly-spacing": [1, "never"],
"keyword-spacing": ["error"]
},
"env": {
"es6": true,
Expand Down
2 changes: 1 addition & 1 deletion src/components/fields/ObjectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class ObjectField extends Component {
try {
const properties = Object.keys(schema.properties);
orderedProperties = orderProperties(properties, uiSchema["ui:order"]);
} catch(err) {
} catch (err) {
return (
<div>
<p className="config-error" style={{color: "red"}}>
Expand Down
5 changes: 4 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ export function mergeObjects(obj1, obj2, concatArrays = false) {
}

export function asNumber(value) {
if (value === "") {
return undefined;
}
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 Expand Up @@ -500,7 +503,7 @@ export function dataURItoBlob(dataURI) {
// Built the Uint8Array Blob parameter from the base64 string.
const binary = atob(splitted[1]);
const array = [];
for(let i = 0; i < binary.length; i++) {
for (let i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
// Create the blob object
Expand Down
2 changes: 1 addition & 1 deletion test/ArrayField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe("ArrayField", () => {

try {
Simulate.submit(node);
} catch(e) {
} catch (e) {
// Silencing error thrown as failure is expected here
}

Expand Down
2 changes: 1 addition & 1 deletion test/FormContext_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("FormContext", () => {

it("should be passed to TemplateField", () => {
function CustomTemplateField({formContext}) {
return <div id={formContext.foo} />;
return <div id={formContext.foo}/>;
}

const {node} = createFormComponent({
Expand Down
2 changes: 1 addition & 1 deletion test/Form_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ describe("Form", () => {
function submit(node) {
try {
Simulate.submit(node);
} catch(err) {
} catch (err) {
// Validation is expected to fail and call console.error, which is
// stubbed to actually throw in createSandbox().
}
Expand Down
4 changes: 4 additions & 0 deletions test/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ describe("utils", () => {
it("should allow numbers with a 0 in the first decimal place", () => {
expect(asNumber("3.07")).eql(3.07);
});

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

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

0 comments on commit e7b2c4d

Please sign in to comment.