Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
U
uview-ui
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李晖
uview-ui
Commits
21759d31
Commit
21759d31
authored
Mar 27, 2023
by
北桥
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ref: deepClone & deepMerge
parent
1e19e9df
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
35 deletions
+46
-35
deepClone.js
uview-ui/libs/function/deepClone.js
+28
-16
deepMerge.js
uview-ui/libs/function/deepMerge.js
+18
-19
No files found.
uview-ui/libs/function/deepClone.js
View file @
21759d31
// 判断arr是否为一个数组,返回一个bool值
function
isArray
(
arr
)
{
function
isArray
(
arr
)
{
return
Object
.
prototype
.
toString
.
call
(
arr
)
===
'
[object Array]
'
;
}
// 深度克隆
function
deepClone
(
obj
)
{
// 对常见的“非”值,直接返回原来值
if
([
null
,
undefined
,
NaN
,
false
].
includes
(
obj
))
return
obj
;
if
(
typeof
obj
!==
"
object
"
&&
typeof
obj
!==
'
function
'
)
{
//原始类型直接返回
return
obj
;
function
deepClone
(
obj
,
cache
=
new
WeakMap
())
{
if
(
obj
===
null
||
typeof
obj
!==
'
object
'
)
return
obj
;
if
(
cache
.
has
(
obj
))
return
cache
.
get
(
obj
);
let
clone
;
if
(
obj
instanceof
Date
)
{
clone
=
new
Date
(
obj
.
getTime
());
}
else
if
(
obj
instanceof
RegExp
)
{
clone
=
new
RegExp
(
obj
);
}
else
if
(
obj
instanceof
Map
)
{
clone
=
new
Map
(
Array
.
from
(
obj
,
([
key
,
value
])
=>
[
key
,
deepClone
(
value
,
cache
)]));
}
else
if
(
obj
instanceof
Set
)
{
clone
=
new
Set
(
Array
.
from
(
obj
,
value
=>
deepClone
(
value
,
cache
)));
}
else
if
(
Array
.
isArray
(
obj
))
{
clone
=
obj
.
map
(
value
=>
deepClone
(
value
,
cache
));
}
else
if
(
Object
.
prototype
.
toString
.
call
(
obj
)
===
'
[object Object]
'
)
{
clone
=
Object
.
create
(
Object
.
getPrototypeOf
(
obj
));
cache
.
set
(
obj
,
clone
);
for
(
const
[
key
,
value
]
of
Object
.
entries
(
obj
))
{
clone
[
key
]
=
deepClone
(
value
,
cache
);
}
var
o
=
isArray
(
obj
)
?
[]
:
{};
for
(
let
i
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
i
)){
o
[
i
]
=
typeof
obj
[
i
]
===
"
object
"
?
deepClone
(
obj
[
i
])
:
obj
[
i
];
}
else
{
clone
=
Object
.
assign
({},
obj
);
}
}
return
o
;
cache
.
set
(
obj
,
clone
);
return
clone
;
}
export
default
deepClone
;
uview-ui/libs/function/deepMerge.js
View file @
21759d31
...
...
@@ -3,28 +3,27 @@ import deepClone from "./deepClone";
// JS对象深度合并
function
deepMerge
(
target
=
{},
source
=
{})
{
target
=
deepClone
(
target
);
if
(
typeof
target
!==
'
object
'
||
typeof
source
!==
'
object
'
)
return
false
;
for
(
var
prop
in
source
)
{
if
(
typeof
target
!==
'
object
'
||
target
===
null
||
typeof
source
!==
'
object
'
||
source
===
null
)
return
target
;
const
merged
=
Array
.
isArray
(
target
)
?
target
.
slice
()
:
Object
.
assign
({},
target
);
for
(
const
prop
in
source
)
{
if
(
!
source
.
hasOwnProperty
(
prop
))
continue
;
if
(
prop
in
target
)
{
if
(
typeof
target
[
prop
]
!==
'
object
'
)
{
target
[
prop
]
=
source
[
prop
];
const
sourceValue
=
source
[
prop
];
const
targetValue
=
merged
[
prop
];
if
(
sourceValue
instanceof
Date
)
{
merged
[
prop
]
=
new
Date
(
sourceValue
);
}
else
if
(
sourceValue
instanceof
RegExp
)
{
merged
[
prop
]
=
new
RegExp
(
sourceValue
);
}
else
if
(
sourceValue
instanceof
Map
)
{
merged
[
prop
]
=
new
Map
(
sourceValue
);
}
else
if
(
sourceValue
instanceof
Set
)
{
merged
[
prop
]
=
new
Set
(
sourceValue
);
}
else
if
(
typeof
sourceValue
===
'
object
'
&&
sourceValue
!==
null
)
{
merged
[
prop
]
=
deepMerge
(
targetValue
,
sourceValue
);
}
else
{
if
(
typeof
source
[
prop
]
!==
'
object
'
)
{
target
[
prop
]
=
source
[
prop
];
}
else
{
if
(
target
[
prop
].
concat
&&
source
[
prop
].
concat
)
{
target
[
prop
]
=
target
[
prop
].
concat
(
source
[
prop
]);
}
else
{
target
[
prop
]
=
deepMerge
(
target
[
prop
],
source
[
prop
]);
}
}
}
}
else
{
target
[
prop
]
=
source
[
prop
];
merged
[
prop
]
=
sourceValue
;
}
}
return
target
;
return
merged
;
}
export
default
deepMerge
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment