refactor: ensure lodash is default-imported (#6716)

This commit is contained in:
Joshua Chen 2022-02-19 18:15:02 +08:00 committed by GitHub
parent 47c9a37c5f
commit ea6ceaa371
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 239 additions and 221 deletions

View file

@ -13,7 +13,7 @@ import type {
NormalizedSidebarItem,
SidebarItemCategoryLinkConfig,
} from './types';
import {sortBy, last} from 'lodash';
import _ from 'lodash';
import {addTrailingSlash, posixPath} from '@docusaurus/utils';
import logger from '@docusaurus/logger';
import path from 'path';
@ -25,7 +25,7 @@ const docIdPrefix = '$doc$/';
// Just an alias to the make code more explicit
function getLocalDocId(docId: string): string {
return last(docId.split('/'))!;
return _.last(docId.split('/'))!;
}
export const CategoryMetadataFilenameBase = '_category_';
@ -248,7 +248,7 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({
}
return item;
});
const sortedSidebarItems = sortBy(
const sortedSidebarItems = _.sortBy(
processedSidebarItems,
(item) => item.position,
);

View file

@ -17,7 +17,7 @@ import {Globby} from '@docusaurus/utils';
import logger from '@docusaurus/logger';
import type {PluginOptions} from '@docusaurus/plugin-content-docs';
import Yaml from 'js-yaml';
import {groupBy, mapValues} from 'lodash';
import _ from 'lodash';
import combinePromises from 'combine-promises';
export const DefaultSidebars: SidebarsConfig = {
@ -46,9 +46,9 @@ async function readCategoriesMetadata(contentPath: string) {
const categoryFiles = await Globby('**/_category_.{json,yml,yaml}', {
cwd: contentPath,
});
const categoryToFile = groupBy(categoryFiles, path.dirname);
const categoryToFile = _.groupBy(categoryFiles, path.dirname);
return combinePromises(
mapValues(categoryToFile, async (files, folder) => {
_.mapValues(categoryToFile, async (files, folder) => {
const [filePath] = files;
if (files.length > 1) {
logger.warn`There are more than one category metadata files for path=${folder}: ${files.join(

View file

@ -17,7 +17,7 @@ import type {
NormalizedSidebarItemCategory,
} from './types';
import {isCategoriesShorthand} from './utils';
import {mapValues} from 'lodash';
import _ from 'lodash';
function normalizeCategoriesShorthand(
sidebar: SidebarCategoriesShorthand,
@ -81,5 +81,5 @@ function normalizeSidebar(sidebar: SidebarConfig): NormalizedSidebar {
export function normalizeSidebars(
sidebars: SidebarsConfig,
): NormalizedSidebars {
return mapValues(sidebars, normalizeSidebar);
return _.mapValues(sidebars, normalizeSidebar);
}

View file

@ -15,7 +15,7 @@ import type {
ProcessedSidebars,
SidebarItemCategoryLink,
} from './types';
import {mapValues} from 'lodash';
import _ from 'lodash';
function normalizeCategoryLink(
category: ProcessedSidebarItemCategory,
@ -88,7 +88,7 @@ export function postProcessSidebars(
sidebars: ProcessedSidebars,
params: SidebarProcessorParams,
): Sidebars {
return mapValues(sidebars, (sidebar) =>
return _.mapValues(sidebars, (sidebar) =>
sidebar.map((item) => postProcessSidebarItem(item, params)),
);
}

View file

@ -21,14 +21,14 @@ import type {
} from './types';
import {DefaultSidebarItemsGenerator} from './generator';
import {validateSidebars} from './validation';
import {mapValues, memoize, pick} from 'lodash';
import _ from 'lodash';
import combinePromises from 'combine-promises';
import {isCategoryIndex} from '../docs';
function toSidebarItemsGeneratorDoc(
doc: DocMetadataBase,
): SidebarItemsGeneratorDoc {
return pick(doc, [
return _.pick(doc, [
'id',
'unversionedId',
'frontMatter',
@ -41,7 +41,7 @@ function toSidebarItemsGeneratorDoc(
function toSidebarItemsGeneratorVersion(
version: VersionMetadata,
): SidebarItemsGeneratorVersion {
return pick(version, ['versionName', 'contentPath']);
return _.pick(version, ['versionName', 'contentPath']);
}
// Handle the generation of autogenerated sidebar items and other
@ -60,7 +60,7 @@ async function processSidebar(
} = params;
// Just a minor lazy transformation optimization
const getSidebarItemsGeneratorDocsAndVersion = memoize(() => ({
const getSidebarItemsGeneratorDocsAndVersion = _.memoize(() => ({
docs: docs.map(toSidebarItemsGeneratorDoc),
version: toSidebarItemsGeneratorVersion(version),
}));
@ -117,7 +117,7 @@ export async function processSidebars(
params: SidebarProcessorParams,
): Promise<ProcessedSidebars> {
const processedSidebars = await combinePromises(
mapValues(unprocessedSidebars, (unprocessedSidebar) =>
_.mapValues(unprocessedSidebars, (unprocessedSidebar) =>
processSidebar(unprocessedSidebar, categoriesMetadata, params),
),
);

View file

@ -19,7 +19,7 @@ import type {
SidebarNavigationItem,
} from './types';
import {mapValues, difference, uniq} from 'lodash';
import _ from 'lodash';
import {getElementsAround, toMessageRelativeFilePath} from '@docusaurus/utils';
import type {DocMetadataBase, DocNavLink} from '../types';
@ -110,13 +110,13 @@ export function collectSidebarNavigation(
export function collectSidebarsDocIds(
sidebars: Sidebars,
): Record<string, string[]> {
return mapValues(sidebars, collectSidebarDocIds);
return _.mapValues(sidebars, collectSidebarDocIds);
}
export function collectSidebarsNavigations(
sidebars: Sidebars,
): Record<string, SidebarNavigationItem[]> {
return mapValues(sidebars, collectSidebarNavigation);
return _.mapValues(sidebars, collectSidebarNavigation);
}
export type SidebarNavigation = {
@ -276,7 +276,7 @@ export function createSidebarsUtils(sidebars: Sidebars): SidebarsUtils {
function checkSidebarsDocIds(validDocIds: string[], sidebarFilePath: string) {
const allSidebarDocIds = Object.values(sidebarNameToDocIds).flat();
const invalidSidebarDocIds = difference(allSidebarDocIds, validDocIds);
const invalidSidebarDocIds = _.difference(allSidebarDocIds, validDocIds);
if (invalidSidebarDocIds.length > 0) {
throw new Error(
`Invalid sidebar file at "${toMessageRelativeFilePath(
@ -286,7 +286,7 @@ These sidebar document ids do not exist:
- ${invalidSidebarDocIds.sort().join('\n- ')}
Available document ids are:
- ${uniq(validDocIds).sort().join('\n- ')}`,
- ${_.uniq(validDocIds).sort().join('\n- ')}`,
);
}
}