Completed
Pull Request — master (#69)
by
unknown
01:43
created

sort.js ➔ byDateAsc   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 7
c 2
b 1
f 0
nc 3
nop 2
dl 0
loc 10
rs 8.2222
1
/**
2
 * sort an array of objects by date attribute
3
 *
4
 * Example : [{date: date}, {date: date}].sort(coreUtils.sort.byDateDesc)
5
 * 
6
 * @param  {Object} a object with date attribute
7
 * @param  {Object} b object with date attribute
8
 * @return {Int}   1 | 0 | -1
9
 */
10
export function byDateDesc(a, b) {
11
  var dateA = (a.abe_meta.publish != null && a.abe_meta.publish.latest != null) ? new Date(a.abe_meta.publish.latest.date) : 0
12
  var dateB = (b.abe_meta.publish != null && b.abe_meta.publish.latest) ? new Date(b.abe_meta.publish.latest.date) : 0
13
  if(dateA < dateB) {
14
    return 1
15
  }else if(dateA > dateB) {
16
    return -1
17
  }
18
  return 0
19
}
20
21
/**
22
 * shuffle an array of objects by date attribute
23
 *
24
 * Example : var shuffledArray = coreUtils.sort.shuffle([Object}, Object])
25
 * 
26
 * @param  {Array} array
27
 * @return {Array} array
28
 */
29
export function shuffle(array) {
30
  var currentIndex = array.length, temporaryValue, randomIndex
31
32
  // While there remain elements to shuffle...
33
  while (0 !== currentIndex) {
34
35
    // Pick a remaining element...
36
    randomIndex = Math.floor(Math.random() * currentIndex)
37
    currentIndex -= 1
38
39
    // And swap it with the current element.
40
    temporaryValue = array[currentIndex]
41
    array[currentIndex] = array[randomIndex]
42
    array[randomIndex] = temporaryValue
43
  }
44
45
  return array
46
}
47
/**
48
 * sort an array of objects by date attribute
49
 *
50
 * Example : [{date: date}, {date: date}].sort(coreUtils.sort.byDateAsc)
51
 * 
52
 * @param  {Object} a object with date attribute
53
 * @param  {Object} b object with date attribute
54
 * @return {Int}   1 | 0 | -1
55
 */
56
export function byDateAsc(a, b) {
57
  var dateA = (a.abe_meta.publish != null && a.abe_meta.publish.latest != null) ? new Date(a.abe_meta.publish.latest.date) : 0
58
  var dateB = (b.abe_meta.publish != null && b.abe_meta.publish.latest) ? new Date(b.abe_meta.publish.latest.date) : 0
59
  if(dateA > dateB) {
60
    return 1
61
  }else if(dateA < dateB) {
62
    return -1
63
  }
64
  return 0
65
}
66
67
/**
68
 * This function makes sorting on an array of Json objects possible.
69
 * Pass the property to be sorted on.
70
 * Usage: myArray.sort(coreUtils.sort.predicatBy('date',-1));
71
 * @param  String prop  the json property to sort on
0 ignored issues
show
Documentation introduced by
The parameter String does not exist. Did you maybe forget to remove this comment?
Loading history...
72
 * @param  integer order order ASC if 1, DESC if -1
0 ignored issues
show
Documentation introduced by
The parameter integer does not exist. Did you maybe forget to remove this comment?
Loading history...
73
 * @return integer the ordered value
74
 */
75
export function predicatBy(prop, order){
76
  if (order !== -1) {
77
    order = 1
78
  }
79
  if(prop === 'date'){
80
    return function(a,b){
81
      a = new Date(a[prop])
82
      b = new Date(b[prop])
83
      if( a > b){
84
        return 1*order
85
      }else if( a < b ){
86
        return -1*order
87
      }
88
      return 0
89
    }
90
  }
91
92
  return function(a,b){
93
    if( a[prop] > b[prop]){
94
      return 1*order
95
    }else if( a[prop] < b[prop] ){
96
      return -1*order
97
    }
98
    return 0
99
  }
100
}