-
Notifications
You must be signed in to change notification settings - Fork 10
/
bicycleTransformer.js
135 lines (125 loc) · 3.15 KB
/
bicycleTransformer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var bicycleTransformer = (function() {
"use strict";
var getBicycles = function() {
return [{
id: 1,
name: "A fast bike",
type: "Road Bike",
quantity: 10,
rentPrice: 20,
dateAdded: new Date(2015, 1, 2)
}, {
id: 2,
name: "An even faster bike",
type: "Road Bike",
quantity: 4,
rentPrice: 25,
dateAdded: new Date(2015, 2, 25)
}, {
id: 3,
name: "A springy bike",
type: "Mountain Bike",
quantity: 20,
rentPrice: 18,
dateAdded: new Date(2014, 10, 1)
}, {
id: 4,
name: "A springier bike",
type: "Mountain Bike",
quantity: 10,
rentPrice: 22,
dateAdded: new Date(2014, 4, 1)
}, {
id: 5,
name: "An all-terain bike",
type: "Mountain Bike",
quantity: 5,
rentPrice: 27,
dateAdded: new Date(2014, 8, 14)
}, {
id: 6,
name: "A classy bike",
type: "Urban Bike",
quantity: 30,
rentPrice: 15,
dateAdded: new Date(2014, 6, 27)
}, {
id: 7,
name: "A modern bike",
type: "Urban Bike",
quantity: 20,
rentPrice: 17,
dateAdded: new Date(2015, 1, 19)
}, {
id: 8,
name: "A commuter bike",
type: "Urban Bike",
quantity: 12,
rentPrice: 14,
dateAdded: new Date(2014, 8, 2)
}, {
id: 9,
name: "A blue bike",
type: "Children Bike",
quantity: 25,
rentPrice: 10,
dateAdded: new Date(2014, 10, 20)
}, {
id: 10,
name: "A pink bike",
type: "Children Bike",
quantity: 25,
rentPrice: 10,
dateAdded: new Date(2015, 2, 5)
}, {
id: 11,
name: "A noisy bike",
type: "Children Bike",
quantity: 3,
rentPrice: 12,
dateAdded: new Date(2014, 8, 23)
}, {
id: 12,
name: "A clown bike",
type: "Children Bike",
quantity: 2,
rentPrice: 12,
dateAdded: new Date(2014, 11, 1)
}];
};
var filterBicyclesByType = function(bicycles, type) {
if (!type) {
return bicycles;
}
return _.where(bicycles, {
type: type
});
};
return {
getBicyclesSortedByRentPrice: function(type) {
var filteredBicycles = filterBicyclesByType(getBicycles(), type);
var sortedBicycles = _.sortBy(filteredBicycles, 'rentPrice');
return _.map(sortedBicycles, function(bicycle) {
return bicycle.name + " - " + bicycle.type + " : " + bicycle.rentPrice;
});
},
getBicyclesGroupedByYear: function(type) {
var filteredBicycles = filterBicyclesByType(getBicycles(), type);
return _.groupBy(filteredBicycles, function(bicycle) {
return bicycle.dateAdded.getFullYear();
});
},
getBicyclesGroupedByPrice: function(type) {
var filteredBicycles = filterBicyclesByType(getBicycles(), type);
return _.groupBy(filteredBicycles, 'rentPrice');
},
getBicyclesIndexedById: function() {
var bicycles = getBicycles();
return _.indexBy(bicycles, 'id');
},
getBicyclesCountByType: function() {
var bicycles = getBicycles();
return _.countBy(bicycles, 'type');
}
};
}());