1
+ import { ORDER_OPTIONS } from '~lib/constants' ;
2
+
1
3
export function restoreOptions ( emitter ) {
2
4
function setCurrentChoice ( result ) {
3
5
const { token = '' } = result ;
@@ -13,13 +15,42 @@ export function restoreOptions(emitter) {
13
15
}
14
16
15
17
export function sortByScore ( a , b ) {
16
- const scoreA = parseInt ( a . score , 10 ) ;
17
- const scoreB = parseInt ( b . score , 10 ) ;
18
- if ( scoreA < scoreB ) return 1 ;
19
- if ( scoreA > scoreB ) return - 1 ;
18
+ const sortA = a ? parseInt ( a . score , 10 ) : 0 ;
19
+ const sortB = b ? parseInt ( b . score , 10 ) : 0 ;
20
+ if ( sortA < sortB ) return 1 ;
21
+ if ( sortA > sortB ) return - 1 ;
22
+ return 0 ;
23
+ }
24
+
25
+ export function sortByComments ( a , b ) {
26
+ const sortA = a ? parseInt ( a . comments , 10 ) : 0 ;
27
+ const sortB = b ? parseInt ( b . comments , 10 ) : 0 ;
28
+ if ( sortA < sortB ) return 1 ;
29
+ if ( sortA > sortB ) return - 1 ;
30
+ return 0 ;
31
+ }
32
+
33
+ export function sortByDate ( a , b ) {
34
+ const sortA = a ? parseInt ( a . age , 10 ) : 0 ;
35
+ const sortB = b ? parseInt ( b . age , 10 ) : 0 ;
36
+ if ( sortA < sortB ) return 1 ;
37
+ if ( sortA > sortB ) return - 1 ;
20
38
return 0 ;
21
39
}
22
40
41
+ export function getSortedResults ( order , results ) {
42
+ switch ( order ) {
43
+ case ORDER_OPTIONS [ 0 ] :
44
+ return results . sort ( sortByScore ) ;
45
+ case ORDER_OPTIONS [ 1 ] :
46
+ return results . sort ( sortByComments ) ;
47
+ case ORDER_OPTIONS [ 2 ] :
48
+ return results . sort ( sortByDate ) ;
49
+ default :
50
+ return results . sort ( sortByScore ) ;
51
+ }
52
+ }
53
+
23
54
export const flatten = list =>
24
55
list . reduce ( ( a , b ) => a . concat ( Array . isArray ( b ) ? flatten ( b ) : b ) , [ ] ) ;
25
56
0 commit comments