Skip to content

Commit

Permalink
feat(creature): add asMapping method
Browse files Browse the repository at this point in the history
  • Loading branch information
ReidWeb committed Jan 2, 2022
1 parent 8f23137 commit d5d8163
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions docs/api/classes/client_LodestoneClient.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Client for interfacing with the Final Fantasy XIV Lodestone.

#### Defined in

[client/LodestoneClient.ts:56](https://github.com/XIVStats/lodestone/blob/ad3cb5c/src/client/LodestoneClient.ts#L56)
[client/LodestoneClient.ts:56](https://github.com/XIVStats/lodestone/blob/8f23137/src/client/LodestoneClient.ts#L56)

## Properties

Expand All @@ -44,4 +44,4 @@ An instance will be generated by default, but as a consumer you can provide your

#### Defined in

[client/LodestoneClient.ts:50](https://github.com/XIVStats/lodestone/blob/ad3cb5c/src/client/LodestoneClient.ts#L50)
[client/LodestoneClient.ts:50](https://github.com/XIVStats/lodestone/blob/8f23137/src/client/LodestoneClient.ts#L50)
26 changes: 18 additions & 8 deletions src/entity/Creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,26 @@ import IItem from '../interface/IItem'
import CreatureType from './CreatureType'

export default class Creature {
constructor(public item: IItem, public creatureType?: CreatureType, public creatureName?: string) {}
constructor(public toolTipId: string, public item: IItem, public type?: CreatureType, public name?: string) {}

static fromToolTip(data: string, cheerio: CheerioAPI, itemIdOnly?: string): Creature {
public asMapping(): { toolTipId: string; itemId: string } {
return {
toolTipId: this.toolTipId,
itemId: this.item.id,
}
}

static fromToolTip(toolTipId: string, data: string, cheerio: CheerioAPI, itemIdOnly?: boolean): Creature {
const $ = cheerio.load(data)
const typeAsString = $('header')[0].attribs.class.replace('__header', '').toUpperCase()
const type = typeAsString as CreatureType
const creatureName = !itemIdOnly ? $('h4').text() : undefined
const item = $('a')[0]
const itemName = !itemIdOnly ? item.attribs['data-tooltip'] : undefined
const itemId = item.attribs.href.replace('/lodestone/playguide/db/item/', '').replace('/', '')
return new Creature({ name: itemName, id: itemId }, type, creatureName)
return new Creature(
toolTipId,
{
name: !itemIdOnly ? item.attribs['data-tooltip'] : undefined,
id: item.attribs.href.replace('/lodestone/playguide/db/item/', '').replace('/', ''),
},
$('header')[0].attribs.class.replace('__header', '').toUpperCase() as CreatureType,
!itemIdOnly ? $('h4').text() : undefined
)
}
}
16 changes: 10 additions & 6 deletions src/entity/__tests__/Creature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ import CreatureType from '../CreatureType'

describe('Creature', () => {
describe('given a mount tooltip is being loaded', () => {
const expectedMountOne: Creature = {
creatureName: 'Company Chocobo',
creatureType: CreatureType.Mount,
item: {
const expectedMountOne: Creature = new Creature(
'9045c5c5d5d181ee495f0e76af07d6d93c9f0f13',
{
id: '85f78cb2a87',
name: 'Chocobo Whistle',
},
}
CreatureType.Mount,
'Company Chocobo'
)
// /lodestone/character/11886902/mount/tooltip/9045c5c5d5d181ee495f0e76af07d6d93c9f0f13
describe.each([['9045c5c5d5d181ee495f0e76af07d6d93c9f0f13', "P'tajha Rihll", 'Company Chocobo', expectedMountOne]])(
'for tooltip %s - relating to %s - Mount: %s',
Expand All @@ -51,7 +52,7 @@ describe('Creature', () => {
readFile(join(__dirname, 'resources', 'mount', 'tooltip', `${toolTipId}.html`), 'utf8', (err, data) => {
jest.setTimeout(10000)
const testString = Buffer.from(data)
resultantCreature = Creature.fromToolTip(testString.toString(), Cheerio)
resultantCreature = Creature.fromToolTip(toolTipId, testString.toString(), Cheerio)
done()
})
})
Expand All @@ -66,11 +67,14 @@ describe('Creature', () => {
// @ts-ignore
it.each(Object.entries(value))("with key %s equal to '%s'", (lowerKey, lowerValue) => {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
expect(resultantCreature[key][lowerKey]).toEqual(lowerValue)
})
})
}
}
)
})

// TODO: Add minion tooltip tests
})

0 comments on commit d5d8163

Please sign in to comment.