Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(filterFilter): let expression object {$: '...'} also match primitive items #10437

Closed
wants to merge 1 commit into from

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Dec 12, 2014

Closes #10428

@googlebot
Copy link

CLAs look good, thanks!

@@ -65,6 +65,26 @@ describe('Filter: filter', function() {
});


it('should treat expression `{$: \'xyz\'}` as `\'xyz\'`', function() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions on better description most welcome :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Should use top-level $ property in expression object to match against primitive array values
  2. should match primitive array values against top-level $ property in object expression

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't accurate, because it will do that iff $ is the only (enumerable) property of the expression object (but nobody really cares I guess 😄).
UPDATED

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't accurate, because it will do that iff $ is the only (enumerable) property of the expression object

Um, why would we care? It doesn't matter if it's the only enumerable property or not, all that matters is that it's a property at all

@gkalpak gkalpak force-pushed the filterFilter-fix-10428 branch from 6705b0e to 95f3869 Compare December 12, 2014 16:53
@@ -133,6 +133,15 @@ function filterFilter() {
//jshint -W086
case 'object':
//jshint +W086
if (!matchAgainstAnyProp && (expression !== null) && ('$' in expression)) {
var keysCount = 0;
for (var key in expression) { keysCount++; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this

@gkalpak
Copy link
Member Author

gkalpak commented Dec 12, 2014

@joshkurz: The "thing" is that we need to also account for inherited (enumerable) properties (while Object.keys only considers "own" properties).

I.e. the following 2 cases should behave identically:

// 1.
var expr = {prop1: 'val1', prop2: 'val2'};

// 2.
Object.prototype.prop1 = 'val1';
var expr = {prop2: 'val2'};

@gkalpak gkalpak force-pushed the filterFilter-fix-10428 branch from 95f3869 to 34b3dfa Compare December 12, 2014 18:59
@@ -313,8 +335,6 @@ describe('Filter: filter', function() {
expect(filter(items, {}).length).toBe(1);
expect(filter(items, {})[0]).toBe(items[1]);

expect(filter(items, {$: 'll'}).length).toBe(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it was failing (it expected expression to be affected by inherited properties, but in the special case of {$: ...} it isn't).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has nothing to do with Object.prototype (or any other prototype), $ is an own property

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this test is failing, it means the implementation is broken

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is supposed to fail (in a sane world), because there is an inherited property (someProp: 'oo'). So, {$: 'll'} in this case, should be equivalent with {$: 'll', someProp: 'oo'}, which in turn (in a sane world again) means: Match any object that has a property named someProp and whose value is a superstring of oo and has any property whose value is a superstring of ll.

And because there is no such property, 0 items should be returned.

But...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But nothing --- this change, which is not supposed to impact objects at all, is breaking this test. Thus proving that it is not the right thing to do.

@gkalpak gkalpak force-pushed the filterFilter-fix-10428 branch from 34b3dfa to c7100b6 Compare December 12, 2014 20:10
@@ -163,6 +164,9 @@ function createPredicateFn(expression, comparator, matchAgainstAnyProp) {
}

predicateFn = function(item) {
if (shouldMatchPrimitives && !isObject(item)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just avoiding having to execute isObject(expression) && ('$' in expression) for every item in the array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine

@gkalpak gkalpak force-pushed the filterFilter-fix-10428 branch from c7100b6 to 724f335 Compare December 12, 2014 20:39
@gkalpak gkalpak force-pushed the filterFilter-fix-10428 branch from 724f335 to d6445b5 Compare December 12, 2014 20:43
@caitp
Copy link
Contributor

caitp commented Dec 12, 2014

lgtm I think --- @lgalfaso do you have an opinion?

@lgalfaso lgalfaso added this to the 1.3.8 milestone Dec 16, 2014
@lgalfaso lgalfaso self-assigned this Dec 16, 2014
@lgalfaso
Copy link
Contributor

I think the patch is fine, let's get this into the next 1.3.x to kill the regression

@lgalfaso
Copy link
Contributor

landed as fb2c585

@lgalfaso lgalfaso closed this Dec 16, 2014
@gkalpak gkalpak deleted the filterFilter-fix-10428 branch December 19, 2014 10:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ng-repeat filter behavior
4 participants