1 | import _typeof from "@babel/runtime/helpers/esm/typeof";
|
2 | import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
3 | import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
4 | import _toArray from "@babel/runtime/helpers/esm/toArray";
|
5 | import get from "./get";
|
6 | function internalSet(entity, paths, value, removeIfUndefined) {
|
7 | if (!paths.length) {
|
8 | return value;
|
9 | }
|
10 | var _paths = _toArray(paths),
|
11 | path = _paths[0],
|
12 | restPath = _paths.slice(1);
|
13 | var clone;
|
14 | if (!entity && typeof path === 'number') {
|
15 | clone = [];
|
16 | } else if (Array.isArray(entity)) {
|
17 | clone = _toConsumableArray(entity);
|
18 | } else {
|
19 | clone = _objectSpread({}, entity);
|
20 | }
|
21 |
|
22 |
|
23 | if (removeIfUndefined && value === undefined && restPath.length === 1) {
|
24 | delete clone[path][restPath[0]];
|
25 | } else {
|
26 | clone[path] = internalSet(clone[path], restPath, value, removeIfUndefined);
|
27 | }
|
28 | return clone;
|
29 | }
|
30 | export default function set(entity, paths, value) {
|
31 | var removeIfUndefined = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
32 |
|
33 | if (paths.length && removeIfUndefined && value === undefined && !get(entity, paths.slice(0, -1))) {
|
34 | return entity;
|
35 | }
|
36 | return internalSet(entity, paths, value, removeIfUndefined);
|
37 | }
|
38 | function isObject(obj) {
|
39 | return _typeof(obj) === 'object' && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;
|
40 | }
|
41 | function createEmpty(source) {
|
42 | return Array.isArray(source) ? [] : {};
|
43 | }
|
44 | var keys = typeof Reflect === 'undefined' ? Object.keys : Reflect.ownKeys;
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | export function merge() {
|
50 | for (var _len = arguments.length, sources = new Array(_len), _key = 0; _key < _len; _key++) {
|
51 | sources[_key] = arguments[_key];
|
52 | }
|
53 | var clone = createEmpty(sources[0]);
|
54 | sources.forEach(function (src) {
|
55 | function internalMerge(path, parentLoopSet) {
|
56 | var loopSet = new Set(parentLoopSet);
|
57 | var value = get(src, path);
|
58 | var isArr = Array.isArray(value);
|
59 | if (isArr || isObject(value)) {
|
60 |
|
61 | if (!loopSet.has(value)) {
|
62 | loopSet.add(value);
|
63 | var originValue = get(clone, path);
|
64 | if (isArr) {
|
65 |
|
66 | clone = set(clone, path, []);
|
67 | } else if (!originValue || _typeof(originValue) !== 'object') {
|
68 |
|
69 | clone = set(clone, path, createEmpty(value));
|
70 | }
|
71 | keys(value).forEach(function (key) {
|
72 | internalMerge([].concat(_toConsumableArray(path), [key]), loopSet);
|
73 | });
|
74 | }
|
75 | } else {
|
76 | clone = set(clone, path, value);
|
77 | }
|
78 | }
|
79 | internalMerge([]);
|
80 | });
|
81 | return clone;
|
82 | } |
\ | No newline at end of file |