-
Notifications
You must be signed in to change notification settings - Fork 1
/
extends.js
112 lines (97 loc) · 4.42 KB
/
extends.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /*
* extend object or array in deep or shallow
* */
exports.default = extend;
var _invariant = require('invariant');
var _invariant2 = _interopRequireDefault(_invariant);
var _clone = require('clone');
var _clone2 = _interopRequireDefault(_clone);
var _isUnscalable = require('./isUnscalable');
var _isUnscalable2 = _interopRequireDefault(_isUnscalable);
var _types = require('./types');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
* handle extends
* */
function handle(options, target, nextSource) {
if ((0, _isUnscalable2.default)(nextSource)) {
return target;
}
var deep = options.deep;
if ((0, _types.isArray)(target) && (0, _types.isArray)(nextSource)) {
if (!options.arrayConcat) {
nextSource.forEach(function (element, index) {
target[index] = deep ? (0, _clone2.default)(element) : element;
});
} else {
nextSource.forEach(function (element) {
target.push(deep ? (0, _clone2.default)(element) : element);
});
}
} else if ((0, _types.isSet)(target) && (0, _types.isSet)(nextSource)) {
nextSource.forEach(function (element) {
target.add(deep ? (0, _clone2.default)(element) : element);
});
} else if ((0, _types.isMap)(target) && (0, _types.isMap)(nextSource)) {
nextSource.forEach(function (element, key) {
target.set(key, deep ? (0, _clone2.default)(element) : element);
});
} else if ((0, _types.isDate)(nextSource)) {
if ((0, _types.isDate)(target)) {
target.setTime(nextSource.getTime());
} else {
target = new Date().setTime(nextSource.getTime());
}
} else if ((0, _types.isRegExp)(nextSource)) {
target = (0, _clone2.default)(nextSource);
} else if ((0, _types.isObject)(target)) {
var value;
for (var nextKey in nextSource) {
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
value = nextSource[nextKey];
if (!deep || (0, _isUnscalable2.default)(value)) {
target[nextKey] = value;
} else {
var next = true;
if ((0, _types.isDate)(value)) {
target[nextKey] = new Date();
} else if ((0, _types.isRegExp)(value)) {
target[nextKey] = (0, _clone2.default)(value);
next = false;
} else if ((0, _types.isNullUndefined)(target[nextKey])) {
if ((0, _types.isArray)(value)) {
target[nextKey] = [];
} else if ((0, _types.isSet)(value)) {
target[nextKey] = new Set();
} else if ((0, _types.isMap)(value)) {
target[nextKey] = new Map();
} else {
target[nextKey] = {};
}
}
next && handle({
arrayConcat: options.arrayConcat,
depth: options.depth - 1,
deep: options.deep && options.depth !== 1
}, target[nextKey], value, target, nextKey);
}
}
}
}
return target;
}
/*
* extend object or array in deep or shallow
* */
function extend(options, target, nextSource) {
if (process.env.NODE_ENV !== 'production') {
(0, _invariant2.default)(target !== null || target !== undefined, 'target value can\'t be ' + String(target).toString());
(0, _invariant2.default)(!(0, _types.isNumber)(target) || !(0, _types.isString)(target) || !(0, _types.isBoolean)(target), 'target value type can\'t be ' + (typeof target === 'undefined' ? 'undefined' : _typeof(target)));
(0, _invariant2.default)(!!options.depth, 'assign depth can\'t be 0!');
}
return handle(options, target, nextSource);
}