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|.
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