mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
refactor(v2): replace Lodash with single methods packages in utils (#2536)
This commit is contained in:
parent
ff5029893e
commit
5d65facc81
3 changed files with 23 additions and 13 deletions
|
@ -34,7 +34,7 @@
|
||||||
"@types/inquirer": "^6.5.0",
|
"@types/inquirer": "^6.5.0",
|
||||||
"@types/jest": "^24.0.23",
|
"@types/jest": "^24.0.23",
|
||||||
"@types/loader-utils": "^1.1.3",
|
"@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.flatmap": "^4.5.6",
|
||||||
"@types/lodash.groupby": "^4.6.6",
|
"@types/lodash.groupby": "^4.6.6",
|
||||||
"@types/lodash.has": "^4.5.6",
|
"@types/lodash.has": "^4.5.6",
|
||||||
|
|
|
@ -15,7 +15,8 @@
|
||||||
"escape-string-regexp": "^2.0.0",
|
"escape-string-regexp": "^2.0.0",
|
||||||
"fs-extra": "^8.1.0",
|
"fs-extra": "^8.1.0",
|
||||||
"gray-matter": "^4.0.2",
|
"gray-matter": "^4.0.2",
|
||||||
"lodash": "^4.17.15"
|
"lodash.camelcase": "^4.3.0",
|
||||||
|
"lodash.kebabcase": "^4.1.1"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=10.9.0"
|
"node": ">=10.9.0"
|
||||||
|
|
|
@ -8,7 +8,8 @@
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import matter from 'gray-matter';
|
import matter from 'gray-matter';
|
||||||
import {createHash} from 'crypto';
|
import {createHash} from 'crypto';
|
||||||
import _ from 'lodash';
|
import camelCase from 'lodash.camelcase';
|
||||||
|
import kebabCase from 'lodash.kebabcase';
|
||||||
import escapeStringRegexp from 'escape-string-regexp';
|
import escapeStringRegexp from 'escape-string-regexp';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
|
|
||||||
|
@ -51,13 +52,14 @@ export async function generate(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function objectWithKeySorted(obj: Object) {
|
export function objectWithKeySorted(obj: {[index: string]: any}) {
|
||||||
// https://github.com/lodash/lodash/issues/1459#issuecomment-253969771
|
// https://github.com/lodash/lodash/issues/1459#issuecomment-460941233
|
||||||
return _(obj)
|
return Object.keys(obj)
|
||||||
.toPairs()
|
.sort()
|
||||||
.sortBy(0)
|
.reduce((acc: any, key: string) => {
|
||||||
.fromPairs()
|
acc[key] = obj[key];
|
||||||
.value();
|
return acc;
|
||||||
|
}, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
const indexRE = /(^|.*\/)index\.(md|js|jsx|ts|tsx)$/i;
|
const indexRE = /(^|.*\/)index\.(md|js|jsx|ts|tsx)$/i;
|
||||||
|
@ -93,7 +95,15 @@ export function docuHash(str: string): string {
|
||||||
.update(str)
|
.update(str)
|
||||||
.digest('hex')
|
.digest('hex')
|
||||||
.substr(0, 3);
|
.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) : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -105,8 +115,7 @@ export function genComponentName(pagePath: string): string {
|
||||||
return 'index';
|
return 'index';
|
||||||
}
|
}
|
||||||
const pageHash = docuHash(pagePath);
|
const pageHash = docuHash(pagePath);
|
||||||
const pascalCase = _.flow(_.camelCase, _.upperFirst);
|
return upperFirst(camelCase(pageHash));
|
||||||
return pascalCase(pageHash);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue