From 151ce0266eb4de2c8fc600c81c192a5f791b6169 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 9 Oct 2019 17:10:09 -0400 Subject: Move everything into std subdir --- std/util/deep_assign.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 std/util/deep_assign.ts (limited to 'std/util/deep_assign.ts') diff --git a/std/util/deep_assign.ts b/std/util/deep_assign.ts new file mode 100644 index 000000000..b1c9f9ac9 --- /dev/null +++ b/std/util/deep_assign.ts @@ -0,0 +1,31 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +export function deepAssign( + target: Record, + ...sources: object[] +): object | undefined { + for (let i = 0; i < sources.length; i++) { + const source = sources[i]; + if (!source || typeof source !== `object`) { + return; + } + Object.entries(source).forEach(([key, value]: [string, unknown]): 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] as Record, value!); + }); + } + return target; +} -- cgit v1.2.3