fix(filter.currency): Return empty string for non-numbers

This commit is contained in:
Vojta Jina 2011-09-07 09:17:10 +02:00
parent 06534413d3
commit 4b1913c5ec
2 changed files with 21 additions and 7 deletions

View file

@ -118,13 +118,14 @@ angularFilter.currency = function(amount, currencySymbol){
var DECIMAL_SEP = '.';
angularFilter.number = function(number, fractionSize) {
if (isNaN(number) || !isFinite(number)) return '';
var formats = this.$service('$locale').NUMBER_FORMATS;
return formatNumber(number, formats.PATTERNS[0], formats.GROUP_SEP,
formats.DECIMAL_SEP, fractionSize);
}
function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
if (isNaN(number) || !isFinite(number)) return '';
var isNegative = number < 0;
number = Math.abs(number);
var numStr = number + '',

View file

@ -83,20 +83,33 @@ describe('filter', function() {
});
describe('currency', function() {
it('should do basic currency filtering', function() {
var html = jqLite('<span/>');
var context = createScope();
context.$element = html;
var currency = bind(context, filter.currency);
var currency, html, context;
beforeEach(function() {
html = jqLite('<span></span>');
context = createScope();
context.$element = html;
currency = bind(context, filter.currency);
});
afterEach(function() {
dealoc(context);
});
it('should do basic currency filtering', function() {
expect(currency(0)).toEqual('$0.00');
expect(html.hasClass('ng-format-negative')).toBeFalsy();
expect(currency(-999)).toEqual('($999.00)');
expect(html.hasClass('ng-format-negative')).toBeTruthy();
expect(currency(1234.5678, "USD$")).toEqual('USD$1,234.57');
expect(html.hasClass('ng-format-negative')).toBeFalsy();
});
dealoc(context);
it('should return empty string for non-numbers', function() {
expect(currency()).toBe('');
expect(html.hasClass('ng-format-negative')).toBeFalsy();
expect(currency('abc')).toBe('');
expect(html.hasClass('ng-format-negative')).toBeFalsy();
});
});