diff --git a/package.json b/package.json index b32285b3eded96d2d6f1502a61013fa58ed659b3..dfd17800494294f58722255e04d7fbf74ad2fcfa 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 77c3aa5578265f77b3942388e854a026f78072eb..b26a7f9e5ffeff5a4192d6923144f943859b1305 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]; };