Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import {type ReactNode, useMemo} from 'react';
import {useLocation} from '@docusaurus/router';
import Link from '@docusaurus/Link';
import {groupBy} from '@docusaurus/theme-common';
import {isSamePath} from '@docusaurus/theme-common/internal';
import type {BlogSidebarItem} from '@docusaurus/plugin-content-blog';

Expand Down Expand Up @@ -36,7 +35,7 @@ export function useVisibleBlogSidebarItems(
export function groupBlogSidebarItemsByYear(
items: BlogSidebarItem[],
): [string, BlogSidebarItem[]][] {
const groupedByYear = groupBy(items, (item) => {
const groupedByYear = Object.groupBy(items, (item) => {
return `${new Date(item.date).getFullYear()}`;
});
// "as" is safe here
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-theme-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export {isMultiColumnFooterLinks} from './utils/footerUtils';

export {isRegexpStringMatch} from './utils/regexpUtils';

export {duplicates, uniq, groupBy} from './utils/jsUtils';
export {duplicates, uniq} from './utils/jsUtils';

export {usePrismTheme} from './hooks/usePrismTheme';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import {describe, expect, it} from 'vitest';
import {uniq, duplicates, groupBy} from '../jsUtils';
import {uniq, duplicates} from '../jsUtils';

describe('duplicates', () => {
it('gets duplicate values', () => {
Expand Down Expand Up @@ -52,55 +52,3 @@ describe('uniq', () => {
).toEqual([obj1, obj2, array1, array3, array2, obj3]);
});
});

describe('groupBy', () => {
type User = {name: string; age: number; type: 'a' | 'b' | 'c'};

const user1: User = {name: 'Seb', age: 42, type: 'c'};
const user2: User = {name: 'Robert', age: 42, type: 'b'};
const user3: User = {name: 'Seb', age: 32, type: 'c'};

const users = [user1, user2, user3];

it('group by name', () => {
const groups = groupBy(users, (u) => u.name);

expect(Object.keys(groups)).toEqual(['Seb', 'Robert']);
expect(groups).toEqual({
Seb: [user1, user3],
Robert: [user2],
});
});

it('group by age', () => {
const groups = groupBy(users, (u) => u.age);

// Surprising keys order due to JS behavior
// see https://x.com/sebastienlorber/status/1806371668614369486
expect(Object.keys(groups)).toEqual(['32', '42']);
expect(groups).toEqual({
'32': [user3],
'42': [user1, user2],
});
});

it('group by type', () => {
const groups = groupBy(users, (u) => u.type);

expect(Object.keys(groups)).toEqual(['c', 'b']);
expect(groups).toEqual({
c: [user1, user3],
b: [user2],
});
});

it('group by name even duplicates', () => {
const groups = groupBy([user1, user2, user3, user1, user3], (u) => u.name);

expect(Object.keys(groups)).toEqual(['Seb', 'Robert']);
expect(groups).toEqual({
Seb: [user1, user3, user1, user3],
Robert: [user2],
});
});
});
18 changes: 0 additions & 18 deletions packages/docusaurus-theme-common/src/utils/jsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,3 @@ export function uniq<T>(arr: T[]): T[] {
// Note: had problems with [...new Set()]: https://github.com/facebook/docusaurus/issues/4972#issuecomment-863895061
return Array.from(new Set(arr));
}

// TODO 2025: replace by std Object.groupBy ?
// This is a local polyfill with exact same TS signature
// see https://github.com/microsoft/TypeScript/blob/main/src/lib/esnext.object.d.ts
export function groupBy<K extends PropertyKey, T>(
items: Iterable<T>,
keySelector: (item: T, index: number) => K,
): Partial<Record<K, T[]>> {
const result: Partial<Record<K, T[]>> = {};
let index = 0;
for (const item of items) {
const key = keySelector(item, index);
result[key] ??= [];
result[key]!.push(item);
index += 1;
}
return result;
}
Loading