From 3887126bedf716f2df0e65ca4afd27ca924f8621 Mon Sep 17 00:00:00 2001
From: Marin Karamihalev <marin.karamihalev@iopsys.eu>
Date: Wed, 7 Dec 2022 13:51:29 +0100
Subject: [PATCH] resolved query issue for get and getNested

---
 package.json                  |  2 +-
 src/commands/util.ts          | 33 ++++++++++++++++++---------------
 tests/integration/config.json |  2 +-
 3 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/package.json b/package.json
index 371670a..531aa06 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "usp-js",
-  "version": "0.3.5",
+  "version": "0.3.6",
   "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/util.ts b/src/commands/util.ts
index f1a2f2a..9ae5d6c 100644
--- a/src/commands/util.ts
+++ b/src/commands/util.ts
@@ -186,10 +186,13 @@ export const parseID = (msg: any) => {
 
 const grab = (item: any, key: string) =>
   Array.isArray(item)
-    ? item.find((val) => val !== null && val !== undefined)
+    ? isDigit(key)
+      ? item.find((val) => val !== null && val !== undefined)
+      : item
     : key === "-1"
     ? item.find((v) => v !== null)
     : item[key];
+
 const skipKeys = (obj: any, keys: string[]) =>
   keys.length === 1
     ? grab(obj, keys[0])
@@ -213,6 +216,13 @@ const removeNulls = (obj: any) =>
 const pathJoin = (a: string, b: string) =>
   a.endsWith(".") ? a + b : a + "." + b;
 
+const cutQueryElements = (path: string): string =>
+  path
+    .split(/\[.*\]./)
+    .join("")
+    .split(/\*\./)
+    .join("");
+
 const addQueries = (obj: any, path: string) =>
   typeof obj !== "object"
     ? obj
@@ -225,7 +235,7 @@ const addQueries = (obj: any, path: string) =>
         )
         .filter((v) => v !== null)
     : {
-        __query__: path,
+        __query__: cutQueryElements(path),
         ...Object.entries(obj).reduce(
           (acc, [key, val]) => ({
             ...acc,
@@ -238,13 +248,6 @@ const addQueries = (obj: any, path: string) =>
         ),
       };
 
-const locatePath = (result: PathResult): string =>
-  result.resolvedPathResults?.reduce((shortestRes, currentRes) =>
-    currentRes.resolvedPath.length < shortestRes.resolvedPath.length
-      ? currentRes
-      : shortestRes
-  ).resolvedPath || result.requestedPath;
-
 export const processGetResult = (
   reqPathResults: PathResult[],
   decodeOptions?: DecodeOptions
@@ -261,6 +264,7 @@ export const processGetResult = (
         ? result
         : ({ __query__: resolvedResult.requestedPath, result } as UspProperty);
     }
+
     if (respType === ResponseType.PropertyList) {
       const result = results.map(
         (result) => Object.values(result.resultParams)[0]
@@ -277,22 +281,21 @@ export const processGetResult = (
             })),
           } as UspPropertyList);
     }
+
     if (
       respType === ResponseType.Object ||
       respType === ResponseType.ObjectList
     ) {
-      const actualPath = containsQuery(resolvedResult.requestedPath)
-        ? locatePath(resolvedResult)
-        : resolvedResult.requestedPath;
-      const keys = actualPath.split(".").slice(0, -1);
+      const path = resolvedResult.requestedPath;
+      const keys = path.split(".").slice(0, -1);
       const nestedObject = convertToNestedObject(results);
       const mainObject = skipKeys(nestedObject, keys);
       const cleanedObject = removeNulls(mainObject);
       return !retainPath
         ? cleanedObject
         : {
-            __query__: resolvedResult.requestedPath,
-            result: addQueries(mainObject, actualPath),
+            __query__: path,
+            result: addQueries(mainObject, path),
           };
     }
 
diff --git a/tests/integration/config.json b/tests/integration/config.json
index a78c36f..43bab89 100644
--- a/tests/integration/config.json
+++ b/tests/integration/config.json
@@ -1,6 +1,6 @@
 {
   "ws": {
-    "host": "iopsys.lan",
+    "host": "localhost",
     "username": "admin",
     "password": "admin",
     "port": 9001,
-- 
GitLab