Skip to content

Commit

Permalink
fix(services/utils): handle null items filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
moki committed Nov 3, 2022
1 parent 60735c4 commit e5d14b5
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,31 @@ export function filterFiles<T extends Filter>(
options?: FilterFilesOptions,
): T[] {
if (!Array.isArray(items)) {
const errorMessage
= `Error while filtering: item has invalid key '${itemsKey}' equals ${JSON.stringify(items)}`;
throw new Error(errorMessage);
return [];
}

return items.reduce((result: T[], item: T) => {
const useItem = shouldProcessItem(item, vars, options);

if (useItem) {
const property = item[itemsKey] as T[] | undefined;
const reducer = (results: T[], item: T) => {
if (shouldProcessItem(item, vars, options)) {
const prop = item[itemsKey] as T[];

if (property === undefined) {
result.push(item);
} else {
const filteredProperty = filterFiles(property, itemsKey, vars, options);
if (prop) {
const filteredProperty = filterFiles(prop, itemsKey, vars, options);

if (filteredProperty.length !== 0) {
result.push({
if (filteredProperty.length) {
results.push({
...item,
[itemsKey]: filteredProperty,
});
}
} else {
results.push(item);
}
}

return result;
}, []);
return results;
};

return items.reduce(reducer, []);
}

export function filterTextItems(
Expand Down

0 comments on commit e5d14b5

Please sign in to comment.