diff options
| author | Vincent LE GOFF <g_n_s@hotmail.fr> | 2019-03-28 17:31:15 +0100 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2019-03-28 12:31:15 -0400 |
| commit | 13aeee460a62dc6b49de3a7d5d6305ea51ab5e4c (patch) | |
| tree | 0605d5e3e4b5e7536aa1ea2f7218fb73edded514 /util/deep_assign.ts | |
| parent | 8f0407efad81ea8c255daf03c0e19b6bae3b88b6 (diff) | |
Add TOML module (denoland/deno_std#300)
Original: https://github.com/denoland/deno_std/commit/fa1664e6ccaad9ad98a131f03fdd600c5fa24100
Diffstat (limited to 'util/deep_assign.ts')
| -rw-r--r-- | util/deep_assign.ts | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/util/deep_assign.ts b/util/deep_assign.ts new file mode 100644 index 000000000..4857e18b5 --- /dev/null +++ b/util/deep_assign.ts @@ -0,0 +1,28 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +export function deepAssign(target: object, ...sources: object[]): object { + for (let i = 0; i < sources.length; i++) { + const source = sources[i]; + if (!source || typeof source !== `object`) { + return; + } + Object.entries(source).forEach(([key, value]) => { + 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); + }); + } + return target; +} |
