From 1cd53768ab93be81821a5130c79177530d1aecc7 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Sun, 22 Oct 2017 23:57:23 -0400 Subject: [PATCH] clean up indexToInsertByPosition --- lib/browser/api/menu.js | 51 +++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/browser/api/menu.js b/lib/browser/api/menu.js index bacebc702c69..f9b4862ed589 100644 --- a/lib/browser/api/menu.js +++ b/lib/browser/api/menu.js @@ -46,39 +46,36 @@ function indexOfItemById (items, id) { } // Returns the index of where to insert the item according to |position|. -function indexToInsertByPosition (items, position) { - var insertIndex - if (!position) { - return items.length - } - const [query, id] = position.split('=') - insertIndex = indexOfItemById(items, id) - if (insertIndex === -1 && query !== 'endof') { +function indexToInsertByPosition (items, pos) { + if (!pos) return items.length + + const [query, id] = pos.split('=') + let idx = indexOfItemById(items, id) + + if (idx === -1 && query !== 'endof') { console.warn("Item with id '" + id + "' is not found") return items.length } - switch (query) { - case 'after': - insertIndex++ - break - case 'endof': - // If the |id| doesn't exist, then create a new group with the |id|. - if (insertIndex === -1) { - items.push({ - id: id, - type: 'separator' - }) - insertIndex = items.length - 1 - } + if (query === 'after') { + idx += 1 + } else if (query === 'endof') { + // create new group with id if none exists + if (idx === -1) { + items.push({ + id, + type: 'separator' + }) + idx = items.length - 1 + } + idx += 1 - // Find the end of the group. - insertIndex++ - while (insertIndex < items.length && items[insertIndex].type !== 'separator') { - insertIndex++ - } + // search for end of group + while (idx < items.length && items[idx].type !== 'separator') { + idx += 1 + } } - return insertIndex + return idx } const Menu = bindings.Menu