Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
fix(DateFilter): fix a wrong type
Browse files Browse the repository at this point in the history
Closes #579
  • Loading branch information
technohippy authored and mhevery committed Feb 19, 2014
1 parent cf0160b commit cec3eda
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
21 changes: 11 additions & 10 deletions lib/filter/date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ part of angular.filter;
*
*/
@NgFilter(name:'date')
class DateFilter {
static Map<String, String> MAP = {
class DateFilter implements Function {
static final _MAP = const <String, String> {
'medium': 'MMM d, y h:mm:ss a',
'short': 'M/d/yy h:mm a',
'fullDate': 'EEEE, MMMM d, y',
Expand All @@ -33,7 +33,7 @@ class DateFilter {
'shortTime': 'h:mm a',
};

Map<num, NumberFormat> nfs = new Map<num, NumberFormat>();
var _dfs = <String, DateFormat>{};

/**
* [date]: Date to format either as Date object, milliseconds
Expand All @@ -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);
}
}
12 changes: 9 additions & 3 deletions test/filter/date_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
});
});

0 comments on commit cec3eda

Please sign in to comment.