Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Molstar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michal Malý
Molstar
Commits
43332fca
Commit
43332fca
authored
6 years ago
by
Alexander Rose
Browse files
Options
Downloads
Patches
Plain Diff
added simple deepClone
parent
74990cab
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/mol-util/object.ts
+25
-1
25 additions, 1 deletion
src/mol-util/object.ts
with
25 additions
and
1 deletion
src/mol-util/object.ts
+
25
−
1
View file @
43332fca
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
*
* @author David Sehnal <david.sehnal@gmail.com>
* @author David Sehnal <david.sehnal@gmail.com>
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
*/
const
hasOwnProperty
=
Object
.
prototype
.
hasOwnProperty
;
const
hasOwnProperty
=
Object
.
prototype
.
hasOwnProperty
;
...
@@ -54,4 +55,27 @@ export function shallowMerge<T>(source: T, ...rest: (Partial<T> | undefined)[]):
...
@@ -54,4 +55,27 @@ export function shallowMerge<T>(source: T, ...rest: (Partial<T> | undefined)[]):
}
}
}
}
return
ret
;
return
ret
;
}
}
\ No newline at end of file
/** Simple deep clone for number, boolean, string, null, undefined, object, array */
export
function
deepClone
<
T
>
(
source
:
T
):
T
{
if
(
null
===
source
||
"
object
"
!==
typeof
source
)
return
source
;
if
(
source
instanceof
Array
)
{
const
copy
:
any
[]
=
[];
for
(
let
i
=
0
,
len
=
source
.
length
;
i
<
len
;
i
++
)
{
copy
[
i
]
=
deepClone
(
source
[
i
]);
}
return
copy
as
any
as
T
;
}
if
(
source
instanceof
Object
)
{
const
copy
:
{
[
k
:
string
]:
any
}
=
{};
for
(
let
k
in
source
)
{
if
(
hasOwnProperty
.
call
(
source
,
k
))
copy
[
k
]
=
deepClone
(
source
[
k
]);
}
return
copy
as
any
as
T
;
}
throw
new
Error
(
`Can't clone, type "
${
typeof
source
}
" unsupported`
);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment