-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (53 loc) · 1.96 KB
/
index.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
'use strict'
const calculateCO2 = require('./lib/co2')
const calculateCalories = require('./lib/calories')
const calculatePrice = require('./lib/price')
const calculateRisk = require('./lib/risk')
const publicTransport = require('./lib/api/public-transport')
const car = require('./lib/api/car')
const bike = require('./lib/api/bike')
const isValidLocation = (location) => {
return location
&& 'object' === typeof location
&& location.coordinates
&& 'number' === typeof location.coordinates.latitude
&& 'number' === typeof location.coordinates.longitude
}
const isValidTime = (time) => time >= Date.now()
const compute = (journey) => {
// if (!Array.isArray(journeys)) throw new Error('journeys must be an array.')
// if (!journeys.length === 0) throw new Error('must be one or more journey.')
// for (let i = 0; i < journeys.length; i++) {
if (!journey) throw new Error(`journey is not an object.`)
if (!isValidLocation(journey.origin)) {
throw new Error(`journey origin is invalid.`)
}
if (!isValidLocation(journey.destination)) {
throw new Error(`journey destination is invalid.`)
}
if (!journey.arrival && !journey.departure) {
throw new Error(`journey must have arrival or departure.`)
}
if (journey.arrival && !isValidTime(journey.arrival)) {
throw new Error(`journey arrival is invalid.`)
}
if (journey.departure && !isValidTime(journey.departure)) {
throw new Error(`journey departure is invalid.`)
}
const {origin, destination, departure, arrival} = journey
const calculate = (data) => ({
co2: calculateCO2(data),
calories: calculateCalories(data),
price: calculatePrice(data),
risk: calculateRisk(data)
})
return Promise.all([
publicTransport(origin, destination, departure, arrival).then(calculate),
bike(origin, destination, departure, arrival).then(calculate),
car(origin, destination, departure, arrival).then(calculate)
])
.then(([publicTransport, bike, car]) => {
return {publicTransport, bike, car}
})
}
module.exports = compute