TypeScript utils speciesToNumber示例

说明

typescript utils speciestonumber示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: TypeScript

命名空间/包名称: utils

示例#1
文件: getPokemonImage.ts 项目: EmmaRamirez/nuzlocke-generator

export function getPokemonImage({
    customImage,
    forme,
    species,
    name,
    style,
    shiny,
}: GetPokemonImage) {
    const leadingZerosNumber = (speciesToNumber(species) || 0).toString().padStart(3, '0');

    if (customImage) {
        return `url(${customImage})`;
    }
    if (
        style.spritesMode &&
        (name === 'Black' ||
            name === 'Emerald' ||
            name === 'Ruby' ||
            name === 'Sapphire' ||
            name === 'LeafGreen' ||
            name === 'FireRed' ||
            name === 'White' ||
            name === 'Black 2' ||
            name === 'White 2' ||
            name === 'X' ||
            name === 'Y' ||
            name === 'OmegaRuby' ||
            name === 'AlphaSapphire' ||
            name === 'Sun' ||
            name === 'Moon' ||
            name === 'Ultra Sun' ||
            name === 'Ultra Moon')
    ) {
        return `url(https://www.serebii.net/${getGameName(
            name,
        )}/pokemon/${leadingZerosNumber}${getForme(forme)}.png)`;
    }
    if (style.spritesMode) {
        if (!shiny) {
            return `url(https://www.serebii.net/pokearth/sprites/${getGameName(
                name as Game,
            )}/${leadingZerosNumber}.png)`;
        } else {
            return `url(https://www.serebii.net/Shiny/${capitalize(
                getGameName(name as Game),
            )}/${leadingZerosNumber}.png)`;
        }
    }
    if (style.teamImages === 'sugimori') {
        return `url(https://assets.pokemon.com/assets/cms2/img/pokedex/full/${leadingZerosNumber}${sugiFormeNotation(
            Forme[forme ? forme : 'Normal'],
        )}.png)`;
    }
    return `url(img/${(
        addForme((species || '').replace(/\s/g, '').replace(/'/g, ''), forme) || 'missingno'
    ).toLowerCase()}.jpg)`;
}

示例#2
文件: utils.test.ts 项目: EmmaRamirez/nuzlocke-generator

 it('returns a number', () => {
     expect(speciesToNumber('Bulbasaur')).toBe(1);
     expect(speciesToNumber('Zeraora')).toBe(807);
     expect(typeof speciesToNumber(choose(listOfPokemon))).toBe('number');
 });

展开阅读全文