@@ -19,54 +19,23 @@ export function freqQuery(queries) {
19
19
const [ operation , data ] = query ;
20
20
21
21
const currentFreqValue = dataMap ?. [ data ] ?? __INITIAL__ ;
22
+ let newFreqKey = currentFreqValue + 1 ;
22
23
23
24
switch ( operation ) {
24
25
case __INSERT__ :
25
- {
26
- // map of values
27
- dataMap [ data ] = currentFreqValue + 1 ;
26
+ // map of values
27
+ dataMap [ data ] = currentFreqValue + 1 ;
28
28
29
- // map of frequencies
30
- const newFreqKey = currentFreqValue + 1 ;
31
- if ( freqMap ?. [ newFreqKey ] ) {
32
- freqMap [ newFreqKey ] . push ( data ) ;
33
- } else {
34
- freqMap [ newFreqKey ] = [ data ] ;
35
- }
36
-
37
- if ( freqMap ?. [ currentFreqValue ] ) {
38
- freqMap [ currentFreqValue ] = freqMap [ currentFreqValue ] . filter (
39
- ( f ) => f !== data
40
- ) ;
41
- }
42
- }
29
+ // map of frequencies
30
+ newFreqKey = currentFreqValue + 1 ;
43
31
break ;
44
32
case __DELETE__ :
45
- {
46
- // map of values
47
- dataMap [ data ] = Math . max ( 0 , currentFreqValue - 1 ) ;
48
-
49
- // map of frequencies
50
- const newFreqKey = currentFreqValue - 1 ;
51
-
52
- if ( newFreqKey > 0 ) {
53
- if ( freqMap ?. [ newFreqKey ] ) {
54
- freqMap [ newFreqKey ] . push ( data ) ;
55
- } else {
56
- freqMap [ newFreqKey ] = [ data ] ;
57
- }
58
- }
33
+ // map of values
34
+ dataMap [ data ] = Math . max ( 0 , currentFreqValue - 1 ) ;
59
35
60
- if ( freqMap ?. [ currentFreqValue ] ) {
61
- freqMap [ currentFreqValue ] = freqMap [ currentFreqValue ] . filter (
62
- ( f ) => f !== data
63
- ) ;
36
+ // map of frequencies
37
+ newFreqKey = currentFreqValue - 1 ;
64
38
65
- if ( freqMap [ currentFreqValue ] . length === 0 ) {
66
- delete freqMap ?. [ currentFreqValue ] ;
67
- }
68
- }
69
- }
70
39
break ;
71
40
case __SELECT__ : {
72
41
if ( freqMap ?. [ data ] ) {
@@ -79,6 +48,26 @@ export function freqQuery(queries) {
79
48
default :
80
49
throw new Error ( 'Invalid operation' ) ;
81
50
}
51
+
52
+ if ( operation === __INSERT__ || operation === __DELETE__ ) {
53
+ if ( newFreqKey > 0 ) {
54
+ if ( freqMap ?. [ newFreqKey ] ) {
55
+ freqMap [ newFreqKey ] . push ( data ) ;
56
+ } else {
57
+ freqMap [ newFreqKey ] = [ data ] ;
58
+ }
59
+ }
60
+
61
+ if ( freqMap ?. [ currentFreqValue ] ) {
62
+ freqMap [ currentFreqValue ] = freqMap [ currentFreqValue ] . filter (
63
+ ( f ) => f !== data
64
+ ) ;
65
+
66
+ if ( freqMap [ currentFreqValue ] . length === 0 ) {
67
+ delete freqMap ?. [ currentFreqValue ] ;
68
+ }
69
+ }
70
+ }
82
71
} ) ;
83
72
84
73
return result ;
0 commit comments