test merging json objects
Signed-off-by: Oleg S <97077423+RobotSail@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
---
|
||||
'@backstage/backend-app-api': minor
|
||||
'@backstage/config-loader': minor
|
||||
'@backstage/types': minor
|
||||
---
|
||||
|
||||
Introduces the ability to merge configs and JsonObjects
|
||||
@@ -178,6 +178,7 @@ export const lifecycleServiceFactory: () => ServiceFactory<
|
||||
export function loadBackendConfig(options: {
|
||||
remote?: LoadConfigOptionsRemote;
|
||||
argv: string[];
|
||||
config?: JsonObject;
|
||||
}): Promise<{
|
||||
config: Config;
|
||||
}>;
|
||||
|
||||
@@ -29,6 +29,9 @@ export type JsonPrimitive = number | string | boolean | null;
|
||||
// @public
|
||||
export type JsonValue = JsonObject | JsonArray | JsonPrimitive;
|
||||
|
||||
// @public
|
||||
export const mergeJson: (a: JsonObject, b: JsonObject) => JsonObject;
|
||||
|
||||
// @public
|
||||
export type Observable<T> = {
|
||||
[Symbol.observable](): Observable<T>;
|
||||
|
||||
@@ -14,7 +14,13 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { JsonPrimitive, JsonArray, JsonObject, JsonValue } from './json';
|
||||
import {
|
||||
JsonPrimitive,
|
||||
JsonArray,
|
||||
JsonObject,
|
||||
JsonValue,
|
||||
mergeJson,
|
||||
} from './json';
|
||||
|
||||
describe('json', () => {
|
||||
it('JsonPrimitive', () => {
|
||||
@@ -79,3 +85,89 @@ describe('json', () => {
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('jsonMerge', () => {
|
||||
it('should merge two objects', () => {
|
||||
const obj1 = { a: 1, b: 2, c: 3 };
|
||||
const obj2 = { b: 4, c: 5, d: 6 };
|
||||
const merged = mergeJson(obj1, obj2);
|
||||
expect(merged).toEqual({ a: 1, b: 4, c: 5, d: 6 });
|
||||
});
|
||||
|
||||
it('should always prefer to merge the values of the second parameter', () => {
|
||||
const obj1 = {
|
||||
a: 1,
|
||||
b: [1, 2, 3],
|
||||
c: {
|
||||
z: 1,
|
||||
y: 2,
|
||||
x: 3,
|
||||
},
|
||||
};
|
||||
const obj2 = {
|
||||
a: 2,
|
||||
b: [2, 4, 6],
|
||||
c: {
|
||||
z: 2,
|
||||
y: 4,
|
||||
x: 6,
|
||||
},
|
||||
};
|
||||
const merged = mergeJson(obj1, obj2);
|
||||
expect(merged).toEqual(obj2);
|
||||
});
|
||||
|
||||
it('should prefer the second argument whenever keys collide', () => {
|
||||
const obj1 = {
|
||||
a: 1,
|
||||
b: [1, 2, 3],
|
||||
c: {
|
||||
z: 1,
|
||||
y: 2,
|
||||
x: 3,
|
||||
},
|
||||
};
|
||||
const obj2 = {
|
||||
a: 2,
|
||||
c: {
|
||||
y: 4,
|
||||
},
|
||||
};
|
||||
const merged = mergeJson(obj1, obj2);
|
||||
expect(merged).toEqual({
|
||||
a: 2,
|
||||
b: [1, 2, 3],
|
||||
c: {
|
||||
z: 1,
|
||||
y: 4,
|
||||
x: 3,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should merge recursively', () => {
|
||||
const obj1 = {
|
||||
backend: {
|
||||
database: {
|
||||
provider: 'sqlite3',
|
||||
},
|
||||
},
|
||||
};
|
||||
const obj2 = {
|
||||
backend: {
|
||||
database: {
|
||||
password: 'password123',
|
||||
},
|
||||
},
|
||||
};
|
||||
const merged = mergeJson(obj1, obj2);
|
||||
expect(merged).toEqual({
|
||||
backend: {
|
||||
database: {
|
||||
provider: 'sqlite3',
|
||||
password: 'password123',
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -47,9 +47,10 @@ export type JsonValue = JsonObject | JsonArray | JsonPrimitive;
|
||||
* prefers values from b, unless the value is an object, in which case it recursively
|
||||
* merges the values.
|
||||
*
|
||||
* @param a The base object
|
||||
* @param b The object to merge into a
|
||||
* @param a - The base object
|
||||
* @param b - The object to merge into a
|
||||
* @returns The merged object
|
||||
* @public
|
||||
*/
|
||||
export const mergeJson = (a: JsonObject, b: JsonObject): JsonObject => {
|
||||
const final: JsonObject = {};
|
||||
|
||||
Reference in New Issue
Block a user