From 989310c29a0f960b4601a8cc21cfae4c2cc2e45d Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 10 Dec 2021 15:27:04 +0000 Subject: [PATCH] Update deepSet helper to create parent keys of a deep path if one does not exist --- packages/bbui/src/utils/helpers.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/bbui/src/utils/helpers.js b/packages/bbui/src/utils/helpers.js index f65e60416f..6cf432f356 100644 --- a/packages/bbui/src/utils/helpers.js +++ b/packages/bbui/src/utils/helpers.js @@ -37,6 +37,8 @@ export const deepGet = (obj, key) => { * Exact matches of keys with dots in them take precedence over nested keys of * the same path - e.g. setting "a.b" of { "a.b": "foo", a: { b: "bar" } } * will override the value "foo" rather than "bar". + * If a deep path is specified and the parent keys don't exist then these will + * be created. * @param obj the object * @param key the key * @param value the value @@ -51,7 +53,14 @@ export const deepSet = (obj, key, value) => { } const split = key.split(".") for (let i = 0; i < split.length - 1; i++) { - obj = obj?.[split[i]] + const nextKey = split[i] + if (obj && obj[nextKey] == null) { + obj[nextKey] = {} + } + obj = obj?.[nextKey] + } + if (!obj) { + return } obj[split[split.length - 1]] = value }