From d4a4efc515894169f0018a39400ef6ec954da5e4 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 16 Aug 2019 14:19:25 -0700 Subject: [PATCH] Tweak keysource alg for more robust recursion --- packages/insomnia-app/app/common/render.js | 30 ++++++++++++---------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/insomnia-app/app/common/render.js b/packages/insomnia-app/app/common/render.js index f5df8f9e2..4a02d424a 100644 --- a/packages/insomnia-app/app/common/render.js +++ b/packages/insomnia-app/app/common/render.js @@ -236,24 +236,26 @@ export async function getRenderContext( ); const subEnvironment = await models.environment.getById(environmentId || 'n/a'); - let keySource = {}; + const keySource = {}; + // Function that gets Keys and stores their Source location function getKeySource(subObject, inKey, inSource) { - for (const key of Object.keys(subObject)) { - if (Object.prototype.toString.call(subObject[key]) === '[object Object]') { - // Type is an Object, keep on going, recursively building the full key path - getKeySource(subObject[key], inKey + key + '.', inSource); - } else if (Object.prototype.toString.call(subObject[key]) === '[object Array]') { - // Type is an Array, Loop and store the full Key and Source in keySource - for (let i = 0, length = subObject[key].length; i < length; i++) { - keySource[inKey + key + '[' + i + ']'] = inSource; - } - } else { - // For all other types, store the full Key and Source in keySource - keySource[inKey + key] = inSource; + // Add key to map if it's not root + if (inKey) { + keySource[inKey] = inSource; + } + + // Recurse down for Objects and Arrays + const typeStr = Object.prototype.toString.call(subObject); + if (typeStr === '[object Object]') { + for (const key of Object.keys(subObject)) { + getKeySource(subObject[key], inKey ? `${inKey}.${key}` : key, inSource); + } + } else if (typeStr === '[object Array]') { + for (let i = 0; i < subObject.length; i++) { + getKeySource(subObject[i], `${inKey}[${i}]`, inSource); } } - return keySource; } // Get Keys from root environment