fix(v2): fix theme array deduplication bug (#5000)

This commit is contained in:
Sébastien Lorber 2021-06-18 11:53:11 +02:00 committed by GitHub
parent fb172dc4e6
commit ba65450571
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 5 deletions

View file

@ -30,6 +30,7 @@
"@docusaurus/theme-common": "2.0.0-beta.0",
"@docusaurus/types": "2.0.0-beta.0",
"@docusaurus/utils": "2.0.0-beta.0",
"@docusaurus/utils-common": "2.0.0-beta.0",
"@docusaurus/utils-validation": "2.0.0-beta.0",
"@mdx-js/mdx": "^1.6.21",
"@mdx-js/react": "^1.6.21",

View file

@ -11,6 +11,7 @@ import {useLatestVersion, useActiveDocContext} from '@theme/hooks/useDocs';
import clsx from 'clsx';
import type {Props} from '@theme/NavbarItem/DocNavbarItem';
import {useDocsPreferredVersion} from '@docusaurus/theme-common';
import {uniq} from '@docusaurus/utils-common';
import type {
GlobalDataVersion,
GlobalDataDoc,
@ -47,11 +48,9 @@ export default function DocNavbarItem({
const latestVersion = useLatestVersion(docsPluginId);
// Versions used to look for the doc to link to, ordered + no duplicate
const versions: GlobalDataVersion[] = [
...new Set(
[activeVersion, preferredVersion, latestVersion].filter(Boolean),
),
];
const versions: GlobalDataVersion[] = uniq(
[activeVersion, preferredVersion, latestVersion].filter(Boolean),
);
const doc = getDocInVersions(versions, docId);
return (

View file

@ -0,0 +1,33 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import uniq from '../uniq';
describe('uniq', () => {
test('remove duplicate primitives', () => {
expect(uniq(['A', 'B', 'C', 'B', 'A', 'D'])).toEqual(['A', 'B', 'C', 'D']);
expect(uniq([3, 3, 5, 1, 6, 3, 5])).toEqual([3, 5, 1, 6]);
expect(uniq([null, undefined, 3, null, 4, 3])).toEqual([
null,
undefined,
3,
4,
]);
});
test('remove duplicate objects/arrays by identity', () => {
const obj1 = {};
const obj2 = {};
const obj3 = {};
const array1: unknown[] = [];
const array2: unknown[] = [];
const array3: unknown[] = [];
expect(
uniq([obj1, obj1, obj2, array1, obj2, array3, array2, array1, obj3]),
).toEqual([obj1, obj2, array1, array3, array2, obj3]);
});
});

View file

@ -6,3 +6,4 @@
*/
export {default as applyTrailingSlash} from './applyTrailingSlash';
export {default as uniq} from './uniq';

View file

@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// Remove duplicate array items (similar to _.uniq)
export default function uniq<T>(array: T[]): T[] {
// Note: had problems with [...new Set()]: https://github.com/facebook/docusaurus/issues/4972#issuecomment-863895061
return Array.from(new Set(array));
}