Skip to content

Commit

Permalink
feat(getpokemon): add support for getting a Pokémon with skills prepo…
Browse files Browse the repository at this point in the history
…pulated
  • Loading branch information
trezy committed Aug 30, 2021
1 parent 1a2808b commit 033b88d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/getPokemon.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Local imports
import './models/Pokemon.js'
import { getEntities } from './api/index.js'
import { getSkills } from './getSkills.js'



Expand All @@ -11,17 +12,36 @@ import { getEntities } from './api/index.js'
*
* @param {Object} [options] An object containing filtering options
* @param {string[]} [options.ids] Array of item IDs to be returned
* @param {boolean} [options.includeSkills] Flag to include the Pokémon's skills in the response
* @param {string} [options.patch] Maximum patch version to return data for
*
* @returns {Promise<Pokemon[]>} An array containing data for each item requested
*/
export async function getPokemon(options = {}) {
if ((typeof options !== 'object') || Array.isArray(options)) {
throw new TypeError('options must be an object')
}

return getEntities({
...options,
export async function getPokemon(options) {
const POKEMON = await getEntities({
...options || {},
type: 'pokemon',
})

if (typeof options?.includeSkills === 'undefined') {
return POKEMON
}

const { includeSkills } = options

if (typeof includeSkills !== 'boolean') {
throw new TypeError('includeSkills must be a boolean')
}

if (includeSkills) {
const SKILLS = await getSkills({
pokemonIDs: POKEMON.map(pokemon => pokemon.id),
})

POKEMON.forEach(pokemon => {
pokemon.skills = SKILLS[pokemon.id]
})
}

return POKEMON
}
8 changes: 8 additions & 0 deletions src/models/Pokemon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// Local imports
import './Skill.js'





/**
* Ratings for a Pokémon
* @typedef {Object} PokemonRatings
Expand Down Expand Up @@ -34,5 +41,6 @@
* @property {string} damageType
* @property {PokemonTags} tags
* @property {PokemonRatings} ratings
* @property {Skill[]} skills
* @property {PokemonStat[]} stats
*/
22 changes: 22 additions & 0 deletions test/getPokemon.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ describe('getPokemon', function () {
})
})
})

describe('options.includeSkills', () => {
it('given true, return each Pokémon with their associated skills', async () => {
const POKEMON_ID = 'crustle'
const POKEMON_DATA = {
...mockData.pokemon[POKEMON_ID],
skills: Object.values(mockData.skills).reduce((accumulator, skill) => {
if (skill.id.startsWith(POKEMON_ID)) {
accumulator.push(skill)
}
return accumulator
}, []),
}
const RESPONSE = await getPokemon({
ids: [POKEMON_ID],
includeSkills: true,
})

expect(RESPONSE).to.be.an('array')
expect(RESPONSE[0]).to.deep.equal(POKEMON_DATA)
})
})
})

describe('with invalid arguments', () => {
Expand Down

0 comments on commit 033b88d

Please sign in to comment.