Sean Wilkinson avatar Sean Wilkinson committed 1f40242

Removed subfunctions from the filter function

Comments (0)

Files changed (1)

couchdb-app/filters/queue.js

 //- JavaScript source code
 
 //- queue.js ~~
-//                                                      ~~ (c) SRW, 28 Oct 2011
+//
+//  This file contains a generalized solution to querying JSON documents'
+//  properties according to known values. Additionally, using the "_changes"
+//  API leaves an easy way room to add authentication on a per-request basis,
+//  if ever such measures should be needed for access control :-)
+//
+//                                                      ~~ (c) SRW, 31 Oct 2011
 
 function (doc, req) {
     'use strict';
 
-    var isTask, matchesQuery;
-
-    isTask = function (doc) {
-        return ((doc.hasOwnProperty('val'))         &&
-                (doc.val !== null)                  &&
-                (doc.val !== undefined)             &&
-                (doc.val.hasOwnProperty('f'))       &&
-                (doc.val.hasOwnProperty('x'))       &&
-                (doc.val.hasOwnProperty('y'))       &&
-                (doc.val.hasOwnProperty('status'))) ;
-    };
-
-    matchesQuery = function (doc, req) {
-        var key, q, v;
-        q = (req.hasOwnProperty('query')) ? req.query : {};
-        v = doc.val;
-        for (key in q) {
-            if (q.hasOwnProperty(key) && v.hasOwnProperty(key)) {
-             // NOTE: This ignores keys that don't exist on the document so
-             // that CouchDB's special filter keywords won't interfere. Then,
-             // you can still specify "limit=10" or "since=100", for example,
-             // to subset results without blacklisting keywords explicitly :-)
-                if (q[key] !== v[key]) {
-                    return false;
-                }
+    var flag, key, q, v;
+
+    if (doc.hasOwnProperty('val')) {
+        v = ((doc.val !== null) && (doc.val !== undefined)) ? doc.val : {};
+        flag = ((v.hasOwnProperty('f')) && (v.hasOwnProperty('x')) &&
+                (v.hasOwnProperty('y')) && (v.hasOwnProperty('status')));
+        if (flag !== true) {
+            return false;
+        }
+    } else {
+        return false;
+    }
+
+    q = (req.hasOwnProperty('query')) ? req.query : {};
+
+    for (key in q) {
+        if (q.hasOwnProperty(key) && v.hasOwnProperty(key)) {
+         // NOTE: This ignores keys that don't exist on the document so
+         // that CouchDB's special filter keywords won't interfere. Then,
+         // you can still specify "limit=10" or "since=100", for example,
+         // to subset results without blacklisting keywords explicitly :-)
+            if (q[key] !== v[key]) {
+                return false;
             }
         }
-        return true;
-    };
-
-    return isTask(doc) && matchesQuery(doc, req);
+    }
 
+    return true;
 }
 
 //- vim:set syntax=javascript:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.