-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(filters): implement telephone filter
This commit implements a telephone number filter in Angular to allow pretty display of telephone numbers. For example usage, check out the cash module receipt. Closes #77.
- Loading branch information
Showing
3 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,3 +70,5 @@ angular.module('bhima.filters') | |
return $sce.trustAsHtml(depreciateElement.concat(depreciateScript)); | ||
}; | ||
}]); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
angular.module('bhima.filters') | ||
.filter('telephone', TelephoneFilter); | ||
|
||
/** | ||
* Telephone Filter | ||
* | ||
* The telephone filter formats telephone numbers into nicely formatted strings. | ||
* If the number is unrecognized, it will simply return it. Otherwise, it works | ||
* as follows for the following formats: | ||
* 1. 0821234567 => (082) 123-4567 | ||
* 2. 243123456789 => +243 (12) 345-6789 | ||
* | ||
* Derived from: http://jsfiddle.net/jorgecas99/S7aSj/. | ||
*/ | ||
function TelephoneFilter() { | ||
return function telephone(tel) { | ||
|
||
if (!tel) { return ''; } | ||
|
||
var value = tel.toString().trim().replace(/^\+/, ''); | ||
|
||
if (value.match(/[^0-9]/)) { | ||
return tel; | ||
} | ||
|
||
var country, city, number; | ||
|
||
switch (value.length) { | ||
case 10: // +1PPP####### -> C (PPP) ###-#### | ||
country = 1; | ||
city = value.slice(0, 3); | ||
number = value.slice(3); | ||
break; | ||
|
||
case 11: // +CPPP####### -> CCC (PP) ###-#### | ||
country = value[0]; | ||
city = value.slice(1, 4); | ||
number = value.slice(4); | ||
break; | ||
|
||
case 12: // +CCCPP####### -> CCC (PP) ###-#### | ||
country = value.slice(0, 3); | ||
city = value.slice(3, 5); | ||
number = value.slice(5); | ||
break; | ||
|
||
default: | ||
return tel; | ||
} | ||
|
||
// if the country is defined, prepend a plus sign | ||
if (country) { | ||
country = '+' + country; | ||
} | ||
|
||
number = number.slice(0, 3) + '-' + number.slice(3); | ||
|
||
return (country + ' (' + city + ') ' + number).trim(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters