Skip to content

Commit 62743a5

Browse files
committed
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 angular#15018 Closes angular#15085 Closes angular#15105
1 parent 22450e5 commit 62743a5

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/ng/filter/filters.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,14 @@ function currencyFilter($locale) {
6868
fractionSize = formats.PATTERNS[1].maxFrac;
6969
}
7070

71+
// If the currency symbol is empty, trim whitespace around the symbol
72+
var currencySymbolRe = !currencySymbol ? /\s*\u00A4\s*/g : /\u00A4/g;
73+
7174
// if null or undefined pass it through
7275
return (amount == null)
7376
? amount
7477
: formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, fractionSize).
75-
replace(/\u00A4/g, currencySymbol);
78+
replace(currencySymbolRe, currencySymbol);
7679
};
7780
}
7881

test/ng/filter/filtersSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ describe('filters', function() {
186186

187187
expect(currency(1.07)).toBe('$1.1');
188188
}));
189+
190+
it('should trim whitespace around the currency symbol if it is empty',
191+
inject(function($locale) {
192+
var pattern = $locale.NUMBER_FORMATS.PATTERNS[1];
193+
pattern.posPre = pattern.posSuf = ' \u00A4 ';
194+
pattern.negPre = pattern.negSuf = ' - \u00A4 - ';
195+
196+
expect(currency(+1.07, '$')).toBe(' $ 1.07 $ ');
197+
expect(currency(-1.07, '$')).toBe(' - $ - 1.07 - $ - ');
198+
expect(currency(+1.07, '')).toBe('1.07');
199+
expect(currency(-1.07, '')).toBe(' -- 1.07 -- ');
200+
})
201+
);
189202
});
190203

191204
describe('number', function() {

0 commit comments

Comments
 (0)