deno.land / x / denon@2.5.0 / src / merge.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright 2020-2021 the denosaurs team. All rights reserved. MIT license.
// deno-lint-ignore-fileexport type Indexable = Record<string, any>;
/** Performs a deep merge of `source` into `target`. * Mutates `target` only but not its objects and arrays. */export function merge<T extends Record<string, any>>( target: T, source: Indexable,): T { const t = target as Record<string, unknown>; const isObject = (obj: unknown) => obj && typeof obj === "object";
if (!isObject(target) || !isObject(source)) { return source as T; }
for (const key of Object.keys(source)) { const targetValue = target[key]; const sourceValue = source[key];
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) { t[key] = sourceValue; } else if (isObject(targetValue) && isObject(sourceValue)) { t[key] = merge(Object.assign({}, targetValue), sourceValue as Indexable); } else { t[key] = sourceValue; } }
return t as T;}
denon

Version Info

Tagged at
2 years ago

External Dependencies

No external dependencies 🎉