diff options
Diffstat (limited to 'util')
| -rw-r--r-- | util/deep_assign.ts | 36 | ||||
| -rw-r--r-- | util/deep_assign_test.ts | 2 |
2 files changed, 20 insertions, 18 deletions
diff --git a/util/deep_assign.ts b/util/deep_assign.ts index 4857e18b5..77bf5e9b3 100644 --- a/util/deep_assign.ts +++ b/util/deep_assign.ts @@ -5,24 +5,26 @@ export function deepAssign(target: object, ...sources: object[]): object { if (!source || typeof source !== `object`) { return; } - Object.entries(source).forEach(([key, value]) => { - if (value instanceof Date) { - target[key] = new Date(value); - return; + Object.entries(source).forEach( + ([key, value]): void => { + if (value instanceof Date) { + target[key] = new Date(value); + return; + } + if (!value || typeof value !== `object`) { + target[key] = value; + return; + } + if (Array.isArray(value)) { + target[key] = []; + } + // value is an Object + if (typeof target[key] !== `object` || !target[key]) { + target[key] = {}; + } + deepAssign(target[key], value); } - if (!value || typeof value !== `object`) { - target[key] = value; - return; - } - if (Array.isArray(value)) { - target[key] = []; - } - // value is an Object - if (typeof target[key] !== `object` || !target[key]) { - target[key] = {}; - } - deepAssign(target[key], value); - }); + ); } return target; } diff --git a/util/deep_assign_test.ts b/util/deep_assign_test.ts index 36be979d8..c197344f7 100644 --- a/util/deep_assign_test.ts +++ b/util/deep_assign_test.ts @@ -3,7 +3,7 @@ import { test } from "../testing/mod.ts"; import { assertEquals, assert } from "../testing/asserts.ts"; import { deepAssign } from "./deep_assign.ts"; -test(function deepAssignTest() { +test(function deepAssignTest(): void { const date = new Date("1979-05-27T07:32:00Z"); const reg = RegExp(/DENOWOWO/); const obj1 = { deno: { bar: { deno: ["is", "not", "node"] } } }; |
