function sort(source, target = []) { if (source.length === 0) { return target } const current = source.shift()
const { id, before, after, first, last } = current
if (first) { target.unshift(id) }
if (last) { target.push(id) }
if ('before' in current || 'after' in current) { const targetId = before || after const index = target.findIndex(value => value === targetId) if (index === -1) {
if (source.find(value => value === targetId)) { source.push(current) return sort(source, target) } else { target.push(id) }
} else { target.splice('before' in current ? index : index + 1, 0, id) } }
return sort(source, target) }
|