Skip to content

Commit

Permalink
feat(filters): implement telephone filter
Browse files Browse the repository at this point in the history
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
jniles committed Mar 5, 2016
1 parent 98b8827 commit 4ead9b3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions client/src/js/filters/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ angular.module('bhima.filters')
return $sce.trustAsHtml(depreciateElement.concat(depreciateScript));
};
}]);


60 changes: 60 additions & 0 deletions client/src/js/filters/telephone.js
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();
};
}
4 changes: 2 additions & 2 deletions client/src/partials/cash/modals/receipt.modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h3>{{ "CASH.VOUCHER.RECEIPTS.TITLE" | translate }} <small>#{{ CashReceiptModalC
<!-- warn the client if not debtor id has been passed in -->
<p ng-show="CashReceiptModalCtrl.HttpError" class="text-danger text-center">
<span class="glyphicon glyphicon-alert"></span>
{{ CashReceiptModalCtrl.HttpError |translate }}
{{ CashReceiptModalCtrl.HttpError | translate }}
</p>

<div ng-show="CashReceiptModalCtrl.loading" class="text-center">
Expand All @@ -23,7 +23,7 @@ <h3>{{ "CASH.VOUCHER.RECEIPTS.TITLE" | translate }} <small>#{{ CashReceiptModalC
<strong>{{ CashReceiptModalCtrl.enterprise.name }}</strong> <br />
{{ CashReceiptModalCtrl.enterprise.email }} <br />
<abbr title="{{ 'RECEIPTS.POST_OFFICE_BOX' | translate }}">{{ "RECEIPTS.PO_BOX" | translate }}</abbr>: {{ CashReceiptModalCtrl.enterprise.po_box }} <br />
<abbr title="{{ 'RECEIPTS.TELEPHONE' | translate }}">{{ "RECEIPTS.TEL" | translate }}</abbr>: {{ CashReceiptModalCtrl.enterprise.phone }} <br />
<abbr title="{{ 'RECEIPTS.TELEPHONE' | translate }}">{{ "RECEIPTS.TEL" | translate }}</abbr>: {{ CashReceiptModalCtrl.enterprise.phone | telephone }} <br />
</address>
</div>

Expand Down

0 comments on commit 4ead9b3

Please sign in to comment.