clean up indexToInsertByPosition

This commit is contained in:
Shelley Vohr 2017-10-22 23:57:23 -04:00
parent b7ebee985b
commit 1cd53768ab
No known key found for this signature in database
GPG key ID: F13993A75599653C

View file

@ -46,39 +46,36 @@ function indexOfItemById (items, id) {
} }
// Returns the index of where to insert the item according to |position|. // Returns the index of where to insert the item according to |position|.
function indexToInsertByPosition (items, position) { function indexToInsertByPosition (items, pos) {
var insertIndex if (!pos) return items.length
if (!position) {
return items.length const [query, id] = pos.split('=')
} let idx = indexOfItemById(items, id)
const [query, id] = position.split('=')
insertIndex = indexOfItemById(items, id) if (idx === -1 && query !== 'endof') {
if (insertIndex === -1 && query !== 'endof') {
console.warn("Item with id '" + id + "' is not found") console.warn("Item with id '" + id + "' is not found")
return items.length 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 (query === 'after') {
if (insertIndex === -1) { idx += 1
items.push({ } else if (query === 'endof') {
id: id, // create new group with id if none exists
type: 'separator' if (idx === -1) {
}) items.push({
insertIndex = items.length - 1 id,
} type: 'separator'
})
idx = items.length - 1
}
idx += 1
// Find the end of the group. // search for end of group
insertIndex++ while (idx < items.length && items[idx].type !== 'separator') {
while (insertIndex < items.length && items[insertIndex].type !== 'separator') { idx += 1
insertIndex++ }
}
} }
return insertIndex return idx
} }
const Menu = bindings.Menu const Menu = bindings.Menu