This repository was 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
[WIP] feat(filters): add filters in support of pure fields and methods, and to observe lists and maps #771
Closed
chalin
wants to merge
2
commits into
dart-archive:master
from
chalin:patch-0319-general-filter-for-pure-members
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,64 @@ | ||
part of angular.filter; | ||
|
||
/** | ||
* This filter returns its argument unchanged but, for `List` and `Map` | ||
* arguments, it causes the argument contents to be observed (as opposed to | ||
* only its identity). | ||
* | ||
* Example: | ||
* | ||
* {{ list | observe }} | ||
*/ | ||
@NgFilter(name: 'observe') | ||
class ObserveFilter implements Function { | ||
dynamic call(dynamic _) => _; | ||
} | ||
|
||
/** | ||
* This filter returns the argument's value of the named field. Use this only | ||
* when the field get operation is known to be pure (side-effect free). | ||
* | ||
* Examples: | ||
* | ||
* {{ map | field:'keys' }} | ||
* {{ map | field:'values' }} | ||
* {{ list | field:'reversed' }} | ||
*/ | ||
@NgFilter(name: 'field') | ||
class GetPureFieldFilter implements Function { | ||
dynamic call(Object o, String fieldName) => | ||
o == null ? null : | ||
reflect(o).getField(new Symbol(fieldName)).reflectee; | ||
} | ||
|
||
/** | ||
* This filter returns the result of invoking the named method on the filter | ||
* argument. Use this only when the method is known to be pure (side-effect free). | ||
* | ||
* Examples: | ||
* | ||
* <span>{{ expression | method:'toString' }}</span> | ||
* <ul><li ng-repeat="n in (names | method:'split':[','])">{{n}}</li></ul> | ||
* | ||
* The first example is useful when _expression_ yields a new identity but its | ||
* string rendering is unchanged. | ||
*/ | ||
@NgFilter(name: 'method') | ||
class ApplyPureMethodFilter implements Function { | ||
dynamic call(Object o, String methodName, [args, Map<String,dynamic> namedArgs]) { | ||
if (o == null) return null; | ||
|
||
if (args is Map) { | ||
namedArgs = args; | ||
args = const []; | ||
} else if (args == null) { | ||
args = const []; | ||
} | ||
final Map<Symbol,dynamic> _namedArgs = namedArgs == null ? | ||
const <Symbol,dynamic>{} : <Symbol,dynamic>{}; | ||
if (namedArgs != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have time to check right now but couldn't this be simpler ?
|
||
namedArgs.forEach((k,v) => _namedArgs[new Symbol(k)] = v); | ||
} | ||
return reflect(o).invoke(new Symbol(methodName), args, _namedArgs).reflectee; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
name: angular | ||
version: 0.9.9 | ||
version: 0.10.0 | ||
authors: | ||
- Misko Hevery <[email protected]> | ||
- Pavel Jbanov <[email protected]> | ||
- James deBoer <[email protected]> | ||
- Chirayu Krishnappa <[email protected]> | ||
- Matias Niemela <[email protected]> | ||
- Paul Rohde <[email protected]> | ||
- Victor Berchet <[email protected]> | ||
description: Angular for dart. | ||
homepage: https://angulardart.org | ||
environment: | ||
|
This file contains hidden or 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 hidden or 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,49 @@ | ||
library pure_spec; | ||
|
||
import '../_specs.dart'; | ||
|
||
void main() { | ||
describe('pure filters', () { | ||
beforeEach((Scope scope, Parser parse, FilterMap filters) { | ||
scope.context['string'] = 'abc'; | ||
scope.context['list'] = 'abc'.split(''); | ||
scope.context['map'] = { 'a': 1, 'b': 2, 'c': 3 }; | ||
}); | ||
|
||
// Note that the `observe` filter is tested in [scope_spec.dart]. | ||
|
||
it('should return the value of the named field', | ||
(Scope scope, Parser parse, FilterMap filters) { | ||
expect(parse("list | field:'reversed'").eval(scope.context, filters) | ||
).toEqual(['c', 'b', 'a']); | ||
expect(parse("map | field:'keys'").eval(scope.context, filters)).toEqual( | ||
['a', 'b', 'c']); | ||
expect(parse("map | field:'values'").eval(scope.context, filters) | ||
).toEqual([1, 2, 3]); | ||
}); | ||
|
||
it('should return method call result', | ||
(Scope scope, Parser parse, FilterMap filters) { | ||
expect(parse("list | method:'toString'").eval(scope.context, filters) | ||
).toEqual('[a, b, c]'); | ||
expect(parse("list | method:'join':['']").eval(scope.context, filters) | ||
).toEqual('abc'); | ||
expect(parse("string | method:'split':['']").eval(scope.context, filters) | ||
).toEqual(['a', 'b', 'c']); | ||
}); | ||
|
||
it('should return method call result using namedArgs', | ||
(Scope scope, Parser parse, FilterMap filters) { | ||
scope.context['isB'] = (s) => s == 'b'; | ||
scope.context['zero'] = () => 0; | ||
|
||
// Test for no positional args but with named args. | ||
expect(parse("list | method:'toList':{'growable':false}").eval( | ||
scope.context, filters)).toEqual(['a', 'b', 'c']); | ||
|
||
// Test for both positional and named args. | ||
expect(parse("list | method:'firstWhere':[isB]:{'orElse':zero}").eval( | ||
scope.context, filters)).toEqual('b'); | ||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mirrors can not be used in Angular. See
https://github.com/angular/angular.dart/blob/master/lib/angular_dynamic.dart
https://github.com/angular/angular.dart/blob/master/lib/angular_static.dart
and
https://github.com/angular/angular.dart/blob/master/example/pubspec.yaml#L10
Basically mirrors get replaced with generated code using the transformers during the compilation phase.