Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Commit

Permalink
fix(ng-currency): Fixed interoperability with cents-to-dolars directi…
Browse files Browse the repository at this point in the history
…ve, issue #140 (#141)

Changed focus listener to use current cleared view value `clearValue($viewValue)` instead of model
value (`$$rawModelValue`) when updating the `$viewValue` for editing.
  • Loading branch information
ognus authored and cecilia-sanare committed Jun 12, 2017
1 parent b3d8292 commit f76438c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/ng-currency.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ export default function ngCurrency($filter, $locale, $timeout, ngCurrencySetting

element.bind('focus', () => {
if (active) {
const groupRegex = new RegExp(`\\${$locale.NUMBER_FORMATS.GROUP_SEP}`, 'g');
const value = [undefined, null, ''].indexOf(controller.$$rawModelValue) === -1 ? $filter('number')(controller.$$rawModelValue, fraction).replace(groupRegex, '') : controller.$$rawModelValue;
const value = clearValue(controller.$viewValue, false);

if (controller.$viewValue !== value) {
controller.$viewValue = value;
controller.$render();
Expand All @@ -168,7 +168,7 @@ export default function ngCurrency($filter, $locale, $timeout, ngCurrencySetting
return RegExp('\\-{0,1}((\\' + dChar + ')|([0-9]{1,}\\' + dChar + '?))&?[0-9]{0,' + fraction + '}', 'g');
}

function clearValue(value) {
function clearValue(value, replaceSeparator = true) {
value = String(value);
let dSeparator = $locale.NUMBER_FORMATS.DECIMAL_SEP;
let cleared = null;
Expand Down Expand Up @@ -196,11 +196,13 @@ export default function ngCurrency($filter, $locale, $timeout, ngCurrencySetting

if (decimalRex(dSeparator).test(value)) {
cleared = value.match(decimalRex(dSeparator))
.join('').match(clearRex(dSeparator));
cleared = cleared ? cleared[0].replace(dSeparator, '.') : null;
.join('').match(clearRex(dSeparator)) || [''];

cleared = cleared[0];
cleared = replaceSeparator ? cleared.replace(dSeparator, '.') : cleared;
}

return cleared;
return cleared || null;
}

function getCurrencySymbol() {
Expand Down
10 changes: 4 additions & 6 deletions test/ng-currency/ng-currency.directive.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,10 @@ describe('ngCurrency directive tests', () => {
expect(element.val()).toEqual('$123.45');
});

it('should update the model correctly', () => {
element.val('$123.45');
element.triggerHandler('input');
element.triggerHandler('blur');
expect(scope.value).toEqual(12345);
expect(element.val()).toEqual('$123.45');
it('should update view value on focus correctly', () => {
expect(element.val()).toEqual('$1.00');
element.triggerHandler('focus');
expect(element.val()).toEqual('1.00');
});
});

Expand Down

0 comments on commit f76438c

Please sign in to comment.