Skip to content

Commit

Permalink
fix: objArraySort top level array check
Browse files Browse the repository at this point in the history
  • Loading branch information
Yazawazi committed Nov 28, 2023
1 parent e57e697 commit 1da9e1c
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions frontend/src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,20 @@ function recursiveSort(arr: (string | object)[]): (string | object)[] {
}

export function objArraySort(obj: object[]): object[] {
if (obj.length === 0) {
return [];
}
const result: object[] = [];
const topLevelArray = {
"": (
(
obj.filter(
(item) =>
Object.keys(item).length === 1 && Object.keys(item)[0] === ""
)[0] as any
)[""] as string[]
).sort((a, b) => a.localeCompare(b)),
};
console.log(obj);
const topLevelObjects = obj.filter(
(item) => Object.keys(item).length === 1 && Object.keys(item)[0] === ""
);
const topLevelArray: Record<string, any> = {};
if (topLevelObjects.length > 0) {
topLevelArray[""] = ((topLevelObjects[0] as any)[""] as string[]).sort(
(a, b) => a.localeCompare(b)
);
}

obj
.sort((a, b) => {
Expand All @@ -299,7 +302,9 @@ export function objArraySort(obj: object[]): object[] {
}
});

result.push(topLevelArray);
if (topLevelObjects.length > 0) {
result.push(topLevelArray);
}

return result;
}

0 comments on commit 1da9e1c

Please sign in to comment.