Skip to content

Commit

Permalink
Fix INT for empty string input (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
laucheukhim committed Jan 25, 2023
1 parent e9e62d4 commit 96f7325
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,15 @@ export function parseNumber(string) {
return string
}

if (string === undefined || string === null || string === '') {
if (string === undefined || string === null) {
return 0
}

if (typeof string === 'boolean') {
string = +string
}

if (!isNaN(string)) {
if (!isNaN(string) && string !== '') {
return parseFloat(string)
}

Expand Down
2 changes: 2 additions & 0 deletions test/math-trig.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ describe('Math & Trig', () => {
expect(mathTrig.INT(error.na)).to.equal(error.na)
expect(mathTrig.INT(5.5)).to.equal(5)
expect(mathTrig.INT('invalid')).to.equal(error.value)
expect(mathTrig.INT('')).to.equal(error.value)
expect(mathTrig.INT(true)).to.equal(1)
})

it('ISO.CEILING', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,15 @@ describe('Utils => Common', () => {
it('parseNumber', () => {
expect(utils.parseNumber()).to.equal(0)
expect(utils.parseNumber(null)).to.equal(0)
expect(utils.parseNumber('')).to.equal(0)
expect(utils.parseNumber('')).to.equal(error.value)
expect(utils.parseNumber(2)).to.equal(2)
expect(utils.parseNumber(error.na)).to.equal(error.na)
expect(utils.parseNumber('text')).to.equal(error.value)
})

it('parseNumberArray', () => {
expect(utils.parseNumberArray()).to.equal(error.value)
expect(utils.parseNumberArray([2, 0, '', null, undefined])).to.eql([2, 0, 0, 0, 0])
expect(utils.parseNumberArray([2, 0, '', null, undefined])).to.eql(error.value)
expect(utils.parseNumberArray([2, 'a', 1, error.na])).to.equal(error.na)
expect(utils.parseNumberArray([2, 'a', 1])).to.equal(error.value)
})
Expand Down

0 comments on commit 96f7325

Please sign in to comment.