aboutsummaryrefslogtreecommitdiffstats
path: root/public/js/searchjs.js
blob: d77f46e94255aeb9b6cc12ef6636a356a6c1093e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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));