Skip to content

Commit 205ef6c

Browse files
authored
fix!: deepMerge for arrays, shortcut keycodes returned as array (#9047)
1 parent 523dca9 commit 205ef6c

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

core/utils/object.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
/**
1010
* Complete a deep merge of all members of a source object with a target object.
1111
*
12+
* N.B. This is not a very sophisticated merge algorithm and does not
13+
* handle complex cases. Use with caution.
14+
*
1215
* @param target Target.
1316
* @param source Source.
1417
* @returns The resulting object.
@@ -18,7 +21,9 @@ export function deepMerge(
1821
source: AnyDuringMigration,
1922
): AnyDuringMigration {
2023
for (const x in source) {
21-
if (source[x] !== null && typeof source[x] === 'object') {
24+
if (source[x] !== null && Array.isArray(source[x])) {
25+
target[x] = deepMerge(target[x] || [], source[x]);
26+
} else if (source[x] !== null && typeof source[x] === 'object') {
2227
target[x] = deepMerge(target[x] || Object.create(null), source[x]);
2328
} else {
2429
target[x] = source[x];

tests/mocha/shortcut_registry_test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,18 @@ suite('Keyboard Shortcut Registry Test', function () {
260260
assert.equal(this.registry.getKeyMap()['keyCode'][0], 'a');
261261
});
262262
test('Gets a copy of the registry', function () {
263-
const shortcut = {'name': 'shortcutName'};
263+
const shortcut = {'name': 'shortcutName', 'keyCodes': ['2', '4']};
264264
this.registry.register(shortcut);
265265
const registrycopy = this.registry.getRegistry();
266266
registrycopy['shortcutName']['name'] = 'shortcutName1';
267267
assert.equal(
268268
this.registry.getRegistry()['shortcutName']['name'],
269269
'shortcutName',
270270
);
271+
assert.deepEqual(
272+
this.registry.getRegistry()['shortcutName']['keyCodes'],
273+
shortcut['keyCodes'],
274+
);
271275
});
272276
test('Gets keyboard shortcuts from a key code', function () {
273277
this.registry.setKeyMap({'keyCode': ['shortcutName']});

tests/mocha/utils_test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,25 @@ suite('Utils', function () {
533533
assert.equal(Blockly.utils.math.toDegrees(5 * quarter), 360 + 90, '450');
534534
});
535535
});
536+
537+
suite('deepMerge', function () {
538+
test('Merges two objects', function () {
539+
const target = {a: 1, b: '2', shared: 'this should be overwritten'};
540+
const source = {c: {deeplyNested: true}, shared: 'I overwrote it'};
541+
542+
const expected = {...target, ...source};
543+
const actual = Blockly.utils.object.deepMerge(target, source);
544+
545+
assert.deepEqual(expected, actual);
546+
});
547+
test('Merges objects with arrays', function () {
548+
const target = {a: 1};
549+
const source = {b: ['orange', 'lime']};
550+
551+
const expected = {...target, ...source};
552+
const actual = Blockly.utils.object.deepMerge(target, source);
553+
554+
assert.deepEqual(expected, actual);
555+
});
556+
});
536557
});

0 commit comments

Comments
 (0)