diff --git a/src/js/lib.js b/src/js/lib.js index e459cd0..01c9f63 100644 --- a/src/js/lib.js +++ b/src/js/lib.js @@ -13,18 +13,6 @@ export const createRootElement = () => { return element } -const sorter = (a, b) => { - const isFileA = Boolean(a.href) - const isFileB = Boolean(b.href) - if (isFileA === isFileB) { - return (b.nodeLabel > a.nodeLabel) ? -1 : ((a.nodeLabel > b.nodeLabel) ? 1 : 0) - } else if (isFileA && !isFileB) { - return 1 - } else { - return -1 - } -} - const parseChangeNumber = (n) => { if (!n.replace) return 0 const number = parseInt(n.replace(',', ''), 10) @@ -115,9 +103,9 @@ export const folderConcat = (node) => { export const createFileTree = (filter = EMPTY_FILTER) => { const fileInfo = [...document.querySelectorAll('.file-info > a')] - const files = fileInfo.map(({ title, href }) => { + const files = fileInfo.map(({ title, href }, idx) => { title = getCurrentFileLocation(title) - return { title, href, parts: title.split('/') } + return { title, href, parts: title.split('/'), idx } }) const count = fileInfo.filter(({ href }) => href && href.includes('#diff')).length const tree = { @@ -126,7 +114,7 @@ export const createFileTree = (filter = EMPTY_FILTER) => { diffElements: [] } - files.forEach(({ parts, href }) => { + files.forEach(({ parts, href, idx }) => { let location = tree if (filterItem(parts[parts.length - 1], filter)) { parts.forEach((part, index) => { @@ -140,6 +128,7 @@ export const createFileTree = (filter = EMPTY_FILTER) => { const isDeleted = isDeletedForFileId(fileId) tree.diffElements.push(diffElement) node = { + idx, nodeLabel: part, list: [], href: (index === parts.length - 1) ? href : null, @@ -151,7 +140,7 @@ export const createFileTree = (filter = EMPTY_FILTER) => { location.list.push(node) } } - location.list = location.list.sort(sorter) + location.list = location.list.sort((a, b) => a.idx - b.idx) location = node }) }