Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type LureIDs = StringOrNumber<
>

export interface UiconsIndex<T extends string[] = string[]> {
background?: T
device?: T
gym?: T
invasion?: T
Expand Down
17 changes: 17 additions & 0 deletions src/uicons.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const BASE_AUDIO_URL =
'https://raw.githubusercontent.com/WatWowMap/wwm-uaudio/main'

const icons = new UICONS(BASE_ICON_URL)
const backgroundIcons = new UICONS({
path: BASE_ICON_URL,
data: { background: ['0.webp', '1.webp'] },
})

describe('webp format', () => {
test('should fetch remotely', async () => {
Expand Down Expand Up @@ -75,6 +79,19 @@ describe('misc', () => {
})
})

describe('background', () => {
test('fallback icon', () => {
expect(backgroundIcons.background(999)).toBe(
`${BASE_ICON_URL}/background/0.webp`
)
})
test('specific background', () => {
expect(backgroundIcons.background(1)).toBe(
`${BASE_ICON_URL}/background/1.webp`
)
})
})

describe('nest', () => {
test('grass - string', () => {
expect(icons.nest('12')).toBe(`${BASE_ICON_URL}/nest/12.webp`)
Expand Down
23 changes: 23 additions & 0 deletions src/uicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class UICONS<Index extends UiconsIndex = UiconsIndex> {
#extensionMap: ExtensionMap
#label: string

#background: Set<Index['background'][number]>
#device: Set<Index['device'][number]>
#gym: Set<Index['gym'][number]>
#invasion: Set<Index['invasion'][number]>
Expand Down Expand Up @@ -124,6 +125,23 @@ export class UICONS<Index extends UiconsIndex = UiconsIndex> {
return [`${flag}${Number(value) || ''}`, flag, '']
}

/**
* @param backgroundId the background ID
* @returns the src of the background icon
*/
background(backgroundId?: string | number): string
background(backgroundId = 0): string {
if (!this.#isReady('background')) return ''

const baseUrl = `${this.#path}/background`

const result = `${backgroundId}.${this.#extensionMap.background}`
if (this.#background.has(result)) {
return `${baseUrl}/${result}`
}
return `${baseUrl}/0.${this.#extensionMap.background}`
}

/**
* This is used to initialize the UICONS class asynchronously by automatically fetching the index.json file
* from the remote UICONS repository provided in the constructor
Expand All @@ -144,6 +162,7 @@ export class UICONS<Index extends UiconsIndex = UiconsIndex> {
* @param data The index.json file from the UICONS repository
*/
init(data: Index) {
this.#background = new Set(data.background || [])
this.#device = new Set(data.device || [])
this.#gym = new Set(data.gym || [])
this.#invasion = new Set(data.invasion || [])
Expand Down Expand Up @@ -177,6 +196,10 @@ export class UICONS<Index extends UiconsIndex = UiconsIndex> {
this.#isReady()
const [first, second] = location.split('.', 2)
switch (first) {
case 'background':
return this.#background.has(
`${fileName}.${this.#extensionMap.background}`
)
case 'device':
return this.#device.has(`${fileName}.${this.#extensionMap.device}`)
case 'gym':
Expand Down