Skip to content
Permalink
Browse files

Parse flots with `parseFloat`; Return `0` instead of `NaN`

  • Loading branch information
Ajaxy committed Aug 30, 2018
1 parent 5c4a966 commit b685d1aa2bb93769c4f64b953360a521e2856beb
Showing with 10 additions and 11 deletions.
  1. +2 −2 lib/index.js
  2. +8 −9 tests/all.test.js
@@ -22,9 +22,9 @@ function castProp(value, propertyDef) {

switch (resolveType(propertyDef)) {
case 'integer':
return parseInt(value, 10);
return parseInt(value, 10) || 0;
case 'number':
return Number(value);
return parseFloat(value) || 0;
case 'boolean':
return value !== 'false';
case 'array':
@@ -6,8 +6,8 @@ describe('All', () => {
a: 'value',
b: '777',
c: '7.77',
d1: '777d', // `777` with integer
d2: '777d', // `NaN` with number
d1: '777d',
d2: '777d',
e: '777',
f: 'true',
g: 'false',
@@ -32,8 +32,8 @@ describe('All', () => {
u: 'value', // missing schema type
v: 'value', // missing type,
w: '',
x: '', // `NaN` with integer
y: '', // `0` with number
x: '',
y: '',
z: '', // `true` with boolean
}, {
type: 'object',
@@ -140,14 +140,13 @@ describe('All', () => {
z: {
type: 'boolean',
},

},
})).toEqual({
a: 'value',
b: 777,
c: 7.77,
d1: 777,
d2: NaN,
d2: 777,
e: '777',
f: true,
g: false,
@@ -165,14 +164,14 @@ describe('All', () => {
n: null,
o: true,
p: null,
q: NaN,
q: 0,
r: undefined,
s: [777, 7.77, NaN],
s: [777, 7.77, 0],
t: undefined,
u: 'value',
v: 'value',
w: '',
x: NaN,
x: 0,
y: 0,
z: true,
});

0 comments on commit b685d1a

Please sign in to comment.
You can’t perform that action at this time.