From 429912385527a0306ce736cee8fd17a2f92a0a3a Mon Sep 17 00:00:00 2001 From: Murod Khaydarov Date: Fri, 18 Jan 2019 10:14:13 +0300 Subject: [PATCH] rewrite to splices --- src/controllers/pagesOrder.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/controllers/pagesOrder.js b/src/controllers/pagesOrder.js index 25719cd..02b3911 100644 --- a/src/controllers/pagesOrder.js +++ b/src/controllers/pagesOrder.js @@ -88,18 +88,10 @@ class PagesOrder { const children = await Model.get(parentPageId); const found1 = children.order.indexOf(putAbovePageId); const found2 = children.order.indexOf(currentPageId); + const margin = found1 < found2 ? 1 : 0; - if (found1 < found2) { - for (let i = found2; i >= found1; i--) { - children.order[i] = children.order[i - 1]; - } - children.order[found1] = currentPageId; - } else { - for (let i = found2; i < found1; i++) { - children.order[i] = children.order[i + 1]; - } - children.order[found1 - 1] = currentPageId; - } + children.order.splice(found1, 0, currentPageId); + children.order.splice(found2 + margin, 1); children.save(); } }