Skip to content

Commit

Permalink
feat(MapFormatter): Add MapFormatter.
Browse files Browse the repository at this point in the history
MapFormatter can be used with repeat decorator to display a list of key value pairs

Closes dart-archive#394
  • Loading branch information
mvuksano committed Apr 18, 2014
1 parent 134530f commit 97985f9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/formatter/map.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
part of angular.formatter_internal;

/**
* Returns a list of key-value pairs.
*
* Usage:
*
* {{ {'key1': 'value1', 'key2':'value2'} | map }}
*/
@Formatter(name:'map')
class MapFormatter<K, V> implements Function {
List<_KeyValue<K, V>> call(Map<K, V> inputMap) {
if (inputMap == null) return null;
List<_KeyValue<K, V>> result = [];
inputMap.keys.forEach((K k) {
result.add(new _KeyValue(k, inputMap[k]));
});
return result;
}
}

class _KeyValue<K, V> {
K key;
V value;

_KeyValue(this.key, this.value);
}
1 change: 1 addition & 0 deletions lib/formatter/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export "package:angular/formatter/module_internal.dart" show
Json,
LimitTo,
Lowercase,
MapFormatter,
Number,
OrderBy,
Uppercase,
Expand Down
2 changes: 2 additions & 0 deletions lib/formatter/module_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ part 'filter.dart';
part 'json.dart';
part 'limit_to.dart';
part 'lowercase.dart';
part 'map.dart';
part 'number.dart';
part 'order_by.dart';
part 'uppercase.dart';
Expand All @@ -26,6 +27,7 @@ class FormatterModule extends Module {
type(Json);
type(LimitTo);
type(Lowercase);
type(MapFormatter);
type(Number);
type(OrderBy);
type(Uppercase);
Expand Down
1 change: 1 addition & 0 deletions test/angular_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ main() {
"angular.formatter_internal.Json",
"angular.formatter_internal.LimitTo",
"angular.formatter_internal.Lowercase",
"angular.formatter_internal.MapFormatter",
"angular.formatter_internal.Number",
"angular.formatter_internal.OrderBy",
"angular.formatter_internal.Stringify",
Expand Down
15 changes: 15 additions & 0 deletions test/formatter/map_spec.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
library map_spec;

import '../_specs.dart';
import 'package:angular/formatter/module.dart';

void main() {
describe('map', () {
it('should convert a map to list of key value pairs', inject((Parser parse, FormatterMap formatters) {
List result = parse('{"key1": "value1", "key2": "value2"} | map').eval(null, formatters);
expect(result.map((kv) => kv.key)).toEqual( ["key1", "key2"]);
expect(result.map((kv) => kv.value)).toEqual( ["value1", "value2"]);
expect(parse('null | map').eval(null, formatters)).toEqual(null);
}));
});
}

0 comments on commit 97985f9

Please sign in to comment.