Comparing version 1.0.7 to 1.0.8
@@ -19,4 +19,4 @@ { | ||
"angular": "~1.3.13", | ||
"lodash": "2.4.1" | ||
"lodash": "~4.5.1" | ||
} | ||
} |
{ | ||
"name": "nmPhone", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Angular directives and a separate module to validate international phone numbers.", | ||
@@ -13,3 +13,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"lodash": "4.5.1" | ||
"lodash": "^4.5.1" | ||
}, | ||
@@ -16,0 +16,0 @@ "devDependencies": { |
@@ -15,16 +15,9 @@ # nmPhone | ||
If you need only the `nmPhoneUtils` library you can include `dist/utils.js`. | ||
You will need to include 2 files into your app: | ||
* `dist/utils.js`: it will load `nmPhoneUtils` module (use by the directives) | ||
* `src/directive.js`: it will load `nmPhoneNumberSingleInput` and `nmPhoneNumber` directives | ||
To use the directives you need to include `src/nmPhoneNumber.js` as the first file (it will register the module `namshi.nmPhoneNumber`), then, based on what you want to use, you will need to include: | ||
If you want to use the directives remember to add the module name into the app definition | ||
* `dist/utils.js` + `src/nmPhoneUtils.js`: it will attach the `nmPhoneUtils` module as a service in your angular app | ||
* `src/nmNumeric.js`: it's a directive that will make sure whatever you type will retain only numbers | ||
* `src/nmRangeLength.js`: it's a directive that will make sure whatever you type will be between a min and max length | ||
* `src/PhoneNumberSingleInput`: it's a directive that will make sure you are typing an international number (+<prefix>[-<carrier>]-<number>) | ||
* `src/PhoneNumberMultiInput`: it's a directive that will give you some utilities to handle the validation of an international number given as a form with 3 different inputs: country, carrier, number. | ||
Remember to add the module name into the app definition | ||
``` | ||
@@ -31,0 +24,0 @@ angular.module('my_App', [..., 'namshi.nmPhoneNumber', ...]); |
185
src/utils.js
@@ -9,26 +9,5 @@ 'use strict'; | ||
function contains(list, item) { | ||
return _(list).map(function(litem) {return litem.toString()}).includes(item.toString()); | ||
return _(list).map(_.toString).includes(_.toString(item)); | ||
}; | ||
/** | ||
* Extracts only the numeric characters from a string. | ||
* | ||
* @param {String} value | ||
* @return {String} | ||
*/ | ||
function extractNumbers(value) { | ||
return value ? value.replace(/[^0-9]+/g, '') : ''; | ||
}; | ||
/** | ||
* Shorten a string to a given length if it is longr than that. | ||
* | ||
* @param {String} value | ||
* @param {Number} length | ||
* @return {String} | ||
*/ | ||
function shortenToLength(value, length) { | ||
return value.substring(0, length); | ||
}; | ||
var emptyPhoneObject = { | ||
@@ -52,30 +31,2 @@ fkCountry: '', | ||
/** | ||
* load country codes (971, 973, etc.) from config | ||
* | ||
* @param {Object} phoneSettings | ||
* @return {Array} | ||
*/ | ||
loadCountryCodes: function loadCountryCodes(phoneSettings) { | ||
return _.map(phoneSettings, function (item) { | ||
return item.phoneCodes.country; | ||
}); | ||
}, | ||
/** | ||
* Get country specific `phoneSetting` object | ||
* | ||
* @param {Object} phoneSettings | ||
* @param {string|integer} countryCode | ||
* @return {Object} | ||
*/ | ||
getCountryPhoneSettings: function getCountryPhoneSettings(phoneSettings, countryCode) { | ||
var phoneSetting = _(phoneSettings).filter(function(item) { | ||
return ''+item.phoneCodes.country === ''+countryCode; | ||
}).value().shift(); | ||
return phoneSetting && phoneSetting.phoneCodes; | ||
}, | ||
/** | ||
* Will verfy that a given value is in a phone number format. | ||
@@ -87,3 +38,3 @@ * The format is defined by a regex (see phoneRegex variable) | ||
*/ | ||
isValidPhoneNumber: function isValidPhoneNumber(value) { | ||
isValidPhoneNumber: function(value) { | ||
return phoneRegex.test(value); | ||
@@ -108,15 +59,6 @@ }, | ||
* but all the values are going to be empty. | ||
* | ||
* If the contextCountry is different from the phone one it | ||
* will be returned an empty object with just the countryCode | ||
* as the contextCountry specifies. | ||
*/ | ||
parsePhone: function parsePhone(phone, phoneSettings, contextCountry) { | ||
var eo = _.clone(emptyPhoneObject); | ||
parsePhone: function(phone, phoneSettings) { | ||
if (!phone || !phoneRegex.test(phone)) { | ||
eo.fkCountry = contextCountry || ''; | ||
eo.cellTokens.countryCode = phoneSettings[contextCountry] ? phoneSettings[contextCountry].phoneCodes.country : ''; | ||
return eo; | ||
return emptyPhoneObject; | ||
} | ||
@@ -130,7 +72,4 @@ | ||
if (!phoneCountryConfig || (contextCountry && (contextCountry.toLowerCase() !== phoneCountryConfig.iso2Code.toLowerCase()))) { | ||
eo.fkCountry = contextCountry || ''; | ||
eo.cellTokens.countryCode = phoneSettings[contextCountry] ? phoneSettings[contextCountry].phoneCodes.country : ''; | ||
return eo; | ||
if (!phoneCountryConfig) { | ||
return emptyPhoneObject; | ||
} | ||
@@ -142,7 +81,7 @@ | ||
return { | ||
fkCountry: ''+phoneCountryConfig.iso2Code, | ||
fkCountry: phoneCountryConfig.iso2Code, | ||
cellTokens: { | ||
countryCode: !!countryCode ? Number(countryCode) : "", | ||
carrierCode: !!carrierCode ? Number(carrierCode) : "", | ||
number: ''+number | ||
countryCode: countryCode, | ||
carrierCode: carrierCode, | ||
number: number | ||
} | ||
@@ -170,3 +109,3 @@ }; | ||
*/ | ||
validateCountry: function validateCountry(countryId, phoneSettings) { | ||
validateCountry: function(countryId, phoneSettings) { | ||
if (!countryId) { | ||
@@ -201,3 +140,3 @@ return {valid: false, errors: ['empty']}; | ||
*/ | ||
validateCarrierCode: function validateCarrierCode(carrierCode, countryId, phoneSettings) { | ||
validateCarrierCode: function(carrierCode, countryId, phoneSettings) { | ||
if (!countryId) { | ||
@@ -253,3 +192,3 @@ return { | ||
*/ | ||
validateNumber: function validateNumber(number, min, max) { | ||
validateNumber: function(number, min, max) { | ||
if (!number) { | ||
@@ -294,3 +233,5 @@ return { | ||
*/ | ||
extractNumbers: extractNumbers, | ||
extractNumbers: function (value) { | ||
return value.replace(/[^\d]+/g, ''); | ||
}, | ||
@@ -304,94 +245,4 @@ /** | ||
*/ | ||
shortenToLength: shortenToLength, | ||
/** | ||
* Format the phoneData object into a string of the form: | ||
* | ||
* +<countrycode>-[<carriercode>-]<number> | ||
* | ||
* @param {Object} phoneData | ||
* @return {String} | ||
*/ | ||
getFormattedNumber: function getFormattedNumber(phoneData) { | ||
if (!phoneData.cellTokens) { | ||
return ''; | ||
} | ||
var data = phoneData.cellTokens; | ||
return [ | ||
(data.countryCode ? '+' + data.countryCode : ''), | ||
data.carrierCode, | ||
data.number | ||
].filter(function(v) { return !!v ; }).join('-'); | ||
}, | ||
isPhoneDataValid: function isPhoneDataValid(phoneData, phoneSettings, min, max) { | ||
return ( | ||
!!phoneData.fkCountry && | ||
this.validateCountry(phoneData.fkCountry, phoneSettings).valid && | ||
this.validateCarrierCode(phoneData.cellTokens.carrierCode, phoneData.fkCountry, phoneSettings).valid && | ||
!!phoneData.cellTokens.number && | ||
this.validateNumber(phoneData.cellTokens.number, min, max).valid | ||
); | ||
}, | ||
/** | ||
* Return the numeric validator function. | ||
* The numericValidator function will filter out non numeric characters from the given input and call the callback. | ||
* The callback is invoked only if one or more characters are removed from the input string | ||
* | ||
* @param callback | ||
* @returns {*} | ||
*/ | ||
getNumericValidator: function getNumericValidator(callback) { | ||
return function numericValidator(input) { | ||
var output; | ||
if (_.isString(input)) { | ||
output = extractNumbers(input); | ||
if (input !== output) { | ||
callback(output); | ||
} | ||
} | ||
return output; | ||
}; | ||
}, | ||
/** | ||
* Parser and formatter to invalidate a field which its value length exceeded the maximum number of | ||
* allowed characters | ||
* | ||
* @param value | ||
* @returns {*} | ||
*/ | ||
getMaxLengthValidator: function getMaxLengthValidator(maxlength, callback) { | ||
return function maxLengthValidator(value) { | ||
var isValid = !value || value.length <= maxlength; | ||
if (!isValid && !!value) { | ||
value = value.substring(0, maxlength); | ||
} | ||
callback(isValid, value); | ||
return value; | ||
} | ||
}, | ||
/** | ||
* Parser and Formatter to invalidate a field which its value length goes below the minimum number of | ||
* allowed characters | ||
* | ||
* @param value | ||
* @returns {*} | ||
*/ | ||
getMinLengthValidator: function getMinLengthValidator(minlength, callback) { | ||
return function minLengthValidator(value) { | ||
var isValid = !value || value.length >= minlength; | ||
callback(isValid, value); | ||
return value; | ||
} | ||
shortenToLength: function (value, length) { | ||
return value.substring(0, length); | ||
} | ||
@@ -398,0 +249,0 @@ } |
@@ -40,4 +40,4 @@ 'use strict'; | ||
"cellTokens": { | ||
"carrierCode": 52, | ||
"countryCode": 971, | ||
"carrierCode": "52", | ||
"countryCode": "971", | ||
"number": "000000" | ||
@@ -48,6 +48,6 @@ }, | ||
expect(utils.parsePhone('+966-50-000000', phoneSettings)).to.deep.equal({ | ||
expect(utils.parsePhone('+966-00-000000', phoneSettings)).to.deep.equal({ | ||
"cellTokens": { | ||
"carrierCode": 50, | ||
"countryCode": 966, | ||
"carrierCode": "00", | ||
"countryCode": "966", | ||
"number": "000000" | ||
@@ -61,3 +61,3 @@ }, | ||
"carrierCode": "", | ||
"countryCode": 973, | ||
"countryCode": "973", | ||
"number": "000000" | ||
@@ -71,3 +71,3 @@ }, | ||
"carrierCode": "", | ||
"countryCode": 973, | ||
"countryCode": "973", | ||
"number": "000000" | ||
@@ -78,3 +78,3 @@ }, | ||
expect(utils.parsePhone('+not-50-valid', phoneSettings)).to.deep.equal({ | ||
expect(utils.parsePhone('+not-00-valid', phoneSettings)).to.deep.equal({ | ||
"cellTokens": { | ||
@@ -97,3 +97,3 @@ "carrierCode": "", | ||
expect(utils.parsePhone('+999-50-000000', phoneSettings)).to.deep.equal({ | ||
expect(utils.parsePhone('+999-00-000000', phoneSettings)).to.deep.equal({ | ||
"cellTokens": { | ||
@@ -107,11 +107,2 @@ "carrierCode": "", | ||
expect(utils.parsePhone('+971-52-0000000', phoneSettings, 'sa')).to.deep.equal({ | ||
"cellTokens": { | ||
"carrierCode": "", | ||
"countryCode": 966, | ||
"number": "" | ||
}, | ||
"fkCountry": "sa" | ||
}); | ||
done(); | ||
@@ -204,228 +195,2 @@ }); | ||
}); | ||
describe('extractNumbers', function() { | ||
it('should return only numbers inside a string and an empty string if none is found', function(done) { | ||
expect(utils.extractNumbers('h8h48hf8h4hf38h92')).to.equal('848843892'); | ||
expect(utils.extractNumbers('12345')).to.equal('12345'); | ||
expect(utils.extractNumbers('')).to.equal(''); | ||
expect(utils.extractNumbers(undefined)).to.equal(''); | ||
expect(utils.extractNumbers('hello')).to.equal(''); | ||
done(); | ||
}); | ||
}); | ||
describe('getFormattedNumber', function() { | ||
it('should return the phone number as a tring given an object', function(done) { | ||
var phoneData = { | ||
"cellTokens": { | ||
"carrierCode": 50, | ||
"countryCode": 971, | ||
"number": "0000000" | ||
}, | ||
"fkCountry": "AE" | ||
}; | ||
expect(utils.getFormattedNumber(phoneData)).to.equal('+971-50-0000000'); | ||
done(); | ||
}); | ||
it('should return the phone number as a tring given an object without carrier code', function(done) { | ||
var phoneData = { | ||
"cellTokens": { | ||
"carrierCode": "", | ||
"countryCode": 444, | ||
"number": "0000000" | ||
}, | ||
"fkCountry": "" | ||
}; | ||
expect(utils.getFormattedNumber(phoneData)).to.equal('+444-0000000'); | ||
done(); | ||
}); | ||
it('should return an empty string for an empty object or empty fields', function(done) { | ||
var phoneData = { | ||
"cellTokens": { | ||
"carrierCode": "", | ||
"countryCode": "", | ||
"number": "" | ||
}, | ||
"fkCountry": "" | ||
}; | ||
expect(utils.getFormattedNumber(phoneData)).to.equal(''); | ||
expect(utils.getFormattedNumber({})).to.equal(''); | ||
done(); | ||
}); | ||
}); | ||
describe('isPhoneDataValid', function() { | ||
it('should return true for a valid phoneDataObject', function(done) { | ||
var phoneData = { | ||
"cellTokens": { | ||
"carrierCode": 50, | ||
"countryCode": 971, | ||
"number": "0000000" | ||
}, | ||
"fkCountry": "AE" | ||
}; | ||
expect(utils.isPhoneDataValid(phoneData, phoneSettings)).to.equal(true); | ||
done(); | ||
}); | ||
it('should return true for a valid phoneDataObject', function(done) { | ||
var phoneData = { | ||
"cellTokens": { | ||
"carrierCode": "", | ||
"countryCode": 973, | ||
"number": "000000" | ||
}, | ||
"fkCountry": "BH" | ||
}; | ||
expect(utils.isPhoneDataValid(phoneData, phoneSettings)).to.equal(true); | ||
done(); | ||
}); | ||
it('should return false for an invalid phoneData object', function(done) { | ||
expect(utils.isPhoneDataValid({}, phoneSettings, 7, 7)).to.equal(false); | ||
expect(utils.isPhoneDataValid({"cellTokens": {}}, phoneSettings, 7, 7)).to.equal(false); | ||
expect(utils.isPhoneDataValid({"fkCountry": ""}, phoneSettings, 7, 7)).to.equal(false); | ||
expect(utils.isPhoneDataValid({ | ||
"cellTokens": { | ||
"carrierCode": "", | ||
"countryCode": "971", | ||
"number": "0000000" | ||
}, | ||
"fkCountry": "AE" | ||
}, phoneSettings, 7, 7)).to.equal(false); | ||
expect(utils.isPhoneDataValid({ | ||
"cellTokens": { | ||
"carrierCode": "50", | ||
"countryCode": "", | ||
"number": "0000000" | ||
}, | ||
"fkCountry": "" | ||
}, phoneSettings, 7, 7)).to.equal(false); | ||
expect(utils.isPhoneDataValid({ | ||
"cellTokens": { | ||
"carrierCode": "50", | ||
"countryCode": "971", | ||
"number": "" | ||
}, | ||
"fkCountry": "AE" | ||
}, phoneSettings, 7, 7)).to.equal(false); | ||
expect(utils.isPhoneDataValid({ | ||
"cellTokens": { | ||
"carrierCode": "50", | ||
"countryCode": "971", | ||
"number": "000" | ||
}, | ||
"fkCountry": "AE" | ||
}, phoneSettings, 7, 7)).to.equal(false); | ||
expect(utils.isPhoneDataValid({ | ||
"cellTokens": { | ||
"carrierCode": "50", | ||
"countryCode": "971", | ||
"number": "00000000" | ||
}, | ||
"fkCountry": "AE" | ||
}, phoneSettings, 7, 7)).to.equal(false); | ||
/** NOTE: TO BE FIXED. This will end up returning true ! */ | ||
// expect(utils.isPhoneDataValid({ | ||
// "cellTokens": { | ||
// "carrierCode": "50", | ||
// "countryCode": "", | ||
// "number": "0000000" | ||
// }, | ||
// "fkCountry": "AE" | ||
// }, phoneSettings, 7, 7)).to.equal(false); | ||
done(); | ||
}); | ||
it('should return an empty string for an empty object or empty fields', function(done) { | ||
var phoneData = { | ||
"cellTokens": { | ||
"carrierCode": "", | ||
"countryCode": "", | ||
"number": "" | ||
}, | ||
"fkCountry": "" | ||
}; | ||
expect(utils.getFormattedNumber(phoneData)).to.equal(''); | ||
expect(utils.getFormattedNumber({})).to.equal(''); | ||
done(); | ||
}); | ||
}); | ||
describe('loadCountryCodes', function() { | ||
it('should return the list of country codes in the settings', function(done) { | ||
expect(utils.loadCountryCodes(phoneSettings)).to.deep.equal([971, 966, 973]); | ||
done(); | ||
}); | ||
}); | ||
describe('getCountryPhoneSettings', function() { | ||
it('should return the country phone setting object for the given country, or return undefined', function(done) { | ||
expect(utils.getCountryPhoneSettings(phoneSettings, 971)).to.deep.equal({ | ||
"carrierCodes": [50,52,54,55,56], | ||
"country": 971, | ||
"maxlength": 7, | ||
"minlength": 7 | ||
}); | ||
expect(utils.getCountryPhoneSettings(phoneSettings, 39)).to.equal(undefined); | ||
done(); | ||
}); | ||
}); | ||
describe('getNumericValidator', function() { | ||
it('should return a function that will filter out not numeric characters', function(done) { | ||
var test = function(result) { | ||
expect(result).to.equal('012345'); | ||
done(); | ||
} | ||
utils.getNumericValidator(test)('a0123b45'); | ||
}); | ||
it('should not call the callback if the the input it is already numeric', function(done) { | ||
var counter = 0; | ||
var test = function(result) { | ||
counter++; | ||
expect(result).to.equal('012345'); | ||
} | ||
var result =utils.getNumericValidator(test)('a0123b45'); | ||
result = utils.getNumericValidator(test)('012345'); | ||
expect(result).to.equal('012345'); | ||
expect(counter).to.equal(1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -432,0 +197,0 @@ |
@@ -11,7 +11,3 @@ var path = require('path'); | ||
library: "nmPhoneUtils" | ||
}, | ||
target: "web", | ||
externals: [ | ||
{"lodash": "_"} | ||
] | ||
} | ||
}; |
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
564664
16403
10
49
+ Addedlodash@4.17.21(transitive)
- Removedlodash@4.5.1(transitive)
Updatedlodash@^4.5.1