refactor: Prevent reduce calls

This commit is contained in:
Joachim Van Herwegen
2023-10-30 16:53:38 +01:00
parent 990184dbb5
commit 20d4a0c3af
12 changed files with 84 additions and 75 deletions

View File

@@ -188,13 +188,13 @@ export class ChainedConverter extends RepresentationConverter {
* Will return undefined if there are no matches.
*/
private findBest(paths: ConversionPath[]): ConversionPath | undefined {
// Need to use null instead of undefined so `reduce` doesn't take the first element of the array as `best`
return paths.reduce((best: ConversionPath | null, path): ConversionPath | null => {
let best: ConversionPath | undefined;
for (const path of paths) {
if (path.weight > 0 && !(best && best.weight >= path.weight)) {
return path;
best = path;
}
return best;
}, null) ?? undefined;
}
return best;
}
/**

View File

@@ -113,20 +113,20 @@ export function getWeightedPreferences(types: ValuePreferences, preferred: Value
* Undefined if there is no match.
*/
export function getBestPreference(types: ValuePreferences, preferred: ValuePreferences): ValuePreference | undefined {
// Could also return the first entry of the above function but this is more efficient
const result = Object.entries(types).reduce((best, [ value, quality ]): ValuePreference => {
// Could also return the first entry of `getWeightedPreferences` but this is more efficient
let best: ValuePreference = { value: '', weight: 0 };
for (const [ value, quality ] of Object.entries(types)) {
if (best.weight >= quality) {
return best;
continue;
}
const weight = quality * getTypeWeight(value, preferred);
if (weight > best.weight) {
return { value, weight };
best = { value, weight };
}
return best;
}, { value: '', weight: 0 });
}
if (result.weight > 0) {
return result;
if (best.weight > 0) {
return best;
}
}

View File

@@ -526,7 +526,11 @@ export class WrappedIndexedStorage<T extends IndexTypeCollection<T>> implements
indexResults.push(rootIds);
}
return indexResults.reduce((acc, ids): string[] => acc.filter((id): boolean => ids.includes(id)));
let indexedRoots: string[] = indexResults[0];
for (const ids of indexResults.slice(1)) {
indexedRoots = indexedRoots.filter((id): boolean => ids.includes(id));
}
return indexedRoots;
}
/**
@@ -581,9 +585,13 @@ export class WrappedIndexedStorage<T extends IndexTypeCollection<T>> implements
}
// For all keys that were not handled recursively: make sure that it matches the found objects
const remainingKeys = Object.keys(query).filter((key): boolean =>
key !== relation?.child.key || typeof query[key] === 'string');
return remainingKeys.reduce((acc, key): any[] => acc.filter((obj): boolean => obj[key] === query[key]), objs);
const remainingKeys = Object.keys(query).filter(
(key): boolean => key !== relation?.child.key || typeof query[key] === 'string',
);
for (const key of remainingKeys) {
objs = objs.filter((obj): boolean => obj[key] === query[key]);
}
return objs;
}
// --------------------------------- INDEX HELPERS ---------------------------------

View File

@@ -71,17 +71,16 @@ export class FileSizeReporter implements SizeReporter<string> {
const childFiles = await fsPromises.readdir(fileLocation);
const rootFilePathLength = trimTrailingSlashes(this.rootFilePath).length;
return await childFiles.reduce(async(acc: Promise<number>, current): Promise<number> => {
let totalSize = stat.size;
for (const current of childFiles) {
const childFileLocation = normalizeFilePath(joinFilePath(fileLocation, current));
let result = await acc;
// Exclude internal files
if (!this.ignoreFolders.some((folder: RegExp): boolean =>
folder.test(childFileLocation.slice(rootFilePathLength)))) {
result += await this.getTotalSize(childFileLocation);
totalSize += await this.getTotalSize(childFileLocation);
}
return result;
}, Promise.resolve(stat.size));
}
return totalSize;
}
}