From 62743a54b79187e6c1325c0f6dec0f474147881d Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Wed, 7 Sep 2016 23:40:38 +0300 Subject: [PATCH] feat(currencyFilter): trim whitespace around an empty currency symbol In most locales, this won't make a difference (since they do not have whitespace around their currency symbols). In locales where there is a whitespace separating the currency symbol from the number, it makes sense to also remove such whitespace if the user specified an empty currency symbol (indicating they just want the number). Fixes #15018 Closes #15085 Closes #15105 --- src/ng/filter/filters.js | 5 ++++- test/ng/filter/filtersSpec.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ng/filter/filters.js b/src/ng/filter/filters.js index 5a79a7799929..482b31897c79 100644 --- a/src/ng/filter/filters.js +++ b/src/ng/filter/filters.js @@ -68,11 +68,14 @@ function currencyFilter($locale) { fractionSize = formats.PATTERNS[1].maxFrac; } + // If the currency symbol is empty, trim whitespace around the symbol + var currencySymbolRe = !currencySymbol ? /\s*\u00A4\s*/g : /\u00A4/g; + // if null or undefined pass it through return (amount == null) ? amount : formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, fractionSize). - replace(/\u00A4/g, currencySymbol); + replace(currencySymbolRe, currencySymbol); }; } diff --git a/test/ng/filter/filtersSpec.js b/test/ng/filter/filtersSpec.js index 8e3a54a0b2df..0646dfa656af 100644 --- a/test/ng/filter/filtersSpec.js +++ b/test/ng/filter/filtersSpec.js @@ -186,6 +186,19 @@ describe('filters', function() { expect(currency(1.07)).toBe('$1.1'); })); + + it('should trim whitespace around the currency symbol if it is empty', + inject(function($locale) { + var pattern = $locale.NUMBER_FORMATS.PATTERNS[1]; + pattern.posPre = pattern.posSuf = ' \u00A4 '; + pattern.negPre = pattern.negSuf = ' - \u00A4 - '; + + expect(currency(+1.07, '$')).toBe(' $ 1.07 $ '); + expect(currency(-1.07, '$')).toBe(' - $ - 1.07 - $ - '); + expect(currency(+1.07, '')).toBe('1.07'); + expect(currency(-1.07, '')).toBe(' -- 1.07 -- '); + }) + ); }); describe('number', function() {