Skip to content
Snippets Groups Projects
Verified Commit 69718fe7 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix: reset config #136

parent d94697cf
Branches
Tags
No related merge requests found
...@@ -5,17 +5,15 @@ ...@@ -5,17 +5,15 @@
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
*/ */
import { isArray, isObject } from "../types/is.mjs"; import {isArray, isObject} from "../types/is.mjs";
import { typeOf } from "../types/typeof.mjs"; import {typeOf} from "../types/typeof.mjs";
export { extend }; export {extend};
/** /**
* Extend copies all enumerable own properties from one or * Extend copies all enumerable own properties from one or
* more source objects to a target object. It returns the modified target object. * more source objects to a target object. It returns the modified target object.
* *
* @param {object} target
* @param {object}
* @return {object} * @return {object}
* @license AGPLv3 * @license AGPLv3
* @since 1.10.0 * @since 1.10.0
...@@ -24,57 +22,68 @@ export { extend }; ...@@ -24,57 +22,68 @@ export { extend };
* @throws {Error} unsupported argument * @throws {Error} unsupported argument
* @throws {Error} type mismatch * @throws {Error} type mismatch
* @throws {Error} unsupported argument * @throws {Error} unsupported argument
* @param args
*/ */
function extend(...args) { function extend(...args) {
let o; let o;
let i; let i;
if (typeof args !== "object" || args[0] === null) { if (typeof args !== "object" || args[0] === null) {
throw new Error(`unsupported argument ${JSON.stringify(args[0])}`); throw new Error(`unsupported argument ${JSON.stringify(args[0])}`);
} }
for (i = 0; i < args.length; i++) { for (i = 0; i < args.length; i++) {
const a = args[i]; const a = args[i];
if (!(isObject(a) || isArray(a))) { if (!(isObject(a) || isArray(a))) {
throw new Error(`unsupported argument ${JSON.stringify(a)}`); throw new Error(`unsupported argument ${JSON.stringify(a)}`);
} }
if (o === undefined) { if (o === undefined) {
o = a; o = a;
continue; continue;
} }
for (const k in a) { for (const k in a) {
const v = a?.[k]; const v = a?.[k];
if (v === o?.[k]) { if (v === o?.[k]) {
continue; continue;
} }
if ((isObject(v) && typeOf(v) === "object") || isArray(v)) { if ((isObject(v) && typeOf(v) === "object") || isArray(v)) {
if (o[k] === undefined) { if (o[k] === undefined) {
if (isArray(v)) { if (isArray(v)) {
o[k] = []; o[k] = [];
} else { } else {
o[k] = {}; o[k] = {};
} }
} else { } else {
if (typeOf(o[k]) !== typeOf(v)) { if (typeOf(o[k]) !== typeOf(v)) {
throw new Error( throw new Error(
`type mismatch: ${JSON.stringify(o[k])}(${typeOf( `type mismatch: ${JSON.stringify(o[k])}(${typeOf(
o[k], o[k],
)}) != ${JSON.stringify(v)}(${typeOf(v)})`, )}) != ${JSON.stringify(v)}(${typeOf(v)})`,
); );
} }
} }
if (isArray(o[k])) {
o[k] = [];
o[k].push(...v);
continue;
}
o[k] = extend(o[k], v); o[k] = extend(o[k], v);
} else { } else {
o[k] = v; if (isArray(o)) {
} o.push(v);
} continue;
} }
o[k] = v;
}
}
}
return o; return o;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment