From e43347adeeee6baf4c72d7d709b07b39821af560 Mon Sep 17 00:00:00 2001 From: Guillermo Moreno Date: Wed, 6 Jul 2016 16:51:30 -0400 Subject: [PATCH] feat(ui-money-mask): add currency symbol attribute --- src/global/money/money.js | 5 +++-- src/global/money/money.test.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/global/money/money.js b/src/global/money/money.js index c052daf4..276769a8 100644 --- a/src/global/money/money.js +++ b/src/global/money/money.js @@ -10,12 +10,13 @@ function MoneyMaskDirective($locale, $parse, PreFormatters) { link: function(scope, element, attrs, ctrl) { var decimalDelimiter = $locale.NUMBER_FORMATS.DECIMAL_SEP, thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP, - currencySym = $locale.NUMBER_FORMATS.CURRENCY_SYM, + currencySym = (typeof (attrs.currencySymbol) !== 'undefined') ? attrs.currencySymbol : $locale.NUMBER_FORMATS.CURRENCY_SYM, decimals = $parse(attrs.uiMoneyMask)(scope); function maskFactory(decimals) { var decimalsPattern = decimals > 0 ? decimalDelimiter + new Array(decimals + 1).join('0') : ''; - var maskPattern = currencySym + ' #' + thousandsDelimiter + '##0' + decimalsPattern; + var currencySymbolPattern = (currencySym) ? currencySym + ' #' : '#'; + var maskPattern = currencySymbolPattern + thousandsDelimiter + '##0' + decimalsPattern; return new StringMask(maskPattern, {reverse: true}); } diff --git a/src/global/money/money.test.js b/src/global/money/money.test.js index 4d9a987b..3457c3fc 100644 --- a/src/global/money/money.test.js +++ b/src/global/money/money.test.js @@ -180,4 +180,22 @@ describe('ui-money-mask', function() { expect(model.$viewValue).toBe('$ 0.00'); expect(model.$modelValue).toBe(0); }); + + it('should change currency symbol if defined', function() { + var input = TestUtil.compile('', { + model: 345.00 + }); + + var model = input.controller('ngModel'); + expect(model.$viewValue).toBe('F$ 345.00'); + }); + + it('should remove space if currency symbol is defined but empty', function() { + var input = TestUtil.compile('', { + model: 345.00 + }); + + var model = input.controller('ngModel'); + expect(model.$viewValue).toBe('345.00'); + }); });