Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(v2): replace Lodash with single methods packages in utils #2536

Merged
merged 1 commit into from
Apr 5, 2020
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@types/inquirer": "^6.5.0",
"@types/jest": "^24.0.23",
"@types/loader-utils": "^1.1.3",
"@types/lodash": "^4.14.149",
"@types/lodash.camelcase": "^4.3.6",
"@types/lodash.flatmap": "^4.5.6",
"@types/lodash.groupby": "^4.6.6",
"@types/lodash.kebabcase": "^4.1.6",
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"escape-string-regexp": "^2.0.0",
"fs-extra": "^8.1.0",
"gray-matter": "^4.0.2",
"lodash": "^4.17.15"
"lodash.camelcase": "^4.3.0",
"lodash.kebabcase": "^4.1.1"
},
"engines": {
"node": ">=10.9.0"
Expand Down
31 changes: 20 additions & 11 deletions packages/docusaurus-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import path from 'path';
import matter from 'gray-matter';
import {createHash} from 'crypto';
import _ from 'lodash';
import camelCase from 'lodash.camelcase';
import kebabCase from 'lodash.kebabcase';
import escapeStringRegexp from 'escape-string-regexp';
import fs from 'fs-extra';

Expand Down Expand Up @@ -51,13 +52,14 @@ export async function generate(
}
}

export function objectWithKeySorted(obj: Object) {
// https://github.com/lodash/lodash/issues/1459#issuecomment-253969771
return _(obj)
.toPairs()
.sortBy(0)
.fromPairs()
.value();
export function objectWithKeySorted(obj: {[index: string]: any}) {
// https://github.com/lodash/lodash/issues/1459#issuecomment-460941233
return Object.keys(obj)
.sort()
.reduce((acc: any, key: string) => {
acc[key] = obj[key];
return acc;
}, {});
}

const indexRE = /(^|.*\/)index\.(md|js|jsx|ts|tsx)$/i;
Expand Down Expand Up @@ -93,7 +95,15 @@ export function docuHash(str: string): string {
.update(str)
.digest('hex')
.substr(0, 3);
return `${_.kebabCase(str)}-${shortHash}`;
return `${kebabCase(str)}-${shortHash}`;
}

/**
* Convert first string character to the upper case.
* E.g: docusaurus -> Docusaurus
*/
export function upperFirst(str: string): string {
return str ? str.charAt(0).toUpperCase() + str.slice(1) : '';
}

/**
Expand All @@ -105,8 +115,7 @@ export function genComponentName(pagePath: string): string {
return 'index';
}
const pageHash = docuHash(pagePath);
const pascalCase = _.flow(_.camelCase, _.upperFirst);
return pascalCase(pageHash);
return upperFirst(camelCase(pageHash));
}

/**
Expand Down