Skip to content

Commit

Permalink
fix(archive): Addressing PR feedback
Browse files Browse the repository at this point in the history
* Remove try/catch
* Add test for comparing to null values in the sort
  • Loading branch information
mickr committed Jan 17, 2020
1 parent e8755db commit 113ff01
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/lib/viewers/archive/ArchiveExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,17 @@ class ArchiveExplorer extends React.Component {
return itemList;
}

let sortedItems;
const sortedItems = itemList.sort((a = {}, b = {}) => {
if (!a[sortBy] || !b[sortBy]) {
return -1;
}

try {
sortedItems = itemList.sort((a = {}, b = {}) => {
if (!a[sortBy] || !b[sortBy]) {
return -1;
}
if (typeof a[sortBy] === 'number' && typeof b[sortBy] === 'number') {
return a[sortBy] - b[sortBy];
}

if (typeof a[sortBy] === 'number' && typeof b[sortBy] === 'number') {
return a[sortBy] - b[sortBy];
}

return a[sortBy].localeCompare(b[sortBy]);
});
} catch {
sortedItems = itemList;
}
return a[sortBy].localeCompare(b[sortBy]);
});

return sortDirection === SortDirection.ASC ? sortedItems : sortedItems.reverse();
}
Expand Down
15 changes: 15 additions & 0 deletions src/lib/viewers/archive/__tests__/ArchiveExplorer-test-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,20 @@ describe('lib/viewers/archive/ArchiveExplorer', () => {

expect(sortedList[0]).to.equal(itemList[0]);
});

it('should sort item list with null values', () => {
data[1].modified_at = null;
data[2].modified_at = null;

const component = getComponent({ filename, itemCollection: data });
const instance = component.instance();
const itemList = instance.getItemList(data, 'test/');

instance.handleSort({ sortBy: 'modified_at' });

const sortedList = instance.sortItemList(itemList);

expect(sortedList[0]).to.equal(itemList[0]);
});
});
});

0 comments on commit 113ff01

Please sign in to comment.