-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (41 loc) · 1.17 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
const pokemon = require('pokemon')
const data = require('./data/data')
function getResult (o, id, name, forme) {
if (forme && forme !== 'base') {
if (!o.otherForme || !o.otherForme[forme]) {
throw new Error(`No.${id} ${name} does not have '${forme}' forme.`)
}
return o.otherForme[forme]
}
if (!('base' in o)) {
throw new Error(`No.${id} ${name} does not have base forme.`)
}
return o.base
}
function getById ({ id, forme, lang = 'en' } = {}) {
let o = data(lang)[id - 1]
let name = pokemon.getName(id, lang)
return getResult(o, id, name, forme)
}
function getByName ({ name, forme, lang = 'en' } = {}) {
let id = pokemon.getId(name, lang)
let o = data(lang)[id - 1]
return getResult(o, id, name, forme)
}
function getFormes ({ id, name, lang = 'en' } = {}) {
if (!id) {
id = pokemon.getId(name, lang)
} else {
// check pokemon exists
pokemon.getName(id, lang)
}
let o = data(lang)[id - 1]
let formes = 'base' in o ? ['base'] : []
if ('otherForme' in o) {
formes = formes.concat(Object.keys(o.otherForme))
}
return formes
}
exports.getById = getById
exports.getByName = getByName
exports.getFormes = getFormes