Files
hoppscotch/packages/hoppscotch-common/src/helpers/collection/affectedIndex.ts
Anwarul Islam defece95fc feat: rest revamp (#2918)
Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
Co-authored-by: Nivedin <53208152+nivedin@users.noreply.github.com>
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
2023-03-31 00:45:42 +05:30

22 lines
522 B
TypeScript

/**
* Get the indexes that are affected by the reorder
* @param from index of the item before reorder
* @param to index of the item after reorder
* @returns Map of from to to
*/
export function getAffectedIndexes(from: number, to: number) {
const indexes = new Map<number, number>()
indexes.set(from, to)
if (from < to) {
for (let i = from + 1; i <= to; i++) {
indexes.set(i, i - 1)
}
} else {
for (let i = from - 1; i >= to; i--) {
indexes.set(i, i + 1)
}
}
return indexes
}