diff --git a/src/commands/get.ts b/src/commands/get.ts index 2642e514068a1d47384c2b1b555c140277bbae4f..08ed6849b1518e674b38db646f6229ed68ba41a3 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 9faef7d90bbcb4ed3a309adb782ba9fb6f8de07f..69338e8e4949578edb3e5bc4b374012d0e29ce76 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;