Skip to content

Commit

Permalink
refactor(v2): replace Lodash with single methods packages in utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Simek committed Apr 5, 2020
1 parent 7bc7241 commit c6c80fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
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

0 comments on commit c6c80fd

Please sign in to comment.