-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gethelditems): add
getHeldItems
method
- Loading branch information
Showing
11 changed files
with
202 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
module.exports = { | ||
parallel: true, | ||
recursive: true, | ||
require: ['test/setup.js'] | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Local imports | ||
import { getDirectory } from './getDirectory.js' | ||
import { getFile } from './getFile.js' | ||
|
||
|
||
|
||
|
||
|
||
/** | ||
* Returns data for entities | ||
* | ||
* @param {Object} options An object containing filtering options | ||
* @param {string[]} [options.ids] Array of entity IDs to be returned | ||
* @param {string} [options.patch] Maximum patch version to return data for | ||
* @param {string} options.type Entity type to return | ||
* | ||
* @returns {Promise<Object[]>} An array containing data for each item requested | ||
*/ | ||
export async function getEntities(options) { | ||
const { | ||
patch, | ||
type, | ||
} = options | ||
let { ids } = options | ||
|
||
// If no ids provided, get all entities | ||
if (typeof ids === 'undefined') { | ||
ids = await getDirectory(type, patch) | ||
} | ||
|
||
// If ids isn't an array of strings, throw an error | ||
if (!Array.isArray(ids) || ids.some(item => typeof item !== 'string')) { | ||
throw new TypeError(`ids must be an array of strings`) | ||
} | ||
|
||
// Convert IDs to filenames | ||
ids = ids.map(id => { | ||
if (!id.endsWith('.json')) { | ||
return `${id}.json` | ||
} | ||
|
||
return id | ||
}) | ||
|
||
// If ids is an array of strings, get | ||
return Promise.all(ids.map(filename => { | ||
return getFile(`${type}/${filename}`) | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { getPatches } from './getPatches.js' | ||
export { getDirectory } from './getDirectory.js' | ||
export { getEntities } from './getEntities.js' | ||
export { getFile } from './getFile.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,29 @@ | ||
export async function getHeldItems() {} | ||
// Local imports | ||
import './models/HeldItem.js' | ||
import { getEntities } from './api/index.js' | ||
|
||
|
||
|
||
|
||
|
||
/** | ||
* Returns data for held items | ||
* | ||
* @param {Object} [options] An object containing filtering options | ||
* @param {string[]} [options.items] Array of item IDs to be returned | ||
* @param {string} [options.patch] Maximum patch version to return data for | ||
* | ||
* @returns {Promise<HeldItem[]>} An array containing data for each item requested | ||
*/ | ||
export async function getHeldItems(options = {}) { | ||
const { | ||
items, | ||
patch, | ||
} = options | ||
|
||
return getEntities({ | ||
ids: items, | ||
patch, | ||
type: 'held-items', | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** Held item stat | ||
* @typedef {Object} HeldItemStat | ||
* @property {string} [formula] A formula that can be evaluated to determine the value of this item's stat, usually based on the item's level | ||
* @property {'flat'|'percentage'} [type] Whether the result of the stat's formula should be interpreted as a flat value or a percentage | ||
*/ | ||
|
||
/** Held item stats | ||
* @typedef {Object} HeldItemStats | ||
* @property {HeldItemStat} [attack] Describes how this item modifies the Pokémon's attack damage. | ||
* @property {HeldItemStat} [attackSpeed] Describes how this item modifies the Pokémon's attack speed. | ||
* @property {HeldItemStat} [cooldown] Describes how this item modifies the Pokémon's attack cooldown. | ||
* @property {HeldItemStat} [critDamage] Describes how this item modifies the Pokémon's damage when it scores a critical hit. | ||
* @property {HeldItemStat} [critRate] Describes how this item modifies the Pokémon's likelihood of scoring a critical hit. | ||
* @property {HeldItemStat} [defense] Describes how this item modifies the Pokémon's defense. | ||
* @property {HeldItemStat} [hp] Describes how this item modifies the Pokémon's hit points. | ||
* @property {HeldItemStat} [hpRecovery] Describes how this item modifies the Pokémon's hit point recovery speed. | ||
* @property {HeldItemStat} [movementSpeed] Describes how this item modifies the Pokémon's movement speed. | ||
* @property {HeldItemStat} [spAttack] Describes how this item modifies the Pokémon's special attack damage. | ||
* @property {HeldItemStat} [spDefense] Describes how this item modifies the Pokémon's special defense. | ||
* @property {HeldItemStat} [uniteMoveChargeRate] Describes how this item modifies the Pokémon's Unite move charge rate. | ||
*/ | ||
|
||
/** | ||
* A held item | ||
* @typedef {Object} HeldItem | ||
* @property {string} displayName The display name of the item | ||
* @property {string} description A description of the item | ||
* @property {['attack'|'defense'|'other'|'score']} tags An array of tags that apply to the | ||
* @property {Object} special An object containing information about the passive bonus provided by the item | ||
* @property {string} special.description A template string for the description of the item's passive bonus | ||
* @property {Object} special.boons An object representing the item's passive bonus and how it changes with the item's level | ||
* @property {HeldItemStats} stats An object containing information about how the item affects the holding Pokémon's stats | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Module imports | ||
import { expect } from 'chai' | ||
|
||
|
||
|
||
|
||
|
||
// Local imports | ||
import { | ||
mockData, | ||
useMockFS, | ||
} from '../test-helpers/useMockFS.js' | ||
import { getEntities } from '../../src/api/getEntities.js' | ||
|
||
|
||
|
||
|
||
|
||
describe('getEntities', function () { | ||
useMockFS() | ||
|
||
it('is a function', () => { | ||
expect(getEntities).to.be.a('function') | ||
}) | ||
|
||
it('given an array of IDs, returns each entity in the array', async () => { | ||
const IDS = ['aeos-cookie', 'buddy-barrier'] | ||
const ENTITY_DATA = IDS.map(id => mockData.heldItems[id]) | ||
const RESPONSE = await getEntities({ | ||
ids: IDS, | ||
type: 'held-items', | ||
}) | ||
|
||
expect(RESPONSE).to.be.an('array') | ||
RESPONSE.forEach((item, index) => { | ||
expect(item).to.deep.equal(ENTITY_DATA[index]) | ||
}) | ||
}) | ||
|
||
it('given anything other than an array, throws an error', async () => { | ||
const errorStuff = [TypeError, 'First argument must be an array of strings.'] | ||
|
||
// @ts-ignore | ||
expect(getEntities(undefined)).to.eventually.throw(...errorStuff) | ||
// @ts-ignore | ||
expect(getEntities(null)).to.eventually.throw(...errorStuff) | ||
// @ts-ignore | ||
expect(getEntities(456)).to.eventually.throw(...errorStuff) | ||
// @ts-ignore | ||
expect(getEntities({})).to.eventually.throw(...errorStuff) | ||
// @ts-ignore | ||
expect(getEntities('mr-mime')).to.eventually.throw(...errorStuff) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters