Cleaner matching separation

pull/2/head
Louis Chatriot 12 years ago
parent 5568b3d08e
commit 14e9484844
  1. 36
      lib/model.js
  2. 3
      test/model.test.js

@ -302,26 +302,38 @@ matcherFunctions.$eq = function (objValue, value) {
* @param {Object} query
*/
function match (obj, query) {
var match = true
, objValue, arrayMatch
, i, k;
var queryKeys = Object.keys(query)
, i
;
for (i = 0; i < queryKeys.length; i += 1) {
if (!matchQueryKey(obj, query, queryKeys[i])) { return false; }
}
return true;
};
Object.keys(query).forEach(function (k) {
objValue = getDotValue(obj, k);
/**
* Match a specific key of a given query
*/
function matchQueryKey (obj, query, queryKey) {
var objValue = getDotValue(obj, queryKey)
, queryValue = query[queryKey]
, i
;
if (util.isArray(objValue)) {
arrayMatch = false; // We only need one match in the array for a success
for (i = 0; i < objValue.length; i += 1) {
if (matcherFunctions.$eq(objValue[i], query[k])) { arrayMatch = true; }
if (matcherFunctions.$eq(objValue[i], queryValue)) { return true; }
}
match = match && arrayMatch;
return false;
} else {
if (!matcherFunctions.$eq(objValue, query[k])) { match = false; }
if (!matcherFunctions.$eq(objValue, queryValue)) { return false; }
}
});
return match;
};
return true;
}
// Interface

@ -416,6 +416,9 @@ describe('Model', function () {
model.match({ tags: ['node', 'js', 'db'] }, { tags: 'python' }).should.equal(false);
model.match({ tags: ['node', 'js', 'db'] }, { tags: 'js' }).should.equal(true);
model.match({ tags: ['node', 'js', 'db'] }, { tags: 'js', tags: 'node' }).should.equal(true);
// Mixed matching with array and non array
model.match({ tags: ['node', 'js', 'db'], nedb: true }, { tags: 'js', nedb: true }).should.equal(true);
});
});

Loading…
Cancel
Save