diff --git a/out/lib/query-engine.js b/out/lib/query-engine.js index 3b7f201..0968a46 100644 --- a/out/lib/query-engine.js +++ b/out/lib/query-engine.js @@ -1027,7 +1027,7 @@ } Query.prototype.test = function(model) { - var $mod, beginsWithValue, empty, endWithValue, match, matchAll, matchAny, modelId, modelValue, modelValueExists, query, queryGroup, queryType, queryValue, selectorName, selectorValue, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref; + var $beginsWith, $beginsWithValue, $endWithValue, $endsWith, $mod, $size, empty, match, matchAll, matchAny, modelId, modelValue, modelValueExists, query, queryGroup, selectorName, selectorValue, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref; matchAll = true; matchAny = false; empty = true; @@ -1096,157 +1096,153 @@ match = true; } } else if (util.isObject(selectorValue)) { - for (queryType in selectorValue) { - if (!__hasProp.call(selectorValue, queryType)) continue; - queryValue = selectorValue[queryType]; - switch (queryType) { - case '$beginsWith': - case '$startsWith': - if (queryValue && modelValueExists && util.isString(modelValue)) { - if (!util.isArray(queryValue)) { - queryValue = [queryValue]; - } - for (_k = 0, _len2 = queryValue.length; _k < _len2; _k++) { - beginsWithValue = queryValue[_k]; - if (modelValue.substr(0, beginsWithValue.length) === beginsWithValue) { - match = true; - break; - } - } - } - break; - case '$endsWith': - case '$finishesWith': - if (queryValue && modelValueExists && util.isString(modelValue)) { - if (!util.isArray(queryValue)) { - queryValue = [queryValue]; - } - for (_l = 0, _len3 = queryValue.length; _l < _len3; _l++) { - endWithValue = queryValue[_l]; - if (modelValue.substr(endWithValue.length * -1) === endWithValue) { - match = true; - break; - } - } - } - break; - case '$all': - if ((queryValue != null) && modelValueExists) { - if ((new Hash(modelValue)).hasAll(queryValue)) { - match = true; - } - } - break; - case '$in': - if ((queryValue != null) && modelValueExists) { - if ((new Hash(modelValue)).hasIn(queryValue) || (new Hash(queryValue)).hasIn(modelValue)) { - match = true; - } - } - break; - case '$nin': - if ((queryValue != null) && modelValueExists) { - if ((new Hash(modelValue)).hasIn(queryValue) === false && (new Hash(queryValue)).hasIn(modelValue) === false) { - match = true; - } - } - break; - case '$has': - if (modelValueExists) { - if ((new Hash(modelValue)).hasIn(queryValue)) { - match = true; - } - } - break; - case '$hasAll': - if (modelValueExists) { - if ((new Hash(modelValue)).hasIn(queryValue)) { - match = true; - } - } - break; - case '$size': - case '$length': - if (modelValue.length != null) { - if (modelValue.length === queryValue) { - match = true; - } - } - break; - case '$type': - if (typeof modelValue === queryValue) { - match = true; - } - break; - case '$like': - if (util.isString(modelValue) && modelValue.toLowerCase().indexOf(queryValue.toLowerCase()) !== -1) { - match = true; - } - break; - case '$likeSensitive': - if (util.isString(modelValue) && modelValue.indexOf(queryValue) !== -1) { - match = true; - } - break; - case '$exists': - if (queryValue === modelValueExists) { - match = true; - } - break; - case '$mod': - if (modelValueExists) { - $mod = queryValue; - if (!util.isArray($mod)) { - $mod = [$mod]; - } - if ($mod.length === 1) { - $mod.push(0); - } - if ((modelValue % $mod[0]) === $mod[1]) { - match = true; - } - } - break; - case '$eq': - case '$equal': - if (util.isEqual(modelValue, queryValue)) { - match = true; - } - break; - case '$ne': - if (modelValue !== queryValue) { - match = true; - } - break; - case '$lt': - if ((queryValue != null) && util.isComparable(modelValue) && modelValue < queryValue) { - match = true; - } - break; - case '$gt': - if ((queryValue != null) && util.isComparable(modelValue) && modelValue > queryValue) { - match = true; - } - break; - case '$bt': - if ((queryValue != null) && util.isComparable(modelValue) && queryValue[0] < modelValue && modelValue < queryValue[1]) { - match = true; - } - break; - case '$lte': - if ((queryValue != null) && util.isComparable(modelValue) && modelValue <= queryValue) { - match = true; - } + $beginsWith = selectorValue.$beginsWith || selectorValue.$startsWith || null; + if ($beginsWith && modelValueExists && util.isString(modelValue)) { + if (!util.isArray($beginsWith)) { + $beginsWith = [$beginsWith]; + } + for (_k = 0, _len2 = $beginsWith.length; _k < _len2; _k++) { + $beginsWithValue = $beginsWith[_k]; + if (modelValue.substr(0, $beginsWithValue.length) === $beginsWithValue) { + match = true; break; - case '$gte': - if ((queryValue != null) && util.isComparable(modelValue) && modelValue >= queryValue) { - match = true; - } + } + } + } + $endsWith = selectorValue.$endsWith || selectorValue.$finishesWith || null; + if ($endsWith && modelValueExists && util.isString(modelValue)) { + if (!util.isArray($endsWith)) { + $endsWith = [$endsWith]; + } + for (_l = 0, _len3 = $endsWith.length; _l < _len3; _l++) { + $endWithValue = $endsWith[_l]; + if (modelValue.substr($endWithValue.length * -1) === $endWithValue) { + match = true; break; - case '$bte': - if ((queryValue != null) && util.isComparable(modelValue) && queryValue[0] <= modelValue && modelValue <= queryValue[1]) { - match = true; - } + } + } + } + if (selectorValue.$all != null) { + if (modelValueExists) { + if ((new Hash(modelValue)).hasAll(selectorValue.$all)) { + match = true; + } + } + } + if (selectorValue.$in != null) { + if (modelValueExists) { + if ((new Hash(modelValue)).hasIn(selectorValue.$in)) { + match = true; + } else if ((new Hash(selectorValue.$in)).hasIn(modelValue)) { + match = true; + } + } + } + if (selectorValue.$has != null) { + if (modelValueExists) { + if ((new Hash(modelValue)).hasIn(selectorValue.$has)) { + match = true; + } + } + } + if (selectorValue.$hasAll != null) { + if (modelValueExists) { + if ((new Hash(modelValue)).hasIn(selectorValue.$hasAll)) { + match = true; + } + } + } + if (selectorValue.$nin != null) { + if (modelValueExists) { + if ((new Hash(modelValue)).hasIn(selectorValue.$nin) === false && (new Hash(selectorValue.$nin)).hasIn(selectorValue) === false) { + match = true; + } + } + } + $size = selectorValue.$size || selectorValue.$length; + if ($size != null) { + if ((modelValue.length != null) && modelValue.length === $size) { + match = true; + } + } + if (selectorValue.$type) { + if (typeof modelValue === selectorValue.$type) { + match = true; + } + } + if (selectorValue.$like != null) { + if (util.isString(modelValue) && modelValue.toLowerCase().indexOf(selectorValue.$like.toLowerCase()) !== -1) { + match = true; + } + } + if (selectorValue.$likeSensitive != null) { + if (util.isString(modelValue) && modelValue.indexOf(selectorValue.$likeSensitive) !== -1) { + match = true; + } + } + if (selectorValue.$exists != null) { + if (selectorValue.$exists) { + if (modelValueExists === true) { + match = true; + } + } else { + if (modelValueExists === false) { + match = true; + } + } + } + if (selectorValue.$mod != null) { + if (modelValueExists) { + $mod = selectorValue.$mod; + if (!util.isArray($mod)) { + $mod = [$mod]; + } + if ($mod.length === 1) { + $mod.push(0); + } + if ((modelValue % $mod[0]) === $mod[1]) { + match = true; + } + } + } + if (util.isDefined(selectorValue.$eq)) { + if (util.isEqual(modelValue, selectorValue.$eq)) { + match = true; + } + } + if (util.isDefined(selectorValue.$ne)) { + if (modelValue !== selectorValue.$ne) { + match = true; + } + } + if (selectorValue.$lt != null) { + if (util.isComparable(modelValue) && modelValue < selectorValue.$lt) { + match = true; + } + } + if (selectorValue.$gt != null) { + if (util.isComparable(modelValue) && modelValue > selectorValue.$gt) { + match = true; + } + } + if (selectorValue.$bt != null) { + if (util.isComparable(modelValue) && selectorValue.$bt[0] < modelValue && modelValue < selectorValue.$bt[1]) { + match = true; + } + } + if (selectorValue.$lte != null) { + if (util.isComparable(modelValue) && modelValue <= selectorValue.$lte) { + match = true; + } + } + if (selectorValue.$gte != null) { + if (util.isComparable(modelValue) && modelValue >= selectorValue.$gte) { + match = true; + } + } + if (selectorValue.$bte != null) { + if (util.isComparable(modelValue) && selectorValue.$bte[0] <= modelValue && modelValue <= selectorValue.$bte[1]) { + match = true; } } } diff --git a/src/lib/query-engine.coffee b/src/lib/query-engine.coffee index 32c7652..f4c371d 100644 --- a/src/lib/query-engine.coffee +++ b/src/lib/query-engine.coffee @@ -1185,141 +1185,142 @@ class Query # Conditional Operators else if util.isObject(selectorValue) - # Doing this tests var a for then switch is super fast verus just a switch - # not sure why this is, but woohoo - for own queryType,queryValue of selectorValue - switch queryType - # The $beginsWith operator checks if the value begins with a particular value or values if an array was passed - when '$beginsWith', '$startsWith' - if queryValue and modelValueExists and util.isString(modelValue) - queryValue = [queryValue] unless util.isArray(queryValue) - for beginsWithValue in queryValue - if modelValue.substr(0,beginsWithValue.length) is beginsWithValue - match = true - break - - # The $endsWith operator checks if the value ends with a particular value or values if an array was passed - when '$endsWith', '$finishesWith' - if queryValue and modelValueExists and util.isString(modelValue) - queryValue = [queryValue] unless util.isArray(queryValue) - for endWithValue in queryValue - if modelValue.substr(endWithValue.length*-1) is endWithValue - match = true - break - - # The $all operator is similar to $in, but instead of matching any value in the specified array all values in the array must be matched. - when '$all' - if queryValue? and modelValueExists - if (new Hash modelValue).hasAll(queryValue) - match = true - - # The $in operator is analogous to the SQL IN modifier, allowing you to specify an array of possible matches. - # The target field's value can also be an array; if so then the document matches if any of the elements of the array's value matches any of the $in field's values - when '$in' - if queryValue? and modelValueExists - if (new Hash modelValue).hasIn(queryValue) or (new Hash queryValue).hasIn(modelValue) - match = true - - # The $nin operator is similar to $in except that it selects objects for which the specified field does not have any value in the specified array. - when '$nin' - if queryValue? and modelValueExists - if (new Hash modelValue).hasIn(queryValue) is false and (new Hash queryValue).hasIn(modelValue) is false - match = true - - # Query-Engine Specific - # The $has operator checks if any of the selectorValue values exist within our model's value - when '$has' - if modelValueExists - if (new Hash modelValue).hasIn(queryValue) - match = true - - # Query-Engine Specific - # The $hasAll operator checks if all of the selectorValue values exist within our model's value - when '$hasAll' - if modelValueExists - if (new Hash modelValue).hasIn(queryValue) - match = true - - # The $size operator matches any array with the specified number of elements. The following example would match the object {a:["foo"]}, since that array has just one element: - when '$size', '$length' - if modelValue.length? - if modelValue.length is queryValue - match = true - - # The $type operator matches values based on their BSON type. - when '$type' - if typeof modelValue is queryValue - match = true - - # Query-Engine Specific - # The $like operator checks if selectorValue string exists within the modelValue string (case insensitive) - when '$like' - if util.isString(modelValue) and modelValue.toLowerCase().indexOf(queryValue.toLowerCase()) isnt -1 - match = true - - # Query-Engine Specific - # The $likeSensitive operator checks if selectorValue string exists within the modelValue string (case sensitive) - when '$likeSensitive' - if util.isString(modelValue) and modelValue.indexOf(queryValue) isnt -1 - match = true - - # Check for existence (or lack thereof) of a field. - when '$exists' - if queryValue is modelValueExists - match = true - - # The $mod operator allows you to do fast modulo queries to replace a common case for where clauses. - when '$mod' - if modelValueExists - $mod = queryValue - $mod = [$mod] unless util.isArray($mod) - $mod.push(0) if $mod.length is 1 - if (modelValue % $mod[0]) is $mod[1] - match = true - - # Query-Engine Specific - # Use $eq for deep equals - when '$eq', '$equal' - if util.isEqual(modelValue,queryValue) - match = true - - # Use $ne for "not equals". - when '$ne' - if modelValue isnt queryValue - match = true - - # less than - when '$lt' - if queryValue? and util.isComparable(modelValue) and modelValue < queryValue - match = true - - # greater than - when '$gt' - if queryValue? and util.isComparable(modelValue) and modelValue > queryValue - match = true - - # Query-Engine Specific - # between - when '$bt' - if queryValue? and util.isComparable(modelValue) and queryValue[0] < modelValue and modelValue < queryValue[1] - match = true - - # less than or equal to - when '$lte' - if queryValue? and util.isComparable(modelValue) and modelValue <= queryValue - match = true - - # greater than or equal to - when '$gte' - if queryValue? and util.isComparable(modelValue) and modelValue >= queryValue - match = true - - # Query-Engine Specific - # between or equal to - when '$bte' - if queryValue? and util.isComparable(modelValue) and queryValue[0] <= modelValue and modelValue <= queryValue[1] - match = true + # The $beginsWith operator checks if the value begins with a particular value or values if an array was passed + $beginsWith = selectorValue.$beginsWith or selectorValue.$startsWith or null + if $beginsWith and modelValueExists and util.isString(modelValue) + $beginsWith = [$beginsWith] unless util.isArray($beginsWith) + for $beginsWithValue in $beginsWith + if modelValue.substr(0,$beginsWithValue.length) is $beginsWithValue + match = true + break + + # The $endsWith operator checks if the value ends with a particular value or values if an array was passed + $endsWith = selectorValue.$endsWith or selectorValue.$finishesWith or null + if $endsWith and modelValueExists and util.isString(modelValue) + $endsWith = [$endsWith] unless util.isArray($endsWith) + for $endWithValue in $endsWith + if modelValue.substr($endWithValue.length*-1) is $endWithValue + match = true + break + + # The $all operator is similar to $in, but instead of matching any value in the specified array all values in the array must be matched. + if selectorValue.$all? + if modelValueExists + if (new Hash modelValue).hasAll(selectorValue.$all) + match = true + + # The $in operator is analogous to the SQL IN modifier, allowing you to specify an array of possible matches. + # The target field's value can also be an array; if so then the document matches if any of the elements of the array's value matches any of the $in field's values + if selectorValue.$in? + if modelValueExists + if (new Hash modelValue).hasIn(selectorValue.$in) + match = true + else if (new Hash selectorValue.$in).hasIn(modelValue) + match = true + + # Query-Engine Specific + # The $has operator checks if any of the selectorValue values exist within our model's value + if selectorValue.$has? + if modelValueExists + if (new Hash modelValue).hasIn(selectorValue.$has) + match = true + + # Query-Engine Specific + # The $hasAll operator checks if all of the selectorValue values exist within our model's value + if selectorValue.$hasAll? + if modelValueExists + if (new Hash modelValue).hasIn(selectorValue.$hasAll) + match = true + + # The $nin operator is similar to $in except that it selects objects for which the specified field does not have any value in the specified array. + if selectorValue.$nin? + if modelValueExists + if (new Hash modelValue).hasIn(selectorValue.$nin) is false and (new Hash selectorValue.$nin).hasIn(selectorValue) is false + match = true + + # The $size operator matches any array with the specified number of elements. The following example would match the object {a:["foo"]}, since that array has just one element: + $size = selectorValue.$size or selectorValue.$length + if $size? + if modelValue.length? and modelValue.length is $size + match = true + + # The $type operator matches values based on their BSON type. + if selectorValue.$type + if typeof modelValue is selectorValue.$type + match = true + + # Query-Engine Specific + # The $like operator checks if selectorValue string exists within the modelValue string (case insensitive) + if selectorValue.$like? + if util.isString(modelValue) and modelValue.toLowerCase().indexOf(selectorValue.$like.toLowerCase()) isnt -1 + match = true + + # Query-Engine Specific + # The $likeSensitive operator checks if selectorValue string exists within the modelValue string (case sensitive) + if selectorValue.$likeSensitive? + if util.isString(modelValue) and modelValue.indexOf(selectorValue.$likeSensitive) isnt -1 + match = true + + # Check for existence (or lack thereof) of a field. + if selectorValue.$exists? + if selectorValue.$exists + if modelValueExists is true + match = true + else + if modelValueExists is false + match = true + + # The $mod operator allows you to do fast modulo queries to replace a common case for where clauses. + if selectorValue.$mod? + if modelValueExists + $mod = selectorValue.$mod + $mod = [$mod] unless util.isArray($mod) + $mod.push(0) if $mod.length is 1 + if (modelValue % $mod[0]) is $mod[1] + match = true + + # Query-Engine Specific + # Use $eq for deep equals + if util.isDefined(selectorValue.$eq) + if util.isEqual(modelValue,selectorValue.$eq) + match = true + # Use $ne for "not equals". + if util.isDefined(selectorValue.$ne) + if modelValue isnt selectorValue.$ne + match = true + + # less than + if selectorValue.$lt? + if util.isComparable(modelValue) and modelValue < selectorValue.$lt + match = true + + # greater than + if selectorValue.$gt? + if util.isComparable(modelValue) and modelValue > selectorValue.$gt + match = true + + # Query-Engine Specific + # between + if selectorValue.$bt? + if util.isComparable(modelValue) and selectorValue.$bt[0] < modelValue and modelValue < selectorValue.$bt[1] + match = true + + # less than or equal to + if selectorValue.$lte? + if util.isComparable(modelValue) and modelValue <= selectorValue.$lte + match = true + + # greater than or equal to + if selectorValue.$gte? + if util.isComparable(modelValue) and modelValue >= selectorValue.$gte + match = true + + # Query-Engine Specific + # between or equal to + if selectorValue.$bte? + if util.isComparable(modelValue) and selectorValue.$bte[0] <= modelValue and modelValue <= selectorValue.$bte[1] + match = true # Matched if match @@ -1335,7 +1336,6 @@ class Query return matchAll - # ------------------------------------- # Exports