-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring sorting & grouping (wip)
- Loading branch information
Showing
6 changed files
with
91 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 0 additions & 79 deletions
79
src/main/modules/ProcessDataRequest/ProcessTodoObjects.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
function sortAndGroupTodoObjects(todoObjects: TodoObject[], sorting: Sorting[]): TodoObject[] { | ||
function compareValues(a: any, b: any, invert: boolean): number { | ||
const comparison = String(a).localeCompare(String(b), undefined, { sensitivity: 'base' }); | ||
return invert ? -comparison : comparison; | ||
} | ||
|
||
function applySorting(a: any, b: any): number { | ||
for (const { value, invert } of sorting) { | ||
const compareResult = compareValues(a[value], b[value], invert); | ||
if (compareResult !== 0) { | ||
return compareResult; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
function groupObjectsByKey(todoObjects, attributeKey) { | ||
const grouped = {}; | ||
for (const todoObject of todoObjects) { | ||
const groupKey = todoObject[attributeKey] || ''; | ||
if (!grouped[groupKey]) { | ||
grouped[groupKey] = { | ||
title: groupKey, | ||
visible: false, | ||
todoObjects: [] | ||
}; | ||
} | ||
if(todoObject.visible) grouped[groupKey].visible = true; | ||
grouped[groupKey].todoObjects.push(todoObject); | ||
} | ||
return Object.values(grouped); | ||
} | ||
|
||
function sortAndGroupObjects(objects: any[]): any { | ||
const { value } = sorting[0]; | ||
const grouped = groupObjectsByKey(objects, value); | ||
return grouped; | ||
} | ||
|
||
const sortedTodoObjects = Object.values(todoObjects).sort(applySorting); | ||
|
||
return sortAndGroupObjects(sortedTodoObjects); | ||
} | ||
|
||
export { sortAndGroupTodoObjects }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters