move index finding to separate method

This commit is contained in:
2025-01-22 20:09:49 -08:00
parent bc07fe5622
commit 144fa7c852

View File

@@ -1,13 +1,5 @@
import { GetterSetterRepository } from "./GetterSetterRepository"; import { GetterSetterRepository } from "./GetterSetterRepository";
import { import { IEntityWithId, IEta, IOrderedStop, IRoute, IShuttle, IStop, ISystem } from "../entities/entities";
IEntityWithId,
IEta,
IOrderedStop,
IRoute,
IShuttle,
IStop,
ISystem
} from "../entities/entities";
/** /**
* An unoptimized in memory repository. * An unoptimized in memory repository.
@@ -94,6 +86,10 @@ export class UnoptimizedInMemoryRepository implements GetterSetterRepository {
return entity; return entity;
} }
private findEntityIndexById<T extends IEntityWithId>(entityId: string, arrayToSearchIn: T[]) {
return arrayToSearchIn.findIndex((value) => value.id === entityId);
}
public async addOrUpdateSystem(system: ISystem): Promise<void> { public async addOrUpdateSystem(system: ISystem): Promise<void> {
const index = this.systems.findIndex((s) => s.id === system.id); const index = this.systems.findIndex((s) => s.id === system.id);
if (index !== -1) { if (index !== -1) {
@@ -149,7 +145,7 @@ export class UnoptimizedInMemoryRepository implements GetterSetterRepository {
} }
public async removeSystemIfExists(systemId: string): Promise<ISystem | null> { public async removeSystemIfExists(systemId: string): Promise<ISystem | null> {
const index = this.systems.findIndex((s) => s.id === systemId); const index = this.findEntityIndexById(systemId, this.systems);
if (index > -1) { if (index > -1) {
const systemToReturn = this.systems[index]; const systemToReturn = this.systems[index];
this.systems.splice(index, 1); this.systems.splice(index, 1);