This repository has been archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(formatter): Add arrayify formatter.
MapFormatter can be used with map and ng-repeat to display a list of key value pairs Closes #394 Closes #931
- Loading branch information
Showing
5 changed files
with
49 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; | ||
|
||
/** | ||
* 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); | ||
} |
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,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); | ||
}); | ||
}); | ||
} |