diff --git a/lib/filter/date.dart b/lib/filter/date.dart index 04f83a952..75d528703 100644 --- a/lib/filter/date.dart +++ b/lib/filter/date.dart @@ -21,8 +21,8 @@ part of angular.filter; * */ @NgFilter(name:'date') -class DateFilter { - static Map MAP = { +class DateFilter implements Function { + static final _MAP = const { 'medium': 'MMM d, y h:mm:ss a', 'short': 'M/d/yy h:mm a', 'fullDate': 'EEEE, MMMM d, y', @@ -33,7 +33,7 @@ class DateFilter { 'shortTime': 'h:mm a', }; - Map nfs = new Map(); + var _dfs = {}; /** * [date]: Date to format either as Date object, milliseconds @@ -47,18 +47,19 @@ class DateFilter { * mediumDate is used * */ - call(date, [format = r'mediumDate']) { + dynamic call(Object date, [String format = 'mediumDate']) { if (date == '' || date == null) return date; if (date is String) date = DateTime.parse(date); if (date is num) date = new DateTime.fromMillisecondsSinceEpoch(date); if (date is! DateTime) return date; - var nf = nfs[format]; - if (nf == null) { - if (MAP.containsKey(format)) { - format = MAP[format]; + var df = _dfs[format]; + if (df == null) { + if (_MAP.containsKey(format)) { + format = _MAP[format]; } - nf = new DateFormat(format); + df = new DateFormat(format); + _dfs[format] = df; } - return nf.format(date); + return df.format(date); } } diff --git a/test/filter/date_spec.dart b/test/filter/date_spec.dart index e508cfe69..2cb48e19d 100644 --- a/test/filter/date_spec.dart +++ b/test/filter/date_spec.dart @@ -3,9 +3,9 @@ library date_spec; import '../_specs.dart'; main() => describe('date', () { - var morning = DateTime.parse('2010-09-03T07:05:08.008Z'); //7am - var noon = DateTime.parse('2010-09-03T12:05:08.012Z'); //12pm - var midnight = DateTime.parse('2010-09-03T12:05:08.123Z'); //12am + var morning = DateTime.parse('2010-09-03T07:05:08.008Z'); //7am + var noon = DateTime.parse('2010-09-03T12:05:08.012Z'); //12pm + var midnight = DateTime.parse('2010-09-03T12:05:08.123Z'); //12am var earlyDate = DateTime.parse('0001-09-03T05:05:08.000Z'); var date; @@ -57,4 +57,10 @@ main() => describe('date', () { expect(date(noon, "shortTime")). toEqual('12:05 PM'); }); + + it('should use cache without any error', () { + + date(noon, "shortTime"); + date(noon, "shortTime"); + }); });