Skip to content

Commit

Permalink
fix: proper input validation thrown error
Browse files Browse the repository at this point in the history
  • Loading branch information
kaungmyatlwin committed Jan 14, 2023
1 parent d7aafb6 commit 257412c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions myanmar-phonenumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@

// Removes trailing spaces, dashes and double country codes
myanmarPhoneNumber.sanitizeInput = function (phoneNumber) {
if (!phoneNumber) {
throw Error('Please include phoneNumber parameter.');
}

phoneNumber = phoneNumber.trim();

if (phoneNumber.length === 0) {
return new Error('Phone number is empty.');
}

if (!phoneNumber) {
return new Error('Please include phoneNumber parameter.');
throw Error('Phone number is empty.');
}

phoneNumber = phoneNumber.replace(/[- )(]/g,'')
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
var assert = require('assert');
var myanmarPhoneNumber = require('../myanmar-phonenumber.js')

describe('Error throwing: ', function() {
it('should throw an error when empty string is passed', () => {
assert.throws(() => myanmarPhoneNumber.sanitizeInput(' '), Error)
assert.throws(() => myanmarPhoneNumber.sanitizeInput(''), Error)
})

it('should throw an error when "undefined" is passed', () => {
assert.throws(myanmarPhoneNumber.sanitizeInput, Error)
})
});

describe('Normalize input: ', function() {
it('should return +959784123456 to 09784123456', function() {
assert.equal(myanmarPhoneNumber.normalizeInput('+959784123456'), '09784123456');
Expand Down

0 comments on commit 257412c

Please sign in to comment.