-
Notifications
You must be signed in to change notification settings - Fork 0
/
bandingCodes.js
110 lines (100 loc) · 2.56 KB
/
bandingCodes.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
const codes = require('./data/ibpAlphaCodes2021.json')
// NB: When I converted this, I replaced true_alpha with alpha
// {
// "non_species": "",
// "alpha": "HITI",
// "conflict": "",
// "common_name": "Highland Tinamou",
// "scientific_name": "Nothocercus bonapartei",
// "alpha_6": "NOTBON",
// "conflict_6": ""
// },
codes.push({
"alpha": "Mandarin Duck",
"common_name": "Mandarin Duck",
"scientific_name": "Aix galericulata",
})
codes.push({
"alpha": "Budgerigar",
"common_name": "Budgerigar",
"scientific_name": "Melopsittacus undulatus",
})
codes.push({
"alpha": "Emu",
"common_name": "Emu",
"scientific_name": "Dromaius novaehollandiae",
})
codes.push({
"alpha": "Golden Pheasant",
"common_name": "Golden Pheasant",
"scientific_name": "Chrysolophus pictus",
})
codes.push({
"alpha": "Bar-headed Goose",
"common_name": "Bar-headed Goose",
"scientific_name": "Anser indicus",
})
function isBandingCode (code) {
return (codes.find(x => x.alpha === code))
}
function codeToCommonName (code, log) {
const species = codes.find(x => x.alpha === code)
if (!species) {
if (log) {
console.log(`Error: Unable to find entry for ${code}, returning ${code}.`)
}
return code
}
return species.common_name
}
function codeToScientificName (code, log) {
const species = codes.find(x => x.alpha === code)
if (!species) {
if (log) {
console.log(`Error: Unable to find entry for ${code}, returning ${code}.`)
}
return code
}
return species.scientific_name
}
function commonNameToCode (commonName, log) {
const species = codes.find(x => {
return x.common_name.replace(/'/g, '') === commonName.replace(/'/g, '')
})
if (!species) {
if (log) {
console.log(`Error: Unable to find entry for ${commonName}, returning ${commonName}.`)
}
return commonName
}
return species.alpha
}
function speciesNameToCode (latin, log) {
const species = codes.find(x => x.scientific_name === latin)
if (!species) {
if (log) {
console.log(`Error: Unable to find entry for ${latin}, returning ${latin}.`)
}
return latin
}
return species.alpha
}
function unfurlObjToSpecies (obj) {
const object = {}
Object.keys(obj).forEach(area => {
object[area] = obj[area].map(code => codeToCommonName(code))
})
return object
}
// TODO Add tests
// codeToCommonName('BCCH')
// commonNameToCode('Bells Vireo')
// speciesNameToCode('Bucephala clangula')
module.exports = {
codeToCommonName,
commonNameToCode,
speciesNameToCode,
unfurlObjToSpecies,
codeToScientificName,
isBandingCode
}