Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Don't throw error on no comparator. Optimisations around testModel ch…

…ecks. Values of criteriaOptions can now be false to ignore.
  • Loading branch information...
commit 89733c1af4c38ba9204b755af477761d091efd31 1 parent e21c3e8
@balupton balupton authored
Showing with 60 additions and 35 deletions.
  1. +42 −23 out/lib/query-engine.js
  2. +18 −12 src/documents/lib/query-engine.js.coffee
View
65 out/lib/query-engine.js
@@ -185,7 +185,7 @@
generateFunction = function(comparator) {
if (!comparator) {
- throw new Error('Cannot sort without a comparator');
+ return null;
} else if (util.isFunction(comparator)) {
return comparator;
} else if (util.isArray(comparator)) {
@@ -951,17 +951,32 @@
Criteria.prototype.testModel = function(model, criteriaOptions) {
var passed;
- passed = this.testQueries(model, criteriaOptions != null ? criteriaOptions.queries : void 0) && this.testFilters(model, criteriaOptions != null ? criteriaOptions.filters : void 0) && this.testPills(model, criteriaOptions != null ? criteriaOptions.pills : void 0);
+ if (criteriaOptions == null) {
+ criteriaOptions = {};
+ }
+ passed = this.testQueries(model, criteriaOptions.queries) && this.testFilters(model, criteriaOptions.filters) && this.testPills(model, criteriaOptions.pills);
return passed;
};
Criteria.prototype.testModels = function(models, criteriaOptions) {
- var comparator, finish, me, model, paging, pass, passed, start, _i, _len, _ref2;
+ var comparator, finish, me, model, paging, pass, passed, start, _i, _len;
+ if (criteriaOptions == null) {
+ criteriaOptions = {};
+ }
me = this;
passed = [];
- paging = (_ref2 = criteriaOptions != null ? criteriaOptions.paging : void 0) != null ? _ref2 : this.getPaging();
- comparator = (criteriaOptions != null ? criteriaOptions.comparator : void 0) != null ? util.generateComparator(criteriaOptions != null ? criteriaOptions.comparator : void 0) : this.getComparator();
+ paging = criteriaOptions.paging, comparator = criteriaOptions.comparator;
+ if (paging == null) {
+ paging = this.getPaging();
+ }
+ if (comparator != null) {
+ if (comparator) {
+ comparator = util.generateComparator(comparator);
+ }
+ } else {
+ comparator = this.getComparator();
+ }
for (_i = 0, _len = models.length; _i < _len; _i++) {
model = models[_i];
pass = me.testModel(model, criteriaOptions);
@@ -972,7 +987,7 @@
if (comparator) {
passed.sort(comparator);
}
- if (paging != null) {
+ if (paging) {
start = paging.offset || 0;
if ((paging.limit != null) && paging.limit > 0) {
start = start + paging.limit * ((paging.page || 1) - 1);
@@ -992,16 +1007,18 @@
if (queries == null) {
queries = this.getQueries();
}
- for (queryName in queries) {
- if (!__hasProp.call(queries, queryName)) continue;
- query = queries[queryName];
- if (!(query instanceof Query)) {
- query = new Query(query);
- queries[queryName] = query;
- }
- if (query.test(model) === false) {
- passed = false;
- return false;
+ if (queries) {
+ for (queryName in queries) {
+ if (!__hasProp.call(queries, queryName)) continue;
+ query = queries[queryName];
+ if (!(query instanceof Query)) {
+ query = new Query(query);
+ queries[queryName] = query;
+ }
+ if (query.test(model) === false) {
+ passed = false;
+ return false;
+ }
}
}
return passed;
@@ -1015,12 +1032,14 @@
if (filters == null) {
filters = this.getFilters();
}
- for (filterName in filters) {
- if (!__hasProp.call(filters, filterName)) continue;
- filter = filters[filterName];
- if (filter(model, cleanedSearchString) === false) {
- passed = false;
- return false;
+ if (filters) {
+ for (filterName in filters) {
+ if (!__hasProp.call(filters, filterName)) continue;
+ filter = filters[filterName];
+ if (filter(model, cleanedSearchString) === false) {
+ passed = false;
+ return false;
+ }
}
}
return passed;
@@ -1034,7 +1053,7 @@
if (pills == null) {
pills = this.getPills();
}
- if (searchString != null) {
+ if ((searchString != null) && pills) {
for (pillName in pills) {
if (!__hasProp.call(pills, pillName)) continue;
pill = pills[pillName];
View
30 src/documents/lib/query-engine.js.coffee
@@ -193,7 +193,7 @@ util =
# Creates a function for a comparator
generateFunction = (comparator) ->
unless comparator
- throw new Error('Cannot sort without a comparator')
+ return null
else if util.isFunction(comparator)
return comparator
else if util.isArray(comparator)
@@ -227,6 +227,7 @@ util =
return comparison
else
throw new Error('Unknown comparator type')
+
# Return the generated function for our comparator
return generateFunction(input)
@@ -981,26 +982,29 @@ class Criteria
# Test Model
test: (args...) -> return @testModel(args...)
- testModel: (model,criteriaOptions) ->
+ testModel: (model,criteriaOptions={}) ->
# Test
- passed = @testQueries(model,criteriaOptions?.queries) and @testFilters(model,criteriaOptions?.filters) and @testPills(model,criteriaOptions?.pills)
+ passed = @testQueries(model,criteriaOptions.queries) and @testFilters(model,criteriaOptions.filters) and @testPills(model,criteriaOptions.pills)
# Return
return passed
# Test Models
- testModels: (models,criteriaOptions) ->
+ testModels: (models,criteriaOptions={}) ->
# Prepare
me = @
passed = []
+ {paging,comparator} = criteriaOptions
# Extract
- paging = criteriaOptions?.paging ? @getPaging()
- comparator =
- if criteriaOptions?.comparator?
- util.generateComparator(criteriaOptions?.comparator)
- else
- @getComparator()
+ paging ?= @getPaging()
+
+ # Comparator
+ if comparator?
+ if comparator
+ comparator = util.generateComparator(comparator)
+ else
+ comparator = @getComparator()
# Cycle through the parent collection finding passing models
for model in models
@@ -1012,7 +1016,7 @@ class Criteria
passed.sort(comparator)
# Page our models
- if paging?
+ if paging
start = paging.offset or 0
if paging.limit? and paging.limit > 0
start = start + paging.limit * ((paging.page or 1) - 1)
@@ -1034,6 +1038,7 @@ class Criteria
queries ?= @getQueries()
# Cycle
+ if queries then \
for own queryName,query of queries
unless query instanceof Query
query = new Query(query)
@@ -1056,6 +1061,7 @@ class Criteria
filters ?= @getFilters()
# Cycle
+ if filters then \
for own filterName,filter of filters
if filter(model,cleanedSearchString) is false
passed = false
@@ -1075,7 +1081,7 @@ class Criteria
pills ?= @getPills()
# Cycle
- if searchString?
+ if searchString? and pills
for own pillName,pill of pills
unless pill instanceof Pill
pill = new Pill(query)
Please sign in to comment.
Something went wrong with that request. Please try again.