Skip to content
Snippets Groups Projects
Commit b4e258f4 authored by Marin Karamihalev's avatar Marin Karamihalev
Browse files

removed empty object arrays, WIP on bug 8452

parent 184eed4b
Branches
Tags
No related merge requests found
import { DecodeFn, EncodeFn } from "../types"; import { DecodeFn, EncodeFn } from "../types";
import * as util from "./util"; 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 decode: DecodeFn = (msg, decodeOptions) => {
const resolvedPathResultsArr = util.searchAll(msg, "resolvedPathResults"); const resolvedPathResultsArr = util.searchAll(msg, "resolvedPathResults");
if (decodeOptions?.raw) return [resolvedPathResultsArr]; if (decodeOptions?.raw) return [resolvedPathResultsArr];
...@@ -34,7 +31,18 @@ const decode: DecodeFn = (msg, decodeOptions) => { ...@@ -34,7 +31,18 @@ const decode: DecodeFn = (msg, decodeOptions) => {
const unflattened = util.convertToNestedObject(resolvedPathResultsArr); const unflattened = util.convertToNestedObject(resolvedPathResultsArr);
const unwrapped = util.fullyUnwrapObject(unflattened, shouldBeArray); 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]; return [null];
}; };
......
...@@ -136,16 +136,23 @@ export const unwrapArray = (arr: any) => ...@@ -136,16 +136,23 @@ export const unwrapArray = (arr: any) =>
const isObject = (val: any): boolean => const isObject = (val: any): boolean =>
typeof val === "object" && val !== null && !Array.isArray(val); 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) => { export const fullyUnwrapObject = (obj: any, shouldBeArray: boolean) => {
if (isObject(obj) && Object.keys(obj).length === 1) if (isObject(obj) && Object.keys(obj).length === 1)
return fullyUnwrapObject(Object.values(obj)[0], shouldBeArray); return fullyUnwrapObject(Object.values(obj)[0], shouldBeArray);
const isArray = Array.isArray(obj); const isArray = Array.isArray(obj);
if (shouldBeArray) if (shouldBeArray)
if (isArray) return obj; if (isArray) return obj.filter((v) => !isEmpty(v));
else return [obj]; else return isEmpty(obj) ? [] : [obj];
else if (isArray) else if (isArray)
return fullyUnwrapObject( return fullyUnwrapObject(
obj.find((v) => v !== null && v !== undefined), obj.find((v) => !isEmpty(v)),
shouldBeArray shouldBeArray
); );
else return obj; else return obj;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment