Skip to content

Commit a1b7136

Browse files
committed
Reduce use of lodash
Signed-off-by: Sora Morimoto <sora@morimoto.io>
1 parent 7a7111b commit a1b7136

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,

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
3636
);
3737

3838
return {
39-
...(lodash.isObject(this.schema) ? this.schema : {}),
39+
...(typeof this.schema === "object" ? this.schema : {}),
4040
$schemaPath: this.schemaPath.slice(),
4141
$parsedSchema: true,
4242
schemaType: SCHEMA_TYPES.COMPLEX,
@@ -121,19 +121,19 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
121121

122122
if (ableToCreateMappingType) {
123123
return ts.TypeWithGeneric(mappingTypeName, [mappingUsageKey, content]);
124-
} else {
125-
return ts.ExpressionGroup(
126-
ts.IntersectionType([
127-
ts.ObjectWrapper(
128-
ts.TypeField({
129-
key: discriminator.propertyName,
130-
value: mappingUsageKey,
131-
}),
132-
),
133-
content,
134-
]),
135-
);
136124
}
125+
126+
return ts.ExpressionGroup(
127+
ts.IntersectionType([
128+
ts.ObjectWrapper(
129+
ts.TypeField({
130+
key: discriminator.propertyName,
131+
value: mappingUsageKey,
132+
}),
133+
),
134+
content,
135+
]),
136+
);
137137
};
138138

139139
for (const [mappingKey, schema] of mappingEntries) {
@@ -213,8 +213,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
213213
const mappingRefSchema =
214214
this.schemaUtils.getSchemaRefType(mappingSchema)?.rawTypeData;
215215
if (mappingRefSchema) {
216-
complexSchemaKeys.forEach((schemaKey) => {
217-
if (lodash.isArray(mappingRefSchema[schemaKey])) {
216+
for (const schemaKey of complexSchemaKeys) {
217+
if (Array.isArray(mappingRefSchema[schemaKey])) {
218218
mappingRefSchema[schemaKey] = mappingRefSchema[schemaKey].map(
219219
(schema) => {
220220
if (schema.$ref === refPath) {
@@ -251,7 +251,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
251251
},
252252
);
253253
}
254-
});
254+
}
255255
}
256256
}
257257
};
@@ -263,13 +263,12 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
263263
this.schemaParser._complexSchemaParsers,
264264
);
265265
const schema = lodash.omit(
266-
lodash.clone(noDiscriminatorSchema),
266+
structuredClone(noDiscriminatorSchema),
267267
complexSchemaKeys,
268268
);
269269
const schemaIsAny =
270-
this.schemaParserFabric.getInlineParseContent(
271-
lodash.cloneDeep(schema),
272-
) === this.config.Ts.Keyword.Any;
270+
this.schemaParserFabric.getInlineParseContent(structuredClone(schema)) ===
271+
this.config.Ts.Keyword.Any;
273272
const schemaIsEmpty = !lodash.keys(schema).length;
274273

275274
if (schemaIsEmpty || schemaIsAny) return null;

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,28 @@ class EnumSchemaParser extends MonoSchemaParser {
7070
return this.config.Ts.NullValue(value);
7171
}
7272
if (
73-
lodash.includes(
74-
keyType,
75-
this.schemaUtils.getSchemaType({ type: "number" }),
76-
)
73+
keyType.includes(this.schemaUtils.getSchemaType({ type: "number" }))
7774
) {
7875
return this.config.Ts.NumberValue(value);
7976
}
8077
if (
81-
lodash.includes(
82-
keyType,
83-
this.schemaUtils.getSchemaType({ type: "boolean" }),
84-
)
78+
keyType.includes(this.schemaUtils.getSchemaType({ type: "boolean" }))
8579
) {
8680
return this.config.Ts.BooleanValue(value);
8781
}
8882

8983
return this.config.Ts.StringValue(value);
9084
};
9185

92-
if (lodash.isArray(enumNames) && lodash.size(enumNames)) {
93-
content = lodash.map(enumNames, (enumName, index) => {
86+
if (Array.isArray(enumNames) && lodash.size(enumNames)) {
87+
content = enumNames.map((enumName, index) => {
9488
const enumValue = lodash.get(this.schema.enum, index);
9589
const formattedKey = this.formatEnumKey({
9690
key: enumName,
9791
value: enumValue,
9892
});
9993

100-
if (this.config.enumNamesAsValues || lodash.isUndefined(enumValue)) {
94+
if (this.config.enumNamesAsValues || enumValue === undefined) {
10195
return {
10296
key: formattedKey,
10397
type: this.config.Ts.Keyword.String,
@@ -112,7 +106,7 @@ class EnumSchemaParser extends MonoSchemaParser {
112106
};
113107
});
114108
} else {
115-
content = lodash.map(this.schema.enum, (value) => {
109+
content = this.schema.enum.map((value) => {
116110
return {
117111
key: this.formatEnumKey({ value }),
118112
type: keyType,
@@ -122,7 +116,7 @@ class EnumSchemaParser extends MonoSchemaParser {
122116
}
123117

124118
return {
125-
...(lodash.isObject(this.schema) ? this.schema : {}),
119+
...(typeof this.schema === "object" ? this.schema : {}),
126120
$ref: $ref,
127121
typeName: this.typeName || ($ref && refType.typeName) || null,
128122
$parsedSchema: true,

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ObjectSchemaParser extends MonoSchemaParser {
77
const contentProperties = this.getObjectSchemaContent(this.schema);
88

99
return {
10-
...(lodash.isObject(this.schema) ? this.schema : {}),
10+
...(typeof this.schema === "object" ? this.schema : {}),
1111
$schemaPath: this.schemaPath.slice(),
1212
$parsedSchema: true,
1313
schemaType: SCHEMA_TYPES.OBJECT,
@@ -17,10 +17,9 @@ class ObjectSchemaParser extends MonoSchemaParser {
1717
description: this.schemaFormatters.formatDescription(
1818
this.schema.description,
1919
),
20-
allFieldsAreOptional: !lodash.some(
21-
lodash.values(contentProperties),
22-
(part) => part.isRequired,
23-
),
20+
allFieldsAreOptional: !contentProperties
21+
.values()
22+
.some((part) => part.isRequired),
2423
content: contentProperties,
2524
};
2625
}

0 commit comments

Comments
 (0)