Skip to content

Commit

Permalink
Add options interfaces to enable sub-filter matching for nested records
Browse files Browse the repository at this point in the history
Add missing dependency to fix min/max rules
Add jasmine unit tests (from anglerfish)
  • Loading branch information
dwmcnelis committed Oct 30, 2017
1 parent f64c246 commit 46823e3
Show file tree
Hide file tree
Showing 4 changed files with 759 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
*.sublime-*
node_modules/
62 changes: 9 additions & 53 deletions index.js
Expand Up @@ -15,6 +15,7 @@
*/
var znFilterMatcher = (function() {

var BigNumber = require('bignumber.js');
var ruleFunctionMap = {
'': 'ruleEquals',
'not': 'ruleDoesNotEqual',
Expand Down Expand Up @@ -155,52 +156,6 @@ var znFilterMatcher = (function() {
}
};

/**
* Is var an object?
*
* @param x
* @returns {Boolean}
* @author David McNelis <david.mcnelis@wizehive.com>
* @since 1.1.0
*/
function isObject(x) {
return (!!x) && (x.constructor === Object);
};

/**
* Is var an empty object?
*
* @param x
* @returns {Boolean}
* @author David McNelis <david.mcnelis@wizehive.com>
* @since 1.1.0
*/
function noKeys(x) {
return isObject(x) && (Object.keys(x).length === 0);
};

/**
* Do any object keys match a pattern
*
* @param x
* @param pattern
* @returns {Boolean}
* @author David McNelis <david.mcnelis@wizehive.com>
* @since 1.1.0
*/
function anyKeysLike(x, pattern) {
if (!isObject(x)) {
return false;
}
var keys = Object.keys(x);
for (var i = 0; i < keys.length; i++) {
if (pattern.test(keys[i])) {
return true;
}
}
return false;
}

/**
* Determine whether the given record matches the given filter
*
Expand All @@ -210,15 +165,16 @@ var znFilterMatcher = (function() {
* @author Paul W. Smith <paul@wizehive.com>
* @since 0.5.75
*/
function recordMatchesFilter(record, filter) {
function recordMatchesFilter(record, filter, options) {
options = typeof options !== 'undefined' ? options : {};
var currentOperator = Object.keys(filter)[0];
if (filter[currentOperator].length === 0) {
// Empty filter / no rules - considered a "match all"
return true;
}

for (var i in filter[currentOperator]) {
var match = recordMatchesRule(record, filter[currentOperator][i]);
var match = recordMatchesRule(record, filter[currentOperator][i], options);
if (currentOperator == 'or' && match) {
return true;
}
Expand All @@ -240,24 +196,24 @@ var znFilterMatcher = (function() {
* @author Paul W. Smith <paul@wizehive.com>
* @since 0.5.75
*/
function recordMatchesRule(record, rule) {
function recordMatchesRule(record, rule, options) {
options = typeof options !== 'undefined' ? options : {};
var operators = ["and", "or"];

if (operators.indexOf(Object.keys(rule)[0]) !== -1) {
// Rule contains "and"/"or" key - nested filter
return recordMatchesFilter(record, rule);
}
if (rule.filter !== undefined) {
var subRecord = record[rule.attribute];
if (subRecord && (noKeys(subRecord) || anyKeysLike(subRecord, /^field/))) {
if (!recordMatchesFilter(subRecord, rule.filter)) {
if (options.subfiltering) {
var subRecord = record[rule.attribute];
if (!recordMatchesFilter(subRecord, rule.filter, options)) {
return false;
}
return true;
} else {
throw new Error("Subfilter matching is not supported");
}

}

if (typeof rule.value === 'string' && rule.value.split('|').indexOf('logged-in-user') !== -1) {
Expand Down
7 changes: 6 additions & 1 deletion package.json
Expand Up @@ -20,5 +20,10 @@
"bugs": {
"url": "https://github.com/ZengineHQ/zn-filter-matcher/issues"
},
"homepage": "https://github.com/ZengineHQ/zn-filter-matcher#readme"
"homepage": "https://github.com/ZengineHQ/zn-filter-matcher#readme",
"dependencies": {
"bignumber.js": "2.4.0"
},
"devDependencies": {}

}

0 comments on commit 46823e3

Please sign in to comment.