7
7
*/
8
8
9
9
module . exports = function removeUnusedArrayFilters ( update , arrayFilters ) {
10
- const updateKeys = Object . keys ( update ) . map ( key => Object . keys ( update [ key ] ) ) . reduce ( ( cur , arr ) => cur . concat ( arr ) , [ ] ) ;
11
- return arrayFilters . filter ( obj => {
12
- const firstKey = Object . keys ( obj ) [ 0 ] ;
13
- const firstDot = firstKey . indexOf ( '.' ) ;
14
- const arrayFilterKey = firstDot === - 1 ? firstKey : firstKey . slice ( 0 , firstDot ) ;
15
-
16
- return updateKeys . find ( key => key . includes ( '$[' + arrayFilterKey + ']' ) ) != null ;
10
+ const updateKeys = Object . keys ( update )
11
+ . map ( ( key ) => Object . keys ( update [ key ] ) )
12
+ . reduce ( ( cur , arr ) => cur . concat ( arr ) , [ ] ) ;
13
+ return arrayFilters . filter ( ( obj ) => {
14
+ return _checkSingleFilterKey ( obj , updateKeys ) ;
17
15
} ) ;
18
- } ;
16
+ } ;
17
+
18
+ function _checkSingleFilterKey ( arrayFilter , updateKeys ) {
19
+ const firstKey = Object . keys ( arrayFilter ) [ 0 ] ;
20
+
21
+ if ( firstKey === '$and' || firstKey === '$or' ) {
22
+ if ( ! Array . isArray ( arrayFilter [ firstKey ] ) ) {
23
+ return false ;
24
+ }
25
+ return (
26
+ arrayFilter [ firstKey ] . find ( ( filter ) =>
27
+ _checkSingleFilterKey ( filter , updateKeys )
28
+ ) != null
29
+ ) ;
30
+ }
31
+
32
+ const firstDot = firstKey . indexOf ( '.' ) ;
33
+ const arrayFilterKey =
34
+ firstDot === - 1 ? firstKey : firstKey . slice ( 0 , firstDot ) ;
35
+
36
+ return (
37
+ updateKeys . find ( ( key ) => key . includes ( '$[' + arrayFilterKey + ']' ) ) != null
38
+ ) ;
39
+ }
0 commit comments