mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Prevent reduce calls
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 ---------------------------------
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user