Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding dotNotation property and test #48

Merged
merged 6 commits into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/prop_tests/dotNotation_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var QUnit = require("steal-qunit");

var set = require('../set-core'),
props = require("../props");

QUnit.module("can-set props.boolean");

/*
* For the dotNotation prop, we define sets like so:
*
* For a property 'n.p', with value 'IL'
* x ∈ X | x.n.p = 'IL'
*
*/
test('dotNotation set membership', function() {
/*
* For a property 'n.p', with value 'IL'
* x ∈ X | x.n.p == 'IL'
*/
var prop = props.dotNotation('n.p'),
alg = new set.Algebra(prop),
res = alg.has({'n.p': 'IL'}, {n:{p:'IL'}});
Copy link
Contributor

Choose a reason for hiding this comment

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

Should you include a test for more than 1 level of nesting like address.state.city?

ok(res, "object with nested property is member of set using dotNotation");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was also looking for a way to test the unpredictable argument order handling. Tried an alg.has with the arguments reversed, and my comparator returns true in that case, but alg.has was still returning false. I'm guessing another 'default' prop was doing its own check and returning false.

Anyone have an idea how I could test that via an algebra operation?

Copy link
Contributor

Choose a reason for hiding this comment

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

I probably should understand this, but I don't, what causes the argument order to be unpredictable?


/*
* For a property 'n.p', with value 'IL'
* x ∉ X | x.n.p != 'IL'
*/
res = alg.has({'n.p': 'IL'}, {n:{p:'MI'}});
ok(res === false, "object with nested property not a member of set using dotNotation");
});
51 changes: 51 additions & 0 deletions src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,55 @@ props.rangeInclusive = function(startIndexProperty, endIndexProperty){
});
};

var nestedLookup = function(obj, propNameArray) {
if (obj === undefined) {
return undefined;
}

if (propNameArray.length === 1) {
return obj[propNameArray[0]];
} else {
return nestedLookup(obj[propNameArray[0]], propNameArray.slice(1));
}
};
/**
* @function can-set.props.dotNotation dotNotation
* @parent can-set.props
*
* @description Supports MongoDB-style 'dot notation' properties.
*
* @signature `set.props.dotNotation(dotProperty)`
*
* Defines a property that specifies a MongoDB-style nested property match.
* For example, a set property of "address.city" matches against the value of the nested `{address: {city}}` value.
*
* ```
* set.props.dotNotation("address.city")
* ```
*
* @param {String} dotProperty The MongoDB-style nested property name
* @return {can-set.compares} Returns a comparator used to build a set algebra
*/
props.dotNotation = function(dotProperty){
var compares = new clause.Where({});

compares[dotProperty] = function(aVal, bVal, a, b, propertyName) {
var modelVal, propertyVal;

// one of the value arguments being defined implies its the can-set definition prop, since a set member itself wont
// have obj['nested.property.name'] defined
if (aVal !== undefined) {
propertyVal = aVal;
modelVal = nestedLookup(b, propertyName.split('.'));
} else if (bVal !== undefined) {
propertyVal = bVal;
modelVal = nestedLookup(a, propertyName.split('.'));
}

return propertyVal === modelVal;
};

return compares;
};

module.exports = props;
1 change: 1 addition & 0 deletions src/props_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ require('./prop_tests/rangeInclusive_test');
require('./prop_tests/offsetLimit_test');
require('./prop_tests/boolean_test');
require('./prop_tests/enum_test');
require('./prop_tests/dotNotation_test');