Completed
Push — master ( 39b560...f48291 )
by greg
01:42
created

sort.js ➔ predicatBy   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
nc 4
dl 0
loc 26
rs 8.8571
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A sort.js ➔ ... ➔ 0 10 3
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 = new Date(a.date)
12
  var dateB = new Date(b.date)
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 = new Date(a.date)
58
  var dateB = new Date(b.date)
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
}