Skip to content

Commit

Permalink
Merge pull request #8 from trabus/nested-traits
Browse files Browse the repository at this point in the history
#5 Nested traits
  • Loading branch information
trabus authored May 23, 2017
2 parents eca9074 + dec2c0a commit 206db8f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
33 changes: 26 additions & 7 deletions lib/criteria/has.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,48 @@ module.exports = function () {
getType = this.diagnostics().getType,
has = function (data_in, c_data) {
var traitVal,
traitKey,
hasProp = false;
debug('======================================== :: brie :: has');
debug("\"has\" argument(s): ", c_data);
if (c_data.hasOwnProperty('trait') && data_in.hasOwnProperty(c_data['trait'])) {
traitVal = data_in[c_data['trait']];
hasProp = true;
if (c_data.hasOwnProperty('trait')) {
traitKey = c_data['trait'];
if (data_in.hasOwnProperty(traitKey)) {
traitVal = data_in[traitKey];
hasProp = true;
} else {
// if the trait key contains '.', assume it is nested
if (traitKey.indexOf('.') > -1) {
var keys = traitKey.split('.');
var obj = data_in;
for (var i = 0, len = keys.length; i < len; i++) {
if (!obj || !obj.hasOwnProperty(keys[i])) {
hasProp = false;
} else {
obj = obj[keys[i]];
traitVal = obj;
hasProp = true;
}
}
}
}
}
debug("Evaluation data has trait (" + c_data['trait'] + ")? ", hasProp);
if (hasProp) {
debug("Trait value (if found): ", traitVal);
debug("Trait type: ", typeof traitVal);
if (c_data.hasOwnProperty('comparison') && c_data.hasOwnProperty('value')) {
var type = getType(data_in[c_data['trait']]).toLowerCase();
var type = getType(traitVal).toLowerCase();
try {
var retOption = brie.determine[type][c_data['comparison']].apply(brie, [data_in[c_data['trait']], c_data['value']]);
debug("Comparison outcome of brie.determine." + type + "." + c_data['comparison'] + "(" + [data_in[c_data['trait']], c_data['value']] + "):", retOption);
var retOption = brie.determine[type][c_data['comparison']].apply(brie, [traitVal, c_data['value']]);
debug("Comparison outcome of brie.determine." + type + "." + c_data['comparison'] + "(" + [traitVal, c_data['value']] + "):", retOption);
debug('======================================== :: end brie :: has');
return retOption;
} catch (e) {
debug('********** Exception handled in "has" method. **********');
debug('Unrecognized type or comparator:\n ', e.message);
debug("Data Trait: ", c_data['trait']);
debug("Trait Value: ", data_in[c_data['trait']]);
debug("Trait Value: ", traitVal);
debug("type: ", type);
debug("comparator: ", c_data['comparison']);
debug("Sorry. brie does not have a method called brie.determine." + type + "." + c_data['comparison'] + "()");
Expand Down
36 changes: 35 additions & 1 deletion test/evaluators/complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ module.exports = function () {
hasNumberValue: 181818,
hasObjectValue: { a: 1, b: 2 },
hasDateValue: new Date(),
hasBooleanValue: true
hasBooleanValue: true,
nested: {
one: {
a: 1,
b: 2
}
}
};
this.features = {
// combination comparisons
Expand Down Expand Up @@ -75,6 +81,28 @@ module.exports = function () {
],
"criteriaLogic": "any"
},
"canCheckNestedTraits": {
"criteria": [
{
"has": {
"trait": "nested.one.a",
"comparison": "below",
"value": 5
}
}
]
},
"canCheckBadNestedTraits": {
"criteria": [
{
"has": {
"trait": "nested.one.a.nope",
"comparison": "below",
"value": 5
}
}
]
},
// "for-ids" check
"canCheckAllowIds": {
"criteria": [
Expand Down Expand Up @@ -224,6 +252,12 @@ module.exports = function () {
it('"canCheckAllowIds" should evaluate to true', function () {
assert(this.bSetup.get("canCheckAllowIds"));
});
it('"canCheckNestedTraits" should evaluate to true', function () {
assert(this.bSetup.get("canCheckNestedTraits"));
});
it('"canCheckBadNestedTraits" bad trait keys should evaluate to false', function () {
assert(!this.bSetup.get("canCheckBadNestedTraits"));
});
it('percentScale should evaluate', function () {
assert(typeof this.bSetup.get("canCheckPercentScale") === 'boolean');
});
Expand Down

0 comments on commit 206db8f

Please sign in to comment.