diff --git a/lib/formatter/map.dart b/lib/formatter/map.dart new file mode 100644 index 000000000..ec5d00559 --- /dev/null +++ b/lib/formatter/map.dart @@ -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 implements Function { + List<_KeyValue> call(Map inputMap) { + if (inputMap == null) return null; + List<_KeyValue> result = []; + inputMap.keys.forEach((K k) { + result.add(new _KeyValue(k, inputMap[k])); + }); + return result; + } +} + +class _KeyValue { + K key; + V value; + + _KeyValue(this.key, this.value); +} diff --git a/lib/formatter/module.dart b/lib/formatter/module.dart index c2dc22e15..8a89bfc69 100644 --- a/lib/formatter/module.dart +++ b/lib/formatter/module.dart @@ -29,6 +29,7 @@ export "package:angular/formatter/module_internal.dart" show Json, LimitTo, Lowercase, + MapFormatter, Number, OrderBy, Uppercase, diff --git a/lib/formatter/module_internal.dart b/lib/formatter/module_internal.dart index 576b9959c..995a67d40 100644 --- a/lib/formatter/module_internal.dart +++ b/lib/formatter/module_internal.dart @@ -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'; @@ -26,6 +27,7 @@ class FormatterModule extends Module { type(Json); type(LimitTo); type(Lowercase); + type(MapFormatter); type(Number); type(OrderBy); type(Uppercase); diff --git a/test/angular_spec.dart b/test/angular_spec.dart index 13d89c631..226429017 100644 --- a/test/angular_spec.dart +++ b/test/angular_spec.dart @@ -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", diff --git a/test/formatter/map_spec.dart b/test/formatter/map_spec.dart new file mode 100644 index 000000000..2cb31d751 --- /dev/null +++ b/test/formatter/map_spec.dart @@ -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); + })); + }); +}