@vendure/core
Version:
A modern, headless ecommerce framework
29 lines • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.moveToIndex = moveToIndex;
const utils_1 = require("../../../common/utils");
/**
* Moves the target Orderable entity to the given index amongst its siblings.
* Returns the siblings (including the target) which should then be
* persisted to the database.
*/
function moveToIndex(index, target, siblings) {
const normalizedIndex = Math.max(Math.min(index, siblings.length), 0);
let currentIndex = siblings.findIndex(sibling => (0, utils_1.idsAreEqual)(sibling.id, target.id));
const orderedSiblings = [...siblings].sort((a, b) => (a.position > b.position ? 1 : -1));
const siblingsWithTarget = currentIndex < 0 ? [...orderedSiblings, target] : [...orderedSiblings];
currentIndex = siblingsWithTarget.findIndex(sibling => (0, utils_1.idsAreEqual)(sibling.id, target.id));
if (currentIndex !== normalizedIndex) {
siblingsWithTarget.splice(normalizedIndex, 0, siblingsWithTarget.splice(currentIndex, 1)[0]);
}
// Always reassign positions to ensure consistency — the target item
// may carry a stale position value from a different parent.
siblingsWithTarget.forEach((collection, i) => {
collection.position = i;
if (target.id === collection.id) {
target.position = i;
}
});
return siblingsWithTarget;
}
//# sourceMappingURL=move-to-index.js.map