Skip to content

Commit

Permalink
Bump to 2.4.10
Browse files Browse the repository at this point in the history
- #190 Fix the mechanism for the input with plus sign and country, but without country code
- updated dependencies version
  • Loading branch information
Bossa573 committed Apr 28, 2020
1 parent 2b651ae commit 17c24b7
Show file tree
Hide file tree
Showing 5 changed files with 1,851 additions and 994 deletions.
129 changes: 129 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1298,3 +1298,132 @@ describe('Landline phone number test', () => {
});
});

describe('#190 phone number with plus sign BUT without country code (intentionally wrong input)', () => {
test('1. with +, country code, and country, should get the result', () => {
const number = '+85293785433';
const country = 'HKG';
const result = ['+85293785433', 'HKG'];
expect(phone(number, country)).toEqual(result);
});

test('2. without +, with country code, with country, should get the result', () => {
const number = '85293785433';
const country = 'HKG';
const result = ['+85293785433', 'HKG'];
expect(phone(number, country)).toEqual(result);
});

test('3a. with +, without country code, with country, should get empty array', () => {
const number = '+93785433';
const country = 'HKG';
const result = [];
expect(phone(number, country)).toEqual(result);
});

test('3b. with +, without country code, with country, should get empty array', () => {
const number = '+41414141';
const country = 'NO';
const result = [];
expect(phone(number, country)).toEqual(result);
});

test('3c. with +, without country code, with country, should get empty array', () => {
const number = '+2011231234';
const country = 'USA';
const result = [];
expect(phone(number, country)).toEqual(result);
});

test('4a. without +, without country code, with country, should get the result', () => {
const number = '93785433';
const country = 'HKG';
const result = ['+85293785433', 'HKG'];
expect(phone(number, country)).toEqual(result);
});

test('4b. without +, without country code, with country, should get the result', () => {
const number = '2014125632';
const country = 'USA';
const result = ['+12014125632', 'USA'];
expect(phone(number, country)).toEqual(result);
});

test('4c. without +, without country code, with country, should get the result', () => {
const number = '41414141';
const country = 'NO';
const result = ['+4741414141', 'NOR'];
expect(phone(number, country)).toEqual(result);
});

test('5a. with +, with country code, without country, should get the result', () => {
const number = '+4741414141';
const result = ['+4741414141', 'NOR'];
expect(phone(number)).toEqual(result);
});

test('5b. with +, with country code, without country, should get the result', () => {
const number = '+85296587452';
const result = ['+85296587452', 'HKG'];
expect(phone(number)).toEqual(result);
});

test('5c. with +, with country code, without country, should get the result', () => {
const number = '+13612145896';
const result = ['+13612145896', 'USA'];
expect(phone(number)).toEqual(result);
});

test('6a. without +, with country code, without country, should get the result', () => {
const number = '13612145896';
const result = ['+13612145896', 'USA'];
expect(phone(number)).toEqual(result);
});

test('6b. without +, with country code, without country, default USA and should get empty result', () => {
const number = '4741414141';
const result = [];
expect(phone(number)).toEqual(result);
});

test('6c. without +, with country code, without country, default USA and should get empty result', () => {
const number = '96587452';
const result = [];
expect(phone(number)).toEqual(result);
});

test('7a. with +, without country code, without country, should get empty result', () => {
const number = '+96587452';
const result = [];
expect(phone(number)).toEqual(result);
});

test('7b. with +, without country code, without country, should get empty result', () => {
const number = '+3612145896';
const result = [];
expect(phone(number)).toEqual(result);
});

test('7c. with +, without country code, without country, should get empty result', () => {
const number = '+41414141';
const result = [];
expect(phone(number)).toEqual(result);
});

test('8a. without +, without country code, without country, should get empty result', () => {
const number = '96587452';
const result = [];
expect(phone(number)).toEqual(result);
});

test('8b. without +, without country code, without country, default USA, should get result', () => {
const number = '3612145896';
const result = ['+13612145896', 'USA'];
expect(phone(number)).toEqual(result);
});

test('8c. without +, without country code, without country, should get empty result', () => {
const number = '41414141';
const result = [];
expect(phone(number)).toEqual(result);
});
});
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = function (phone, country, allowLandline) {
defaultCountry = true;
}

let validateResult = validatePhoneIso3166(formatPhone, iso3166, allowLandline);
let validateResult = validatePhoneIso3166(formatPhone, iso3166, allowLandline, plusSign);

if (validateResult) {
return ['+' + formatPhone, iso3166.alpha3];
Expand All @@ -98,7 +98,7 @@ module.exports = function (phone, country, allowLandline) {
if (defaultCountry) {
// also try to validate against CAN for default country, as CAN is also start with +1
iso3166 = getISO3166('CAN');
validateResult = validatePhoneIso3166(formatPhone, iso3166, allowLandline);
validateResult = validatePhoneIso3166(formatPhone, iso3166, allowLandline, plusSign);
if (validateResult) {
return ['+' + formatPhone, iso3166.alpha3];
}
Expand Down
22 changes: 21 additions & 1 deletion lib/validatePhoneISO3166.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
'use strict';

module.exports = function validatePhoneISO3166(phone, iso3166, allowLandline) {
/**
*
* @param {string} phone - phone number without plus sign, with or without country calling code
* @param {Object} iso3166 - iso 3166 data
* @param {String} iso3166.country_code - country calling codes
* @param {Array} iso3166.phone_number_lengths - all available phone number lengths for this country
* @param {Array} iso3166.mobile_begin_with - mobile begin with number
* @param {boolean} allowLandline - true if we skip mobile begin with checking
* @param {boolean} plusSign - true if the input contains a plus sign
* @returns {*|boolean}
*/
module.exports = function validatePhoneISO3166(phone, iso3166, allowLandline, plusSign) {
if (!iso3166.phone_number_lengths) {
return false;
}

// remove country calling code from the phone number
const phoneWithoutCountry = phone.replace(new RegExp('^' + iso3166.country_code), '');

// if the phone number have +, iso3166 detected,
// but the phone number does not have country calling code
// then should consider the phone number as invalid
if (plusSign && iso3166 && phoneWithoutCountry.length === phone.length) {
return false;
}

const phone_number_lengths = iso3166.phone_number_lengths;
const mobile_begin_with = iso3166.mobile_begin_with;

Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phone",
"version": "2.4.9",
"version": "2.4.10",
"description": "With a given country and phone number, validate and format the phone number to E.164 standard",
"main": "./dist/index.js",
"engines": {
Expand All @@ -27,7 +27,7 @@
"url": "https://github.com/aftership/phone/issues"
},
"devDependencies": {
"@types/node": "^13.7.1",
"@types/node": "^13.13.4",
"babel-cli": "^6.26.0",
"babel-loader": "^7.1.5",
"babel-minify-webpack-plugin": "^0.3.1",
Expand All @@ -37,14 +37,14 @@
"dotenv": "^8.2.0",
"eslint": "^6.8.0",
"eslint-config-aftership": "^5.1.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-import": "^2.20.2",
"idempotent-babel-polyfill": "^7.4.4",
"jest": "^24.8.0",
"jest": "^25.4.0",
"lodash": "^4.17.15",
"ts-node": "^8.6.2",
"twilio": "^3.39.4",
"typescript": "^3.7.5",
"webpack": "^4.41.6",
"ts-node": "^8.9.1",
"twilio": "^3.42.2",
"typescript": "^3.8.3",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3",
"webpack-sources": "^1.4.3"
Expand Down
Loading

0 comments on commit 17c24b7

Please sign in to comment.