Renames queryOperatorArray

pull/2/head
Louis Chatriot 11 years ago
parent 5c2e44ea69
commit bbbc18137c
  1. 19
      lib/model.js

@ -584,12 +584,15 @@ comparisonFunctions.$exists = function (value, exists) {
} }
}; };
arrayComparisonFunctions.$size = function (obj, value) { // Specific to arrays
comparisonFunctions.$size = function (obj, value) {
if (!util.isArray(obj)) { return false; } if (!util.isArray(obj)) { return false; }
if (value % 1 !== 0) { throw "$size operator called without an integer"; } if (value % 1 !== 0) { throw "$size operator called without an integer"; }
return (obj.length == value); return (obj.length == value);
}; };
arrayComparisonFunctions.$size = true;
/** /**
* Match any of the subqueries * Match any of the subqueries
@ -672,22 +675,20 @@ function match (obj, query) {
/** /**
* Match an object against a specific { key: value } part of a query * Match an object against a specific { key: value } part of a query
* if the treatObjAsValue flag is set, don't try to match every part separately, but the array as a whole
*/ */
function matchQueryPart (obj, queryKey, queryValue) { function matchQueryPart (obj, queryKey, queryValue, treatObjAsValue) {
var objValue = getDotValue(obj, queryKey) var objValue = getDotValue(obj, queryKey)
, i, keys, firstChars, dollarFirstChars; , i, keys, firstChars, dollarFirstChars;
// Check if the object value is an array // Check if the object value is an array
if (util.isArray(objValue)) { if (util.isArray(objValue) && !treatObjAsValue) {
// Check if it's a query operator array // Check if we are applying a comparison function to the array itself
if (queryValue !== null && typeof queryValue === 'object' && !util.isRegExp(queryValue)) { if (queryValue !== null && typeof queryValue === 'object' && !util.isRegExp(queryValue)) {
keys = Object.keys(queryValue); keys = Object.keys(queryValue);
firstChars = _.map(keys, function (item) { return item[0]; });
dollarFirstChars = _.filter(firstChars, function (c) { return c === '$'; });
if (keys.length == 1 && dollarFirstChars.length > 0 && arrayComparisonFunctions[keys[0]]) { for (i = 0; i < keys.length; i += 1) {
if (!arrayComparisonFunctions[keys[0]](objValue, queryValue[keys[0]])) { return false; } if (arrayComparisonFunctions[keys[i]]) { return matchQueryPart(obj, queryKey, queryValue, true); }
return true;
} }
} }

Loading…
Cancel
Save