Skip to content

Commit

Permalink
fix: 🐛 setDefaultValueForUndefined clone defined value
Browse files Browse the repository at this point in the history
  • Loading branch information
mjancarik committed Sep 27, 2024
1 parent 18a68e5 commit 4702ae4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/core/src/__tests__/utilsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ describe('utils function', () => {

expect(original).toEqual({});
});

it('should create copy of default value', () => {
const newObject = setDefaultValueForUndefined({}, ['a', 'b'], {});

expect(newObject.a !== newObject.b).toBeTruthy();
});
});

describe('bindWidgetToFunctions', () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ function createBoundedFunction(widget, originalFunction) {
};
}

// @TODO in future use structuredClone instead of JSON.parse
function clone(object) {
try {
return JSON.parse(JSON.stringify(object));
} catch (_) {
return object;
}
}

export function setDefaultValueForUndefined(object, keys, value = {}) {
let objectClone = { ...object };

keys.forEach((key) => {
objectClone[key] = objectClone[key] || value;
objectClone[key] = objectClone[key] || clone(value);
});

return objectClone;
Expand Down

0 comments on commit 4702ae4

Please sign in to comment.