Refactor to improve iterations

- Use function abstractions (such as map, reduce, filter etc.) over
  for-of loops to gain benefits of having less side effects and easier
  readability.
- Enable `downLevelIterations` for writing modern code with lazy evaluation.
- Refactor for of loops to named abstractions to clearly express their
  intentions without needing to analyse the loop itself.
- Add missing cases for changes that had no tests.
This commit is contained in:
undergroundwires
2022-01-04 21:45:22 +01:00
parent bd23faa28f
commit 31f70913a2
35 changed files with 342 additions and 343 deletions

View File

@@ -61,46 +61,27 @@ export class CategoryCollectionStub implements ICategoryCollection {
}
public getAllScripts(): ReadonlyArray<IScript> {
const scripts = [];
for (const category of this.actions) {
const categoryScripts = getScriptsRecursively(category);
scripts.push(...categoryScripts);
}
return scripts;
return this.actions.flatMap((category) => getScriptsRecursively(category));
}
public getAllCategories(): ReadonlyArray<ICategory> {
const categories = [];
categories.push(...this.actions);
for (const category of this.actions) {
const subCategories = getSubCategoriesRecursively(category);
categories.push(...subCategories);
}
return categories;
return this.actions.flatMap(
(category) => [category, ...getSubCategoriesRecursively(category)],
);
}
}
function getSubCategoriesRecursively(category: ICategory): ReadonlyArray<ICategory> {
const subCategories = [];
if (category.subCategories) {
for (const subCategory of category.subCategories) {
subCategories.push(subCategory);
subCategories.push(...getSubCategoriesRecursively(subCategory));
}
}
return subCategories;
return (category.subCategories || []).flatMap(
(subCategory) => [subCategory, ...getSubCategoriesRecursively(subCategory)],
);
}
function getScriptsRecursively(category: ICategory): ReadonlyArray<IScript> {
const categoryScripts = [];
if (category.scripts) {
categoryScripts.push(...category.scripts);
}
if (category.subCategories) {
for (const subCategory of category.subCategories) {
const subCategoryScripts = getScriptsRecursively(subCategory);
categoryScripts.push(...subCategoryScripts);
}
}
return categoryScripts;
return [
...(category.scripts || []),
...(category.subCategories || []).flatMap(
(subCategory) => getScriptsRecursively(subCategory),
),
];
}

View File

@@ -27,10 +27,9 @@ export class CategoryStub extends BaseEntity<number> implements ICategory {
}
public withScriptIds(...scriptIds: string[]): CategoryStub {
for (const scriptId of scriptIds) {
this.withScript(new ScriptStub(scriptId));
}
return this;
return this.withScripts(
...scriptIds.map((id) => new ScriptStub(id)),
);
}
public withScripts(...scripts: IScript[]): CategoryStub {

View File

@@ -58,12 +58,9 @@ function deepEqual(
if (!scrambledEqual(expectedParameterNames, actualParameterNames)) {
return false;
}
for (const parameterName of expectedParameterNames) {
return expectedParameterNames.every((parameterName) => {
const expectedValue = expected.getArgument(parameterName).argumentValue;
const actualValue = actual.getArgument(parameterName).argumentValue;
if (expectedValue !== actualValue) {
return false;
}
}
return true;
return expectedValue === actualValue;
});
}

View File

@@ -14,9 +14,8 @@ export class FunctionCallArgumentCollectionStub implements IFunctionCallArgument
}
public withArguments(args: { readonly [index: string]: string }) {
for (const parameterName of Object.keys(args)) {
const parameterValue = args[parameterName];
this.withArgument(parameterName, parameterValue);
for (const [name, value] of Object.entries(args)) {
this.withArgument(name, value);
}
return this;
}

View File

@@ -13,7 +13,7 @@ export class ScriptStub extends BaseEntity<string> implements IScript {
public readonly documentationUrls = new Array<string>();
public level = RecommendationLevel.Standard;
public level? = RecommendationLevel.Standard;
constructor(public readonly id: string) {
super(id);
@@ -23,7 +23,7 @@ export class ScriptStub extends BaseEntity<string> implements IScript {
return Boolean(this.code.revert);
}
public withLevel(value: RecommendationLevel): ScriptStub {
public withLevel(value?: RecommendationLevel): ScriptStub {
this.level = value;
return this;
}