diff options
| author | Adam Soltys <[email protected]> | 2012-10-23 17:29:29 +0000 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2012-10-23 17:29:29 +0000 |
| commit | 98ecfec09563811bc65c2bd86b27e77fa2823da0 (patch) | |
| tree | 7acad5c23b92305f1f70e593be39059c544865f2 /js/searchjs.js | |
| parent | c4cf80845afd2d7d6862074060c00eceea51bad8 (diff) | |
| parent | b94145ff904315c48781f958f54fae1428ef60e6 (diff) | |
| download | coinos-server-98ecfec09563811bc65c2bd86b27e77fa2823da0.tar.gz coinos-server-98ecfec09563811bc65c2bd86b27e77fa2823da0.zip | |
Merge branch 'master' of https://github.com/asoltys/bitcoin_pos
Diffstat (limited to 'js/searchjs.js')
| -rw-r--r-- | js/searchjs.js | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/js/searchjs.js b/js/searchjs.js new file mode 100644 index 00000000..d77f46e9 --- /dev/null +++ b/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)); |
