Fix merge behaviour when the value is null

This commit is contained in:
Pieter Vander Vennet 2021-07-27 19:35:43 +02:00
parent 9248f264f1
commit 7352a4a41d
2 changed files with 22 additions and 6 deletions

View file

@ -135,7 +135,7 @@ export class Utils {
} }
return newArr; return newArr;
} }
public static MergeTags(a: any, b: any) { public static MergeTags(a: any, b: any) {
const t = {}; const t = {};
for (const k in a) { for (const k in a) {
@ -210,7 +210,9 @@ export class Utils {
if (sourceV?.length !== undefined && targetV?.length !== undefined && key.startsWith("+")) { if (sourceV?.length !== undefined && targetV?.length !== undefined && key.startsWith("+")) {
target[key] = targetV.concat(sourceV) target[key] = targetV.concat(sourceV)
} else if (typeof sourceV === "object") { } else if (typeof sourceV === "object") {
if (targetV === undefined) { if (sourceV === null) {
target[key] = null
} else if (targetV === undefined) {
target[key] = sourceV; target[key] = sourceV;
} else { } else {
Utils.Merge(sourceV, targetV); Utils.Merge(sourceV, targetV);
@ -362,9 +364,11 @@ export class Utils {
public static offerContentsAsDownloadableFile(contents: string | Blob, fileName: string = "download.txt") { public static offerContentsAsDownloadableFile(contents: string | Blob, fileName: string = "download.txt") {
const element = document.createElement("a"); const element = document.createElement("a");
let file; let file;
if(typeof(contents) === "string"){ if (typeof (contents) === "string") {
file = new Blob([contents], {type: 'text/plain'}); file = new Blob([contents], {type: 'text/plain'});
}else {file = contents;} } else {
file = contents;
}
element.href = URL.createObjectURL(file); element.href = URL.createObjectURL(file);
element.download = fileName; element.download = fileName;
document.body.appendChild(element); // Required for this to work in FireFox document.body.appendChild(element); // Required for this to work in FireFox
@ -452,8 +456,8 @@ export class Utils {
} }
} }
public static setDefaults(options, defaults){ public static setDefaults(options, defaults) {
for (let key in defaults){ for (let key in defaults) {
if (!(key in options)) options[key] = defaults[key]; if (!(key in options)) options[key] = defaults[key];
} }
return options; return options;

View file

@ -2,6 +2,8 @@ import T from "./TestHelper";
import {Utils} from "../Utils"; import {Utils} from "../Utils";
import {equal} from "assert"; import {equal} from "assert";
import LZString from "lz-string"; import LZString from "lz-string";
import * as Assert from "assert";
import * as assert from "assert";
export default class UtilsSpec extends T { export default class UtilsSpec extends T {
private static readonly example = { private static readonly example = {
@ -104,6 +106,16 @@ export default class UtilsSpec extends T {
equal(result.list1[1], "appended") equal(result.list1[1], "appended")
equal(result.list2.length, 1) equal(result.list2.length, 1)
equal(result.list2[0], "should-be-untouched") equal(result.list2[0], "should-be-untouched")
}],
["Test merge with null", () => {
const obj = {
someValue: 42
}
const override = {
someValue: null
}
Utils.Merge(override, obj)
equal(obj.someValue, null)
}] }]
]); ]);
} }