From b4e258f4b282f9562fb2d365a4932e77a411b5b9 Mon Sep 17 00:00:00 2001 From: Marin Karamihalev <marin.karamihalev@iopsys.eu> Date: Tue, 23 Aug 2022 12:58:49 +0200 Subject: [PATCH] removed empty object arrays, WIP on bug 8452 --- src/commands/get.ts | 16 ++++++++++++---- src/commands/util.ts | 13 ++++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/commands/get.ts b/src/commands/get.ts index 2642e51..08ed684 100644 --- a/src/commands/get.ts +++ b/src/commands/get.ts @@ -1,9 +1,6 @@ import { DecodeFn, EncodeFn } from "../types"; import * as util from "./util"; -const isEmpty = (obj: any): boolean => - obj && typeof obj === "object" && Object.keys(obj).length === 0; - const decode: DecodeFn = (msg, decodeOptions) => { const resolvedPathResultsArr = util.searchAll(msg, "resolvedPathResults"); if (decodeOptions?.raw) return [resolvedPathResultsArr]; @@ -34,7 +31,18 @@ const decode: DecodeFn = (msg, decodeOptions) => { const unflattened = util.convertToNestedObject(resolvedPathResultsArr); const unwrapped = util.fullyUnwrapObject(unflattened, shouldBeArray); - return [isEmpty(unwrapped) ? [] : unwrapped]; + console.log({ + resolvedPathResultsArr, + requestedPath, + pathIncludesSearch, + pathSplit, + hasMultipleIndexes, + shouldBeArray, + unflattened, + unwrapped + }) + + return [util.isEmpty(unwrapped) ? [] : unwrapped]; } return [null]; }; diff --git a/src/commands/util.ts b/src/commands/util.ts index 9faef7d..69338e8 100644 --- a/src/commands/util.ts +++ b/src/commands/util.ts @@ -136,16 +136,23 @@ export const unwrapArray = (arr: any) => const isObject = (val: any): boolean => typeof val === "object" && val !== null && !Array.isArray(val); +export const isEmpty = (obj: any): boolean => + obj !== null && + obj !== undefined && + obj && + typeof obj === "object" && + Object.keys(obj).length === 0; + export const fullyUnwrapObject = (obj: any, shouldBeArray: boolean) => { if (isObject(obj) && Object.keys(obj).length === 1) return fullyUnwrapObject(Object.values(obj)[0], shouldBeArray); const isArray = Array.isArray(obj); if (shouldBeArray) - if (isArray) return obj; - else return [obj]; + if (isArray) return obj.filter((v) => !isEmpty(v)); + else return isEmpty(obj) ? [] : [obj]; else if (isArray) return fullyUnwrapObject( - obj.find((v) => v !== null && v !== undefined), + obj.find((v) => !isEmpty(v)), shouldBeArray ); else return obj; -- GitLab