Skip to content

Commit 7b7e4f2

Browse files
authored
Merge pull request #874 from acacode/reduce-use-of-lodash
Reduce use of lodash
2 parents 7a7111b + a1b7136 commit 7b7e4f2

30 files changed

+231
-278
lines changed

cli/execute.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ const processArgs = (commands, args) => {
9898

9999
let allFlagKeys = [];
100100

101-
lodash.forEach(args, (arg, i) => {
101+
args.forEach((arg, i) => {
102102
if (error) return;
103103

104104
if (i === 0) {
105105
command = commands[arg];
106106

107107
if (!command && !arg.startsWith("-")) {
108-
const tip = didYouMean(arg, lodash.keys(commands));
108+
const tip = didYouMean(arg, commands.keys());
109109
error = `unknown command ${arg}${
110110
tip ? `\n(Did you mean ${tip} ?)` : ""
111111
}`;

cli/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const cli = (input) => {
1212
commands[command.name] = {
1313
name: command.name,
1414
description: `${command.description || ""}`,
15-
options: lodash.compact(lodash.map(command.options, processOption)),
15+
options: lodash.compact(command.options.map(processOption)),
1616
};
1717

1818
if (addVersion) {
@@ -57,7 +57,7 @@ const cli = (input) => {
5757
},
5858
);
5959

60-
lodash.forEach(input.options, (option) => {
60+
for (const option of input.options) {
6161
const processed = processOption(option);
6262

6363
if (!processed) return;
@@ -68,7 +68,7 @@ const cli = (input) => {
6868
}
6969

7070
commands[root_command].options.push(processed);
71-
});
71+
}
7272

7373
commands[root_command].options.unshift(
7474
processOption({
@@ -86,7 +86,9 @@ const cli = (input) => {
8686
}),
8787
);
8888

89-
lodash.forEach(input.commands, addCommand);
89+
for (const command of input.commands) {
90+
addCommand(command);
91+
}
9092

9193
return instance;
9294
};

cli/process-option.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,30 @@ const processFlags = (flags) => {
1515
let value = null;
1616
const isNoFlag = flags.includes("--no-");
1717

18-
lodash
19-
.compact(lodash.split(flags, " ").map((str) => str.replace(/,/g, "")))
20-
.forEach((str) => {
21-
if (str.startsWith("-")) {
22-
keys.push(str);
23-
} else if (value === null) {
24-
if (str.startsWith("{") || str.startsWith("[") || str.startsWith("<")) {
25-
const rawValue = str.replace(/[{[<>}\].]/g, "");
26-
const variadic = str.includes("...");
27-
value = {
28-
raw: str,
29-
variadic,
30-
name: rawValue,
31-
formatter: optionFormatters[rawValue] || optionFormatters.string,
32-
};
33-
}
18+
const strArr = lodash.compact(
19+
flags.split(" ").map((str) => str.replace(/,/g, "")),
20+
);
21+
22+
for (const str of strArr) {
23+
if (str.startsWith("-")) {
24+
keys.push(str);
25+
} else if (value === null) {
26+
if (str.startsWith("{") || str.startsWith("[") || str.startsWith("<")) {
27+
const rawValue = str.replace(/[{[<>}\].]/g, "");
28+
const variadic = str.includes("...");
29+
value = {
30+
raw: str,
31+
variadic,
32+
name: rawValue,
33+
formatter: optionFormatters[rawValue] || optionFormatters.string,
34+
};
3435
}
35-
});
36+
}
37+
}
3638

3739
const longestKey = keys.slice().sort((a, b) => b.length - a.length)[0];
3840

39-
if (!lodash.isEmpty(longestKey)) {
41+
if (longestKey !== "") {
4042
name = lodash.camelCase(
4143
(isNoFlag ? longestKey.replace("--no-", "") : longestKey).replace(
4244
/(--?)/,

src/code-formatter.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ class CodeFormatter {
2424
)[0];
2525

2626
if (fileTextChanges?.textChanges.length) {
27-
return lodash.reduceRight(
28-
fileTextChanges.textChanges,
27+
return fileTextChanges.textChanges.reduceRight(
2928
(content, { span, newText }) =>
3029
`${content.slice(0, span.start)}${newText}${content.slice(
3130
span.start + span.length,

src/code-gen-process.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class CodeGenProcess {
177177
const isDirPath = this.fileSystem.pathIsDir(this.config.output);
178178

179179
if (isDirPath) {
180-
files.forEach((file) => {
180+
for (const file of files) {
181181
this.fileSystem.createFile({
182182
path: this.config.output,
183183
fileName: `${file.fileName}${file.fileExtension}`,
@@ -190,7 +190,7 @@ class CodeGenProcess {
190190
`"${file.fileName}${file.fileExtension}"`,
191191
`created in ${this.config.output}`,
192192
);
193-
});
193+
}
194194
}
195195

196196
return {
@@ -289,7 +289,12 @@ class CodeGenProcess {
289289
rawTypeData,
290290
)
291291
: rawTypeData;
292-
let { typeIdentifier, name: originalName, content, description } = typeData;
292+
const {
293+
typeIdentifier,
294+
name: originalName,
295+
content,
296+
description,
297+
} = typeData;
293298
const name = this.typeNameFormatter.format(originalName);
294299

295300
if (name === null) return null;
@@ -560,11 +565,11 @@ class CodeGenProcess {
560565

561566
injectClassInstance = (key, value) => {
562567
this[key] = value;
563-
PATCHABLE_INSTANCES.forEach((instanceKey) => {
568+
for (const instanceKey of PATCHABLE_INSTANCES) {
564569
if (instanceKey !== key && key in this[instanceKey]) {
565570
this[instanceKey][key] = value;
566571
}
567-
});
572+
}
568573
};
569574
}
570575

src/commands/generate-templates/templates-gen-process.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class TemplatesGenProcess {
5959
this.fileSystem.createDir(outputPath);
6060
}
6161

62-
templates.forEach((template) => {
62+
for (const template of templates) {
6363
const templateName = this.fileSystem.cropExtension(template.name);
6464
const templateEjsPath = path.resolve(outputPath, `${templateName}.ejs`);
6565
const templateEtaPath = path.resolve(outputPath, `${templateName}.eta`);
@@ -94,7 +94,7 @@ class TemplatesGenProcess {
9494
});
9595
}
9696
}
97-
});
97+
}
9898

9999
this.logger.success(
100100
`source templates has been successfully created in "${outputPath}"`,

src/configuration.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ class CodeGenConfig {
213213
customTranslator;
214214

215215
Ts = {
216-
Keyword: lodash.cloneDeep(TsKeyword),
217-
CodeGenKeyword: lodash.cloneDeep(TsCodeGenKeyword),
216+
Keyword: structuredClone(TsKeyword),
217+
CodeGenKeyword: structuredClone(TsCodeGenKeyword),
218218
/**
219219
* $A[] or Array<$A>
220220
*/

src/schema-components-map.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ class SchemaComponentsMap {
6161
* @returns {SchemaComponent[]}
6262
*/
6363
filter(...componentNames) {
64-
return lodash.filter(this._data, (it) =>
64+
return this._data.filter((it) =>
6565
componentNames.some((componentName) =>
66-
lodash.startsWith(it.$ref, `#/components/${componentName}`),
66+
it.$ref.startsWith(`#/components/${componentName}`),
6767
),
6868
);
6969
}

src/schema-parser/base-schema-parsers/array.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ArraySchemaParser extends MonoSchemaParser {
77
let contentType;
88
const { type, description, items } = this.schema || {};
99

10-
if (lodash.isArray(items) && type === SCHEMA_TYPES.ARRAY) {
10+
if (Array.isArray(items) && type === SCHEMA_TYPES.ARRAY) {
1111
const tupleContent = [];
1212
for (const item of items) {
1313
tupleContent.push(
@@ -25,7 +25,7 @@ class ArraySchemaParser extends MonoSchemaParser {
2525
}
2626

2727
return {
28-
...(lodash.isObject(this.schema) ? this.schema : {}),
28+
...(typeof this.schema === "object" ? this.schema : {}),
2929
$schemaPath: this.schemaPath.slice(),
3030
$parsedSchema: true,
3131
schemaType: SCHEMA_TYPES.PRIMITIVE,

src/schema-parser/base-schema-parsers/complex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ComplexSchemaParser extends MonoSchemaParser {
1414
](this.schema);
1515

1616
return {
17-
...(lodash.isObject(this.schema) ? this.schema : {}),
17+
...(typeof this.schema === "object" ? this.schema : {}),
1818
$schemaPath: this.schemaPath.slice(),
1919
$parsedSchema: true,
2020
schemaType: SCHEMA_TYPES.COMPLEX,

0 commit comments

Comments
 (0)