Skip to content

Commit

Permalink
pendo-sdk/commit for staging branch-added function in utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivam4415 committed Nov 26, 2020
1 parent 5a95500 commit 0c3b385
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,39 @@ function getUserProvidedConfigUrl(configUrl) {
return url;
}

/* ------- Start FlattenJson -----------
* This function flatten given json object to single level.
* So if there is nested object or array, all will apear in first level properties of an object.
* Following is case we are handling in this function ::
* condition 1: String
* condition 2: Array
* condition 3: Nested object
*/
function recurse(cur, prop, result) {
const res = result;
if (Object(cur) !== cur) {
res[prop] = cur;
} else if (Array.isArray(cur)) {
const l = cur.length;
for (let i = 0; i < l; i += 1)
recurse(cur[i], prop ? `${prop}.${i}` : `${i}`, res);
if (l === 0) res[prop] = [];
} else {
let isEmpty = true;
Object.keys(cur).forEach((key) => {
isEmpty = false;
recurse(cur[key], prop ? `${prop}.${key}` : key, res);
});
if (isEmpty) res[prop] = {};
}
return res;
}

function flattenJsonPayload(data) {
return recurse(data, "", {});
}
/* ------- End FlattenJson ----------- */

export {
replacer,
generateUUID,
Expand All @@ -434,5 +467,6 @@ export {
transformToServerNames,
handleError,
rejectArr,
type
type,
flattenJsonPayload
};

0 comments on commit 0c3b385

Please sign in to comment.