-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
45 lines (35 loc) · 1.16 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
const got = require('got')
const sexagesimal = require('@mapbox/sexagesimal')
const headers = {
'user-agent': 'https://github.com/rafaelrinaldi/whereami'
}
const formatToJson = ({ latitude, longitude }) =>
JSON.stringify({
latitude,
longitude
})
const formatToSexagesimal = data => {
const latitude = sexagesimal.format(data.latitude, 'lat')
const longitude = sexagesimal.format(data.longitude, 'lon')
return `${latitude},${longitude}`
}
const formatToHuman = data => {
if (!data.country_name && !data.region_name && !data.city) { throw new Error('Unable to retrieve region') }
return [data.city, data.region_name, data.country_name]
.filter(Boolean)
.join(', ')
}
const formatOutput = (data, options) => {
if (options.raw) return JSON.stringify(data, null, 2)
const mapFormatToFormatter = {
sexagesimal: formatToSexagesimal,
json: formatToJson,
human: formatToHuman
}
return mapFormatToFormatter[options.format](data)
}
const whereami = ({ ipOrHostname = '', ...options }) =>
got(`freegeoip.app/json/${ipOrHostname}`, { headers }).then(({ body }) =>
formatOutput(JSON.parse(body), options)
)
module.exports = whereami