Fix regression from 70c3b51b0b

Negative indices were not treated correctly (as offset from end)
This commit is contained in:
Henrik Giesel 2021-04-23 18:21:03 +02:00
parent 7563a3c929
commit 7cd779063f

View File

@ -8,11 +8,19 @@ function normalize<T extends Identifiable>(
values: T[],
idOrIndex: string | number
): number {
let normalizedIndex: number;
if (typeof idOrIndex === "string") {
return values.findIndex((value) => value.id === idOrIndex);
} else {
return idOrIndex >= values.length ? -1 : idOrIndex;
normalizedIndex = values.findIndex((value) => value.id === idOrIndex);
}
else if (idOrIndex < 0) {
normalizedIndex = values.length + idOrIndex;
}
else {
normalizedIndex = idOrIndex;
}
return normalizedIndex >= values.length ? -1 : normalizedIndex;
}
export function search<T extends Identifiable>(