forked from jonschlinkert/assign-deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (38 loc) · 1.12 KB
/
index.js
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
35
36
37
38
39
40
41
42
43
44
/*!
* assign-deep <https://github.com/jonschlinkert/assign-deep>
*
* Copyright (c) 2017-present, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
const toString = Object.prototype.toString;
const assignSymbols = require('assign-symbols');
const isValidKey = key => {
return key !== '__proto__' && key !== 'constructor' && key !== 'prototype';
};
const assign = module.exports = (target, ...args) => {
let i = 0;
if (isPrimitive(target)) target = args[i++];
if (!target) target = {};
for (; i < args.length; i++) {
if (isObject(args[i])) {
for (const key of Object.keys(args[i])) {
if (isValidKey(key)) {
if (isObject(target[key]) && isObject(args[i][key])) {
assign(target[key], args[i][key]);
} else {
target[key] = args[i][key];
}
}
}
assignSymbols(target, args[i]);
}
}
return target;
};
function isObject(val) {
return typeof val === 'function' || toString.call(val) === '[object Object]';
}
function isPrimitive(val) {
return typeof val === 'object' ? val === null : typeof val !== 'function';
}