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

Commit

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

Closes #394

Closes #931
  • Loading branch information
mvuksano authored and mhevery committed Apr 21, 2014
1 parent 474b002 commit d2780f8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/formatter/arrayify.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
part of angular.formatter_internal;

/**
* Given a Map, returns a list of items which have `key` and `value` property.
*
* Usage:
*
* <div ng-repeat="item in {'key1': 'value1', 'key2':'value2'} | arrayify">
* {{item.key}}: {{item.value}}
* </div>
*/
@Formatter(name:'arrayify')
class Arrayify implements Function {
List<_KeyValue> call(Map inputMap) {
if (inputMap == null) return null;
List<_KeyValue> result = [];
inputMap.forEach((k, v) => result.add(new _KeyValue(k, v)));
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,
Arrayify,
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,13 +13,15 @@ part 'filter.dart';
part 'json.dart';
part 'limit_to.dart';
part 'lowercase.dart';
part 'arrayify.dart';
part 'number.dart';
part 'order_by.dart';
part 'uppercase.dart';
part 'stringify.dart';

class FormatterModule extends Module {
FormatterModule() {
type(Arrayify);
type(Currency);
type(Date);
type(Filter);
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.Arrayify",
"angular.formatter_internal.Number",
"angular.formatter_internal.OrderBy",
"angular.formatter_internal.Stringify",
Expand Down
18 changes: 18 additions & 0 deletions test/formatter/arrayify_spec.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
library map_spec;

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

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

it('should treat null as noop', (Parser parse, FormatterMap formatters) {
expect(parse('null | arrayify').eval(null, formatters)).toEqual(null);
});
});
}

0 comments on commit d2780f8

Please sign in to comment.