Skip to content

Commit

Permalink
Added the getLocaleFromPhoneNumber function
Browse files Browse the repository at this point in the history
  • Loading branch information
rik-smit committed Aug 14, 2019
1 parent e316bd3 commit e609a7a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "upinion/intl",
"minimum-stability": "dev",
"version": "1.4.0",
"version": "1.5.0",
"require": {
"upinion/intl-translations": "^1.0"
},
Expand Down
22 changes: 22 additions & 0 deletions helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ const Utils = {
*/
getTranslations(sector) {
return translations[sector];
},

/**
* Return the locale that matches the phone number
*
* @param phoneNumber
* @param currentLocale
* @param returnCurrent When true it returns the current locale if there is no match. When false it returns null
* @returns {null}
*/
getLocaleFromPhoneNumber(phoneNumber, currentLocale, returnCurrent = true) {
let foundLocale = returnCurrent ? currentLocale : null;

Object.keys(locales).forEach((localeName) => {
let e123Number = phoneNumber;
if (phoneNumber.substr(0, 1) !== '+') e123Number = `+${phoneNumber}`;
if (e123Number.indexOf(locales[localeName].callingCode) === 0) {
foundLocale = localeName;
}
});

return foundLocale;
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@upinion/intl",
"version": "1.4.0",
"version": "1.5.0",
"description": "Everything related to the internationalization of our systems.",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit e609a7a

Please sign in to comment.