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 * 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];
};
......
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment