Skip to content

Commit

Permalink
support replacing values on merge (#6875)
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys Vuika authored Mar 30, 2021
1 parent ab4ab2f commit ff8e40d
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 2 deletions.
109 changes: 109 additions & 0 deletions lib/extensions/src/lib/config/extension-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { mergeObjects } from './extension-utils';

// tslint:disable-next-line:ban
fdescribe('Extension Utils', () => {
describe('mergeObjects', () => {
it('should merge two objects', () => {
const obj1 = { aHello: 1 };
const obj2 = { bWorld: 2 };

const result = mergeObjects(obj1, obj2);

expect(result).toEqual({
aHello: 1,
bWorld: 2
});
});

it('should merge three objects', () => {
const obj1 = { aHello: 1 };
const obj2 = { bWorld: 2 };
const obj3 = { aHello: 3 };

const result = mergeObjects(obj1, obj2, obj3);

expect(result).toEqual({
aHello: 3,
bWorld: 2
});
});

it('should not process special properties starting with $', () => {
const obj1 = { '$id': 'uid', aHello: 1 };
const obj2 = { '$schema': 'schema-id', bWorld: 2 };

const result = mergeObjects(obj1, obj2);

expect(result).toEqual({
aHello: 1,
bWorld: 2
});
});

it('should merge arrays', () => {
const obj1 = { values: ['one', 'two'] };
const obj2 = { values: ['three', 'four'] };

const result = mergeObjects(obj1, obj2);

expect(result).toEqual({
values: ['one', 'two', 'three', 'four']
});
});

it('should replace array', () => {
const obj1 = { values: ['one', 'two'] };
const obj2 = { 'values.$replace': ['three', 'four'] };

const result = mergeObjects(obj1, obj2);

expect(result).toEqual({
values: ['three', 'four']
});
});

it('should replace objects', () => {
const obj1 = { values: { tag: 'test' } };
const obj2 = { 'values.$replace': { hello: 'world' } };

const result = mergeObjects(obj1, obj2);

expect(result).toEqual({
values: { hello: 'world' }
});
});

it('should replace nested objects', () => {
const obj1 = { level1: { level2: { name: 'level2' } } };
const obj2 = { level1: { 'level2.$replace': { name: 'modified', tag: 'node' } } };

const result = mergeObjects(obj1, obj2);

expect(result).toEqual({
level1: {
level2: {
name: 'modified',
tag: 'node'
}
}
});
});
});
});
12 changes: 10 additions & 2 deletions lib/extensions/src/lib/config/extension-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,17 @@ export function mergeObjects(...objects: object[]): any {

objects.forEach((source) => {
Object.keys(source).forEach((prop) => {
let replace = false;

if (prop.endsWith('.$replace')) {
replace = true;
prop = prop.replace('.$replace', '');
}

if (!prop.startsWith('$')) {
if (prop in result && Array.isArray(result[prop])) {
// result[prop] = result[prop].concat(source[prop]);
if (replace) {
result[prop] = source[`${prop}.$replace`];
} else if (prop in result && Array.isArray(result[prop])) {
result[prop] = mergeArrays(result[prop], source[prop]);
} else if (prop in result && typeof result[prop] === 'object') {
result[prop] = mergeObjects(result[prop], source[prop]);
Expand Down

0 comments on commit ff8e40d

Please sign in to comment.