From bb85b1e73ac74d0f5738c2813d1f1849ae38ff42 Mon Sep 17 00:00:00 2001 From: Christopher Ramey Date: Thu, 5 Apr 2012 04:16:30 +0000 Subject: [PATCH] Added raw searching function, useful for appending multiple searches together. Clarified a few comments, changed the names of a few internal functions. --- tidx.js | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/tidx.js b/tidx.js index 3075377..ad0a444 100644 --- a/tidx.js +++ b/tidx.js @@ -62,9 +62,9 @@ var Tidx = function() }; - // Conducts a global search for a string (value) globally, - // iterating through all fields. - this.gsearch = function(value, result) + // Conducts a global index scan for a string (value), iterating + // through all fields. + this.global_scan = function(value, result) { // Refuse empty searches if(value.length === 0){ return []; } @@ -86,8 +86,8 @@ var Tidx = function() }; - // Conducts a field specific search for a string (value) - this.fsearch = function(field, value, result) + // Conducts a field specific scan for a string (value) + this.field_scan = function(field, value, result) { var f; switch(typeof field){ @@ -112,14 +112,9 @@ var Tidx = function() }; - // Multi-term searching function. This is what you should use - // to search with. If and_query is true, returned results - // must match every term. Otherwise, any ID matching any - // term is returned. - this.search = function(and_query, search) + // Raw searching function useful for chaining seaches + this.raw_search = function(search, result) { - var r = {}; - var re, tc=0; while((re = this.s_rx.exec(search)) !== null){ var field = re[2]; @@ -132,15 +127,30 @@ var Tidx = function() } } - // Global term + // Field specific scan for term if(field !== undefined && field.length !== 0){ - this.fsearch(field, value, r); + this.field_scan(field, value, result); + // Global scan for term } else { - this.gsearch(value, r); + this.global_scan(value, result); } + tc++; } + return tc; + } + + + // Multi-term searching function. This is what you should use + // to search with in most cases. If and_query is true, returned + // results must match every term. Otherwise, any ID matching any + // term is returned. + this.search = function(and_query, search) + { + var r = {}; + var tc = this.raw_search(search, r); + return this.order(r, (and_query ? tc : 0)); };