-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance.js
More file actions
80 lines (77 loc) · 2.34 KB
/
distance.js
File metadata and controls
80 lines (77 loc) · 2.34 KB
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
const filterTransportations = (transportations, km, filter) =>
filter === 'all'
? transportations
: transportations.filter((transportation) =>
filter === 'smart'
? //Not set
((!transportation.display.min && !transportation.display.max) ||
//Only max
(!transportation.display.min &&
transportation.display.max >= km) ||
//Only min
(!transportation.display.max &&
transportation.display.min <= km) ||
//Both min and max
(transportation.display.min <= km &&
transportation.display.max >= km)) &&
transportation.footprint[0].gco2ePerKm
: transportation.footprint[0].gco2ePerKm
)
const getFootprint = (transportations, km, ignoreRadiativeForcing) =>
transportations.map((transportation) => {
const footprintObject = transportation.footprint.find(
(footprintrow) =>
!footprintrow.validity ||
(footprintrow.validity[0] < km && footprintrow.validity[1] > km)
)
const footprint = {
usage:
footprintObject && footprintObject.gco2ePerKm
? footprintObject.gco2ePerKm * km
: null,
unit: 'gco2e',
}
if (
footprintObject &&
footprintObject.gco2ePerKmWithoutRadiativeForcing &&
ignoreRadiativeForcing
) {
footprint.usage = footprintObject.gco2ePerKmWithoutRadiativeForcing * km
}
return {
...transportation,
footprint,
}
})
const filterFields = (transportations, fields) =>
transportations.map((transportation) => {
let response = {
id: transportation.id,
name: transportation.name.fr,
footprint: transportation.footprint,
}
for (let field of fields) {
response[field] =
(transportation[field] && transportation[field].fr) ||
transportation[field]
}
return response
})
const sortTransportations = (transportations, sort) =>
sort === 'id'
? transportations
: transportations.sort((a, b) =>
sort === 'name'
? a.name.fr > b.name.fr
? 1
: -1
: a.footprint.usage === null || a.footprint.usage > b.footprint.usage
? 1
: -1
)
module.exports = {
filterTransportations,
getFootprint,
filterFields,
sortTransportations,
}