Skip to content

Commit

Permalink
fix(lib): some country dosen't have phone_number_length error
Browse files Browse the repository at this point in the history
  • Loading branch information
Jian Gong committed Dec 29, 2018
1 parent 732be37 commit b1a2277
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 3,151 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -25,3 +25,9 @@ coverage

# ==== .env
.env

# ==== stackdriver
stackdriver

# === logs
logs
1 change: 1 addition & 0 deletions __tests__/__snapshots__/getISO3166.js.snap
Expand Up @@ -394,6 +394,7 @@ Object {
"985",
"986",
"989",
"888",
],
"phone_number_lengths": Array [
10,
Expand Down
15 changes: 15 additions & 0 deletions __tests__/index.js
Expand Up @@ -213,6 +213,14 @@ describe('Testing USA Phone', () => {
expect(phone(number, country)).toEqual(result);
});
});

describe('Test 14', () => {
const number = '+1 (888) 569-8900';
const result = ['+18885698900', 'USA'];
test('returns ' + result, () => {
expect(phone(number)).toEqual(result);
});
});
});


Expand Down Expand Up @@ -825,6 +833,13 @@ describe('Testing RUS Phone Quick Test', () => {
});
});

describe('Testing Argentina Phone Quick Test', () => {
describe('Test 1', () => {
const number = '+5491156131499';
const result = ['+5491156131499', 'ARG'];
expect(phone(number)).toEqual(result);
});
});

describe('Testing THA Phone Quick Test', () => {
describe('Test 1', () => {
Expand Down
12 changes: 9 additions & 3 deletions lib/getISO3166ByPhone.js
Expand Up @@ -8,7 +8,13 @@ module.exports = function getISO3166ByPhone(phone) {
.filter(iso3166_datum => iso3166_datum.phone_number_lengths.some(length => {
return phone.length === iso3166_datum.country_code.length + length;
}))
.find(iso3166_datum => iso3166_datum.mobile_begin_with.some(beginWith => {
return phone.match(new RegExp('^' + iso3166_datum.country_code + beginWith));
})) || {};
.find(iso3166_datum => {
// some country doesn't have mobile_begin_with
if (iso3166_datum.mobile_begin_with.length) {
return iso3166_datum.mobile_begin_with.some(beginWith => {
return phone.match(new RegExp('^' + iso3166_datum.country_code + beginWith));
});
}
return true;
}) || {};
};
4 changes: 2 additions & 2 deletions lib/iso3166Data.js
Expand Up @@ -28,7 +28,7 @@ module.exports = [
'904', '906', '907', '908', '909', '910', '912', '913', '914', '915', '916', '917', '918', '919', '920',
'925', '927', '928', '929', '931', '934', '935', '936', '937', '938', '940', '941', '947', '949', '951',
'952', '954', '956', '957', '959', '970', '971', '972', '973', '975', '978', '979', '980', '984', '985',
'986', '989'],
'986', '989', '888'],
phone_number_lengths: [10]
},
{
Expand Down Expand Up @@ -521,7 +521,7 @@ module.exports = [
country_code: '20',
country_name: 'Egypt',
mobile_begin_with: ['1'],
phone_number_lengths: [10]
phone_number_lengths: [10, 8]
},
{
alpha2: 'ER',
Expand Down
11 changes: 7 additions & 4 deletions lib/validatePhoneISO3166.js
Expand Up @@ -8,8 +8,11 @@ module.exports = function validatePhoneISO3166(phone, iso3166) {
const phoneWithoutCountry = phone.replace(new RegExp('^' + iso3166.country_code), '');
const {phone_number_lengths, mobile_begin_with} = iso3166;

return (
phone_number_lengths.some(length => phoneWithoutCountry.length === length) &&
mobile_begin_with.some(beginWith => phoneWithoutCountry.match(new RegExp('^' + beginWith)))
);
const isLengthValid = phone_number_lengths.some(length => phoneWithoutCountry.length === length);
// some country doesn't have mobile_begin_with
const isBeginWithValid = mobile_begin_with.length ?
mobile_begin_with.some(beginWith => phoneWithoutCountry.match(new RegExp('^' + beginWith))):
true;

return isLengthValid && isBeginWithValid;
};

0 comments on commit b1a2277

Please sign in to comment.