Kaynağa Gözat

:recycle: Refactored filtering by presence and duration

tags/0.0.4
tomit4 2 yıl önce
ebeveyn
işleme
c947ae0069
1 değiştirilmiş dosya ile 20 ekleme ve 19 silme
  1. 20
    19
      backend/lib/services/filter.js

+ 20
- 19
backend/lib/services/filter.js Dosyayı Görüntüle

@@ -29,34 +29,35 @@ module.exports = class FilterService extends Schmervice.Service {
29 29
         })
30 30
     }
31 31
 
32
-    byPresence(profileList, presence) {
33
-        const matchingProfiles = []
32
+    _filterByAspect(profileList, aspect, responseKeyId) {
33
+        const responseIndex = new Map()
34
+        // NOTE: Initializes a  hash map so that we can filter
35
+        // based off of initially nested responses
34 36
         for (const profile of profileList) {
35 37
             for (const response of profile.responses) {
36
-                if (
37
-                    response.response_key_id === 15 &&
38
-                    response.val === presence
39
-                ) {
40
-                    matchingProfiles.push(profile)
38
+                const key = `${response.response_key_id}-${response.val}`
39
+                if (!responseIndex.has(key)) {
40
+                    responseIndex.set(key, [])
41 41
                 }
42
+                responseIndex.get(key).push(profile)
42 43
             }
43 44
         }
45
+        // NOTE: Then utilizes the responseKeyId, and
46
+        // aspect(either presence or duration)
47
+        // to look up which profiles have matching responses
48
+        const key = `${responseKeyId}-${aspect}`
49
+        const matchingProfiles = responseIndex.get(key) || []
44 50
         return matchingProfiles
45 51
     }
46 52
 
53
+    byPresence(profileList, presence) {
54
+        const responseKeyId = 15
55
+        return this._filterByAspect(profileList, presence, responseKeyId)
56
+    }
57
+
47 58
     byDuration(profileList, duration) {
48
-        const matchingProfiles = []
49
-        for (const profile of profileList) {
50
-            for (const response of profile.responses) {
51
-                if (
52
-                    response.response_key_id === 14 &&
53
-                    response.val === duration
54
-                ) {
55
-                    matchingProfiles.push(profile)
56
-                }
57
-            }
58
-        }
59
-        return matchingProfiles
59
+        const responseKeyId = 14
60
+        return this._filterByAspect(profileList, duration, responseKeyId)
60 61
     }
61 62
 
62 63
     // TODO: Implement filtering by matching certification

Loading…
İptal
Kaydet