mirror of
https://github.com/google/blockly.git
synced 2025-12-16 06:10:12 +01:00
fix!: deepMerge for arrays, shortcut keycodes returned as array (#9047)
This commit is contained in:
@@ -9,6 +9,9 @@
|
||||
/**
|
||||
* Complete a deep merge of all members of a source object with a target object.
|
||||
*
|
||||
* N.B. This is not a very sophisticated merge algorithm and does not
|
||||
* handle complex cases. Use with caution.
|
||||
*
|
||||
* @param target Target.
|
||||
* @param source Source.
|
||||
* @returns The resulting object.
|
||||
@@ -18,7 +21,9 @@ export function deepMerge(
|
||||
source: AnyDuringMigration,
|
||||
): AnyDuringMigration {
|
||||
for (const x in source) {
|
||||
if (source[x] !== null && typeof source[x] === 'object') {
|
||||
if (source[x] !== null && Array.isArray(source[x])) {
|
||||
target[x] = deepMerge(target[x] || [], source[x]);
|
||||
} else if (source[x] !== null && typeof source[x] === 'object') {
|
||||
target[x] = deepMerge(target[x] || Object.create(null), source[x]);
|
||||
} else {
|
||||
target[x] = source[x];
|
||||
|
||||
@@ -260,7 +260,7 @@ suite('Keyboard Shortcut Registry Test', function () {
|
||||
assert.equal(this.registry.getKeyMap()['keyCode'][0], 'a');
|
||||
});
|
||||
test('Gets a copy of the registry', function () {
|
||||
const shortcut = {'name': 'shortcutName'};
|
||||
const shortcut = {'name': 'shortcutName', 'keyCodes': ['2', '4']};
|
||||
this.registry.register(shortcut);
|
||||
const registrycopy = this.registry.getRegistry();
|
||||
registrycopy['shortcutName']['name'] = 'shortcutName1';
|
||||
@@ -268,6 +268,10 @@ suite('Keyboard Shortcut Registry Test', function () {
|
||||
this.registry.getRegistry()['shortcutName']['name'],
|
||||
'shortcutName',
|
||||
);
|
||||
assert.deepEqual(
|
||||
this.registry.getRegistry()['shortcutName']['keyCodes'],
|
||||
shortcut['keyCodes'],
|
||||
);
|
||||
});
|
||||
test('Gets keyboard shortcuts from a key code', function () {
|
||||
this.registry.setKeyMap({'keyCode': ['shortcutName']});
|
||||
|
||||
@@ -533,4 +533,25 @@ suite('Utils', function () {
|
||||
assert.equal(Blockly.utils.math.toDegrees(5 * quarter), 360 + 90, '450');
|
||||
});
|
||||
});
|
||||
|
||||
suite('deepMerge', function () {
|
||||
test('Merges two objects', function () {
|
||||
const target = {a: 1, b: '2', shared: 'this should be overwritten'};
|
||||
const source = {c: {deeplyNested: true}, shared: 'I overwrote it'};
|
||||
|
||||
const expected = {...target, ...source};
|
||||
const actual = Blockly.utils.object.deepMerge(target, source);
|
||||
|
||||
assert.deepEqual(expected, actual);
|
||||
});
|
||||
test('Merges objects with arrays', function () {
|
||||
const target = {a: 1};
|
||||
const source = {b: ['orange', 'lime']};
|
||||
|
||||
const expected = {...target, ...source};
|
||||
const actual = Blockly.utils.object.deepMerge(target, source);
|
||||
|
||||
assert.deepEqual(expected, actual);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user