forked from dart-archive/angular.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(MapFormatter): Add MapFormatter.
MapFormatter can be used with repeat decorator to display a list of key value pairs Closes dart-archive#394
- Loading branch information
Showing
5 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
})); | ||
}); | ||
} |