Skip to content

Commit

Permalink
Improve validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagoporto committed Jun 18, 2018
1 parent 9f03f49 commit f06ff73
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
8 changes: 6 additions & 2 deletions dist/js/CPF.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
};

CPF.validate = function (value) {
if (!value) {
return false;
if (!value && value === true) {
return;
}

if (typeof value === 'number') {
value = String(value);
}

var cleanCPF = value.replace(/\.|-|\s/g, '');
Expand Down
32 changes: 28 additions & 4 deletions spec/cpf-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,42 @@ describe('Formatando CPF com input errado', function () {

describe('No values', function () {
it('Null', function () {
expect(CPF.format(null)).toBeFalsy()
expect(CPF.format(null) === true).toBeFalsy()
})

it('Null', function () {
expect(CPF.format(null) === false).toBeFalsy()
})

it('Undefined', function () {
expect(CPF.format(undefined) === true).toBeFalsy()
})

it('Undefined', function () {
expect(CPF.format(undefined)).toBeFalsy()
expect(CPF.format(undefined) === false).toBeFalsy()
})

it('Empty string', function () {
expect(CPF.format('') === false).toBeFalsy()
})

it('Empty string', function () {
expect(CPF.format('')).toBeFalsy()
expect(CPF.format('') === true).toBeFalsy()
})

it('No value', function () {
expect(CPF.format() === true).toBeFalsy()
})

it('No value', function () {
expect(CPF.format('')).toBeFalsy()
expect(CPF.format() === false).toBeFalsy()
})

it('NaN value', function () {
expect(CPF.format(NaN) === false).toBeFalsy()
})

it('NaN value', function () {
expect(CPF.format(NaN) === true).toBeFalsy()
})
})
8 changes: 6 additions & 2 deletions src/scripts/CPF_SEPARATE.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@
}

CPF.validate = value => {
if (!value) {
return false
if (!value && value === true) {
return
}

if (typeof value === 'number') {
value = String(value)
}

const cleanCPF = value.replace(/\.|-|\s/g, '')
Expand Down

0 comments on commit f06ff73

Please sign in to comment.