Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Jul 22, 2024
1 parent 2ac906d commit e015960
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ import type {
GlobalDoc,
} from '@docusaurus/plugin-content-docs/client';

function createVersion(name: string, path: string, isLast: boolean) {
return {
name,
label: name,
path,
isLast,
docs: [],
mainDocId: '???',
draftIds: [],
};
}

describe('docsClientUtils', () => {
it('getActivePlugin', () => {
const data: {[key: string]: GlobalPluginData} = {
Expand Down Expand Up @@ -191,61 +203,58 @@ describe('docsClientUtils', () => {
);
});

it('sortVersionsByPathDepth', () => {
function createVersion(name: string, path: string, isLast: boolean) {
return {
name,
label: name,
path,
isLast,
docs: [],
mainDocId: '???',
draftIds: [],
};
}
it('sortVersionsByPathDepth without trailing slash', () => {
const test1 = [
createVersion('current', '/docs', false),
createVersion('version2', '/docs/version2', true),
createVersion('version1', '/docs/version1', false),
];

expect(sortVersionsByPathDepth(test1)).toEqual([
test1[1], // version2
test1[2], // version1
test1[0], // current
]);
});

it('sortVersionsByPathDepth with trailing slash', () => {
const test2 = [
createVersion('current', '/docs/', false),
createVersion('version2', '/docs/version2/', true),
createVersion('version1', '/docs/version1/', false),
];

// docs only website
expect(sortVersionsByPathDepth(test2)).toEqual([
test2[1], // version2
test2[2], // version1
test2[0], // current
]);
});

it('sortVersionsByPathDepth docs only without trailing slash', () => {
const test3 = [
createVersion('current', '/', false),
createVersion('version2', '/version2', true),
createVersion('version1', '/version1', false),
];

// docs only with trailing slash
expect(sortVersionsByPathDepth(test3)).toEqual([
test3[1], // version2
test3[2], // version1
test3[0], // current
]);
});

it('sortVersionsByPathDepth docs only with trailing slash', () => {
const test4 = [
createVersion('current', '/', false),
createVersion('version2', '/version2/', true),
createVersion('version1', '/version1/', false),
];

expect(sortVersionsByPathDepth(test1)).toEqual([
test1[2], // version1
test1[1], // version2
test1[0], // current
]);
expect(sortVersionsByPathDepth(test2)).toEqual([
test2[2], // version1
test2[1], // version2
test2[0], // current
]);
expect(sortVersionsByPathDepth(test3)).toEqual([
test3[2], // version1
test3[1], // version2
test3[0], // current
]);
expect(sortVersionsByPathDepth(test4)).toEqual([
test4[2], // version1
test4[1], // version2
test4[2], // version1
test4[0], // current
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,18 @@ export function sortVersionsByPathDepth(
versions: GlobalVersion[],
): GlobalVersion[] {
return [...versions].sort((a, b) => {
const getDepth = (path: string): number => {
if (path === '/') {
return 0;
if (a.path === b.path) {
return 0;
} else {
if (a.path.includes(b.path)) {
return -1;
}
if (b.path.includes(a.path)) {
return 1;
}
const trimmedPath = path.replace(/^\/|\/$/g, '');
return trimmedPath ? trimmedPath.split('/').length : 0;
};

const depthA = getDepth(a.path);
const depthB = getDepth(b.path);

// Sort by depth (descending order)
if (depthA !== depthB) {
return depthB - depthA;
}

// else sort alphabetically
return a.path.localeCompare(b.path);
return 0;
});
}

Expand Down

0 comments on commit e015960

Please sign in to comment.