Skip to content

Commit

Permalink
Add internal-first sort strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
rsanchez authored and Gerrit0 committed Oct 28, 2023
1 parent d26f553 commit 02a14c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lib/utils/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const SORT_STRATEGIES = [
"visibility",
"required-first",
"kind",
"internal-first",
] as const;

export type SortStrategy = (typeof SORT_STRATEGIES)[number];
Expand Down Expand Up @@ -151,6 +152,9 @@ const sorts: Record<
kind(a, b, { kindSortOrder }) {
return kindSortOrder.indexOf(a.kind) < kindSortOrder.indexOf(b.kind);
},
"internal-first"(a, b) {
return !a.flags.isExternal && b.flags.isExternal;
},
};

export function getSortFunction(opts: Options) {
Expand Down
17 changes: 17 additions & 0 deletions src/test/utils/sort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@ describe("Sort", () => {
);
});

it("Should sort by internal first", () => {
const arr = [
new DeclarationReflection("a", ReflectionKind.Function),
new DeclarationReflection("b", ReflectionKind.Function),
new DeclarationReflection("c", ReflectionKind.Function),
];
arr[0].setFlag(ReflectionFlag.External, true);
arr[1].setFlag(ReflectionFlag.External, false);
arr[2].setFlag(ReflectionFlag.External, true);

sortReflections(arr, ["internal-first"]);
equal(
arr.map((r) => r.name),
["b", "a", "c"],
);
});

it("Should sort with multiple strategies", () => {
resetReflectionID();
const arr = [
Expand Down

0 comments on commit 02a14c0

Please sign in to comment.