From e5d14b5b7fac64ccbc618dd6c1eef5ea2a4429fd Mon Sep 17 00:00:00 2001 From: moki-codes Date: Wed, 2 Nov 2022 20:41:28 +0400 Subject: [PATCH] fix(services/utils): handle null items filtering --- src/services/utils.ts | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/services/utils.ts b/src/services/utils.ts index 2e734de8..41cb0120 100644 --- a/src/services/utils.ts +++ b/src/services/utils.ts @@ -23,33 +23,31 @@ export function filterFiles( 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(