Replace if with ternary, rename to coerceArray

This commit is contained in:
NightKev 2025-04-27 15:53:38 -07:00
parent 5ae28da186
commit fd09bd746d
11 changed files with 26 additions and 29 deletions

View File

@ -17,7 +17,7 @@ import {
isNullOrUndefined, isNullOrUndefined,
BooleanHolder, BooleanHolder,
type Constructor, type Constructor,
makeArray, coerceArray,
} from "#app/utils/common"; } from "#app/utils/common";
import { deepMergeSpriteData } from "#app/utils/data"; import { deepMergeSpriteData } from "#app/utils/data";
import type { Modifier, ModifierPredicate, TurnHeldItemTransferModifier } from "./modifier/modifier"; import type { Modifier, ModifierPredicate, TurnHeldItemTransferModifier } from "./modifier/modifier";

View File

@ -1,7 +1,7 @@
import type Pokemon from "../field/pokemon"; import type Pokemon from "../field/pokemon";
import { allMoves } from "./data-lists"; import { allMoves } from "./data-lists";
import { MoveCategory } from "#enums/MoveCategory"; import { MoveCategory } from "#enums/MoveCategory";
import { makeArray, type Constructor, type nil } from "#app/utils/common"; import { coerceArray, type Constructor, type nil } from "#app/utils/common";
import { AbilityId } from "#enums/ability-id"; import { AbilityId } from "#enums/ability-id";
import { MoveId } from "#enums/move-id"; import { MoveId } from "#enums/move-id";
import { SpeciesId } from "#enums/species-id"; import { SpeciesId } from "#enums/species-id";

View File

@ -1,7 +1,7 @@
import { globalScene } from "#app/global-scene"; import { globalScene } from "#app/global-scene";
import { modifierTypes } from "../data-lists"; import { modifierTypes } from "../data-lists";
import { PokemonMove } from "../moves/pokemon-move"; import { PokemonMove } from "../moves/pokemon-move";
import { toReadableString, isNullOrUndefined, randSeedItem, randSeedInt, makeArray, randSeedIntRange } from "#app/utils/common"; import { toReadableString, isNullOrUndefined, randSeedItem, randSeedInt, coerceArray, randSeedIntRange } from "#app/utils/common";
import { pokemonEvolutions, pokemonPrevolutions } from "#app/data/balance/pokemon-evolutions"; import { pokemonEvolutions, pokemonPrevolutions } from "#app/data/balance/pokemon-evolutions";
import { getPokemonSpecies } from "#app/data/pokemon-species"; import { getPokemonSpecies } from "#app/data/pokemon-species";
import { tmSpecies } from "#app/data/balance/tms"; import { tmSpecies } from "#app/data/balance/tms";
@ -554,7 +554,7 @@ export class TrainerConfig {
this.speciesPools = evilAdminTrainerPools[poolName]; this.speciesPools = evilAdminTrainerPools[poolName];
signatureSpecies.forEach((speciesPool, s) => { signatureSpecies.forEach((speciesPool, s) => {
this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(makeArray(speciesPool))); this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(coerceArray(speciesPool)));
}); });
const nameForCall = this.name.toLowerCase().replace(/\s/g, "_"); const nameForCall = this.name.toLowerCase().replace(/\s/g, "_");
@ -617,7 +617,7 @@ export class TrainerConfig {
this.setPartyTemplates(trainerPartyTemplates.RIVAL_5); this.setPartyTemplates(trainerPartyTemplates.RIVAL_5);
} }
signatureSpecies.forEach((speciesPool, s) => { signatureSpecies.forEach((speciesPool, s) => {
this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(makeArray(speciesPool))); this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(coerceArray(speciesPool)));
}); });
if (!isNullOrUndefined(specialtyType)) { if (!isNullOrUndefined(specialtyType)) {
this.setSpeciesFilter(p => p.isOfType(specialtyType)); this.setSpeciesFilter(p => p.isOfType(specialtyType));
@ -663,7 +663,7 @@ export class TrainerConfig {
// Set up party members with their corresponding species. // Set up party members with their corresponding species.
signatureSpecies.forEach((speciesPool, s) => { signatureSpecies.forEach((speciesPool, s) => {
// Set a function to get a random party member from the species pool. // Set a function to get a random party member from the species pool.
this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(makeArray(speciesPool))); this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(coerceArray(speciesPool)));
}); });
// If specialty type is provided, set species filter and specialty type. // If specialty type is provided, set species filter and specialty type.
@ -720,7 +720,7 @@ export class TrainerConfig {
// Set up party members with their corresponding species. // Set up party members with their corresponding species.
signatureSpecies.forEach((speciesPool, s) => { signatureSpecies.forEach((speciesPool, s) => {
// Set a function to get a random party member from the species pool. // Set a function to get a random party member from the species pool.
this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(makeArray(speciesPool))); this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(coerceArray(speciesPool)));
}); });
// Set species filter and specialty type if provided, otherwise filter by base total. // Set species filter and specialty type if provided, otherwise filter by base total.

