From 9e46088077ba676649e2824a2167ad5b6e74fea7 Mon Sep 17 00:00:00 2001
From: Marin Karamihalev <marin.karamihalev@iopsys.eu>
Date: Wed, 26 Jan 2022 11:19:24 +0100
Subject: [PATCH] return empty array over empty object

---
 package.json        |  2 +-
 src/commands/get.ts | 13 +++++++++++--
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/package.json b/package.json
index b32285b..dfd1780 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "usp-js",
-  "version": "0.2.18",
+  "version": "0.2.19",
   "description": "Helper library for easy usp communication using mqtt over tcp or ws.",
   "main": "node/index.js",
   "browser": "web/index.js",
diff --git a/src/commands/get.ts b/src/commands/get.ts
index 77c3aa5..b26a7f9 100644
--- a/src/commands/get.ts
+++ b/src/commands/get.ts
@@ -1,16 +1,21 @@
 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) => {
   const resolvedPathResultsArr = util.searchAll(msg, "resolvedPathResults");
   const requestedPath = util.search(msg, "requestedPath");
   if (resolvedPathResultsArr) {
+    // path has search query (ex. Device.IP.Interface.[Name=="wan"].)
     const pathIncludesSearch =
       typeof requestedPath === "string" &&
       (requestedPath.includes("*") || requestedPath.includes("["));
 
     const pathSplit = requestedPath.split(".");
 
+    // found multiple items
     const hasMultipleIndexes = util.hasMultipleIndexes(
       resolvedPathResultsArr
         .map((it) =>
@@ -20,10 +25,14 @@ const decode: DecodeFn = (msg) => {
       pathSplit
     );
 
-    const shouldBeArray = pathIncludesSearch || hasMultipleIndexes || resolvedPathResultsArr.length > 1;
+    const shouldBeArray =
+      pathIncludesSearch ||
+      hasMultipleIndexes ||
+      resolvedPathResultsArr.length > 1;
     const unflattened = util.convertToNestedObject(resolvedPathResultsArr);
     const unwrapped = util.fullyUnwrapObject(unflattened, shouldBeArray);
-    return [unwrapped];
+
+    return [isEmpty(unwrapped) ? [] : unwrapped];
   }
   return [null];
 };
-- 
GitLab