diff options
| author | Adam Soltys <[email protected]> | 2012-11-06 20:59:25 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2012-11-06 22:56:57 -0800 |
| commit | 5f907fcf5b98c5be780bb0edfe830b96d068e585 (patch) | |
| tree | 6819e040a4bc8f41330103dd9cb54ba528c550d1 /public/js/searchjs.js | |
| parent | 08f341f692688f077e9080bded2b14fdd5979c1e (diff) | |
| download | coinos-server-5f907fcf5b98c5be780bb0edfe830b96d068e585.tar.gz coinos-server-5f907fcf5b98c5be780bb0edfe830b96d068e585.zip | |
Use node and express
Diffstat (limited to 'public/js/searchjs.js')
| -rw-r--r-- | public/js/searchjs.js | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/public/js/searchjs.js b/public/js/searchjs.js new file mode 100644 index 00000000..d77f46e9 --- /dev/null +++ b/public/js/searchjs.js @@ -0,0 +1,107 @@ +/*jslint node:true,nomen:false */ +(function(exports){ + var matchObj, matchArray, singleMatch; + + singleMatch = function(field,s) { + var oneMatch = false, t, re, i, j; + // for numbers, exact match; for strings, ignore-case match; for anything else, no match + t = typeof(field); + if (field === null) { + oneMatch = s === null; + } else if (field === undefined) { + oneMatch = false; + } else if (t === "number") { + oneMatch = field === s; + } else if (t === "string") { + re = new RegExp(s,"i"); + oneMatch = field && field.match(re) !== null; + } else if (typeof(field.length) !== "undefined") { + // array, so go through each + for (j=0;j<field.length;j++) { + oneMatch = singleMatch(field[j],s); + if (oneMatch) { + break; + } + } + } else if (typeof(field) === "object") { + oneMatch = field[s] !== undefined; + } + return(oneMatch); + }; + + matchArray = function(ary,search) { + var matched = false, i, ret = []; + if (ary && ary.length > 0) { + for (i=0;i<ary.length;i++) { + matched = matchObj(ary[i],search); + if (matched) { + ret.push(ary[i]); + } + } + } + return(ret); + }; + matchObj = function(obj,search) { + var i, j, matched, oneMatch, t, re, reMatch, negator, joinAnd, ary; + search = search || {}; + + // did we have a negator? + negator = search._not ? true : false; + // do we join via AND or OR + joinAnd = search._join && search._join === "OR" ? false : true; + // if joinAnd, then matched=true until we have a single non-match; if !joinAnd, then matched=false until we have a single match + matched = joinAnd ? true : false; + + // are we a primitive or a composite? + if (search.terms) { + for (j=0; j<search.terms.length; j++) { + oneMatch = matchObj(obj,search.terms[j]); + if (negator) { + oneMatch = !oneMatch; + } + // if AND, a single match failure makes all fail, and we break + // if OR, a single match success makes all succeed, and we break + if (joinAnd && !oneMatch) { + matched = false; + break; + } else if (!joinAnd && oneMatch) { + matched = true; + break; + } + } + } else { + // match to the search field + for(i in search) { + if (search.hasOwnProperty(i) && i.indexOf("_") !== 0) { + // match each one, if search[i] is an array - just concat to be safe + ary = [].concat(search[i]); + for (j=0;j<ary.length;j++) { + oneMatch = (ary[j] !== undefined && obj[i] !== undefined) ? oneMatch = singleMatch(obj[i],ary[j]) : false; + if (oneMatch) { + break; + } + } + // negator + if (negator) { + oneMatch = !oneMatch; + } + + // if AND, a single match failure makes all fail, and we break + // if OR, a single match success makes all succeed, and we break + if (joinAnd && !oneMatch) { + matched = false; + break; + } else if (!joinAnd && oneMatch) { + matched = true; + break; + } + } + } + } + + return(matched); + }; + + exports.matchArray = matchArray; + exports.matchObject = matchObj; +}(typeof module === "undefined" || typeof module.exports === "undefined" ? this.SEARCHJS = {} : module.exports)); |