View File

@ -1,6 +1,6 @@
import { globalScene } from "#app/global-scene"; import { globalScene } from "#app/global-scene";
import Pokemon from "./pokemon"; import Pokemon from "./pokemon";
import { fixedInt, makeArray, randInt } from "#app/utils/common"; import { fixedInt, coerceArray, randInt } from "#app/utils/common";
export default class PokemonSpriteSparkleHandler { export default class PokemonSpriteSparkleHandler {
private sprites: Set<Phaser.GameObjects.Sprite>; private sprites: Set<Phaser.GameObjects.Sprite>;
@ -57,7 +57,7 @@ export default class PokemonSpriteSparkleHandler {
} }
add(sprites: Phaser.GameObjects.Sprite | Phaser.GameObjects.Sprite[]): void { add(sprites: Phaser.GameObjects.Sprite | Phaser.GameObjects.Sprite[]): void {
sprites = makeArray(sprites); sprites = coerceArray(sprites);
for (const s of sprites) { for (const s of sprites) {
if (this.sprites.has(s)) { if (this.sprites.has(s)) {
continue; continue;
@ -67,7 +67,7 @@ export default class PokemonSpriteSparkleHandler {
} }
remove(sprites: Phaser.GameObjects.Sprite | Phaser.GameObjects.Sprite[]): void { remove(sprites: Phaser.GameObjects.Sprite | Phaser.GameObjects.Sprite[]): void {
sprites = makeArray(sprites); sprites = coerceArray(sprites);
for (const s of sprites) { for (const s of sprites) {
this.sprites.delete(s); this.sprites.delete(s);
} }

View File

@ -41,7 +41,7 @@ import {
type nil, type nil,
type Constructor, type Constructor,
randSeedIntRange, randSeedIntRange,
makeArray, coerceArray,
} from "#app/utils/common"; } from "#app/utils/common";
import type { TypeDamageMultiplier } from "#app/data/type"; import type { TypeDamageMultiplier } from "#app/data/type";
import { getTypeDamageMultiplier, getTypeRgb } from "#app/data/type"; import { getTypeDamageMultiplier, getTypeRgb } from "#app/data/type";
@ -1775,7 +1775,7 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container {
let overrideArray: MoveId | Array<MoveId> = this.isPlayer() let overrideArray: MoveId | Array<MoveId> = this.isPlayer()
? Overrides.MOVESET_OVERRIDE ? Overrides.MOVESET_OVERRIDE
: Overrides.OPP_MOVESET_OVERRIDE; : Overrides.OPP_MOVESET_OVERRIDE;
overrideArray = makeArray(overrideArray); overrideArray = coerceArray(overrideArray);
if (overrideArray.length > 0) { if (overrideArray.length > 0) {
if (!this.isPlayer()) { if (!this.isPlayer()) {
this.moveset = []; this.moveset = [];

View File

@ -1,4 +1,4 @@
import { makeArray } from "#app/utils/common"; import { coerceArray } from "#app/utils/common";
let manifest: object; let manifest: object;
@ -12,7 +12,7 @@ export default class CacheBustedLoaderPlugin extends Phaser.Loader.LoaderPlugin
} }
addFile(file): void { addFile(file): void {
file = makeArray(file); file = coerceArray(file);
file.forEach(item => { file.forEach(item => {
if (manifest) { if (manifest) {

View File

@ -1,4 +1,4 @@
import { makeArray } from "#app/utils/common"; import { coerceArray } from "#app/utils/common";
export const legacyCompatibleImages: string[] = []; export const legacyCompatibleImages: string[] = [];
@ -90,7 +90,7 @@ export class SceneBase extends Phaser.Scene {
} else { } else {
folder += "/"; folder += "/";
} }
filenames = makeArray(filenames); filenames = coerceArray(filenames);
for (const f of filenames as string[]) { for (const f of filenames as string[]) {
this.load.audio(folder + key, this.getCachedUrl(`audio/${folder}${f}`)); this.load.audio(folder + key, this.getCachedUrl(`audio/${folder}${f}`));
} }

View File

@ -1,5 +1,5 @@
import { globalScene } from "#app/global-scene"; import { globalScene } from "#app/global-scene";
import { fixedInt, makeArray } from "#app/utils/common"; import { fixedInt, coerceArray } from "#app/utils/common";
export enum PokemonIconAnimMode { export enum PokemonIconAnimMode {
NONE, NONE,
@ -49,7 +49,7 @@ export default class PokemonIconAnimHandler {
} }
addOrUpdate(icons: PokemonIcon | PokemonIcon[], mode: PokemonIconAnimMode): void { addOrUpdate(icons: PokemonIcon | PokemonIcon[], mode: PokemonIconAnimMode): void {
icons = makeArray(icons); icons = coerceArray(icons);
for (const i of icons) { for (const i of icons) {
if (this.icons.has(i) && this.icons.get(i) === mode) { if (this.icons.has(i) && this.icons.get(i) === mode) {
continue; continue;
@ -64,7 +64,7 @@ export default class PokemonIconAnimHandler {
} }
remove(icons: PokemonIcon | PokemonIcon[]): void { remove(icons: PokemonIcon | PokemonIcon[]): void {
icons = makeArray(icons); icons = coerceArray(icons);
for (const i of icons) { for (const i of icons) {
if (this.toggled) { if (this.toggled) {
const icon = this.icons.get(i); const icon = this.icons.get(i);

View File

@ -612,9 +612,6 @@ export function getShinyDescriptor(variant: Variant): string {
} }
} }
export function makeArray<T>(input: T | T[]): T[] { export function coerceArray<T>(input: T | T[]): T[] {
if (!Array.isArray(input)) { return Array.isArray(input) ? input : [input];
return [input];
}
return input;
} }

View File

@ -12,7 +12,7 @@ import { UiMode } from "#enums/ui-mode";
import { getMovePosition } from "#test/testUtils/gameManagerUtils"; import { getMovePosition } from "#test/testUtils/gameManagerUtils";
import { GameManagerHelper } from "#test/testUtils/helpers/gameManagerHelper"; import { GameManagerHelper } from "#test/testUtils/helpers/gameManagerHelper";
import { vi } from "vitest"; import { vi } from "vitest";
import { makeArray } from "#app/utils/common"; import { coerceArray } from "#app/utils/common";
/** /**
* Helper to handle a Pokemon's move * Helper to handle a Pokemon's move
@ -158,7 +158,7 @@ export class MoveHelper extends GameManagerHelper {
* @param moveset - The {@linkcode MoveId} (single or array) to change the Pokemon's moveset to. * @param moveset - The {@linkcode MoveId} (single or array) to change the Pokemon's moveset to.
*/ */
public changeMoveset(pokemon: Pokemon, moveset: MoveId | MoveId[]): void { public changeMoveset(pokemon: Pokemon, moveset: MoveId | MoveId[]): void {
moveset = makeArray(moveset); moveset = coerceArray(moveset);
pokemon.moveset = []; pokemon.moveset = [];
moveset.forEach(move => { moveset.forEach(move => {
pokemon.moveset.push(new PokemonMove(move)); pokemon.moveset.push(new PokemonMove(move));

View File

@ -14,7 +14,7 @@ import { StatusEffect } from "#enums/status-effect";
import type { WeatherType } from "#enums/weather-type"; import type { WeatherType } from "#enums/weather-type";
import { expect, vi } from "vitest"; import { expect, vi } from "vitest";
import { GameManagerHelper } from "./gameManagerHelper"; import { GameManagerHelper } from "./gameManagerHelper";
import { makeArray, shiftCharCodes } from "#app/utils/common"; import { coerceArray, shiftCharCodes } from "#app/utils/common";
import type { RandomTrainerOverride } from "#app/overrides"; import type { RandomTrainerOverride } from "#app/overrides";
import type { BattleType } from "#enums/battle-type"; import type { BattleType } from "#enums/battle-type";
@ -202,7 +202,7 @@ export class OverridesHelper extends GameManagerHelper {
*/ */
public moveset(moveset: MoveId | MoveId[]): this { public moveset(moveset: MoveId | MoveId[]): this {
vi.spyOn(Overrides, "MOVESET_OVERRIDE", "get").mockReturnValue(moveset); vi.spyOn(Overrides, "MOVESET_OVERRIDE", "get").mockReturnValue(moveset);
moveset = makeArray(moveset); moveset = coerceArray(moveset);
const movesetStr = moveset.map(moveId => MoveId[moveId]).join(", "); const movesetStr = moveset.map(moveId => MoveId[moveId]).join(", ");
this.log(`Player Pokemon moveset set to ${movesetStr} (=[${moveset.join(", ")}])!`); this.log(`Player Pokemon moveset set to ${movesetStr} (=[${moveset.join(", ")}])!`);
return this; return this;
@ -380,7 +380,7 @@ export class OverridesHelper extends GameManagerHelper {
*/ */
public enemyMoveset(moveset: MoveId | MoveId[]): this { public enemyMoveset(moveset: MoveId | MoveId[]): this {
vi.spyOn(Overrides, "OPP_MOVESET_OVERRIDE", "get").mockReturnValue(moveset); vi.spyOn(Overrides, "OPP_MOVESET_OVERRIDE", "get").mockReturnValue(moveset);
moveset = makeArray(moveset); moveset = coerceArray(moveset);
const movesetStr = moveset.map(moveId => MoveId[moveId]).join(", "); const movesetStr = moveset.map(moveId => MoveId[moveId]).join(", ");
this.log(`Enemy Pokemon moveset set to ${movesetStr} (=[${moveset.join(", ")}])!`); this.log(`Enemy Pokemon moveset set to ${movesetStr} (=[${moveset.join(", ")}])!`);
return this; return this;