Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
basic
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xrm
basic
Commits
eae2402c
Commit
eae2402c
authored
4 years ago
by
Sebastian Listl
Browse files
Options
Downloads
Patches
Plain Diff
Util_lib Utils.clone improved
parent
fa52aec2
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
process/Util_lib/process.js
+29
-12
29 additions, 12 deletions
process/Util_lib/process.js
with
29 additions
and
12 deletions
process/Util_lib/process.js
+
29
−
12
View file @
eae2402c
...
...
@@ -61,29 +61,46 @@ Utils.isNullOrEmpty = function (pObject)
*
* var original = new MyObject();
* original.name = "Jotaro";
* original.obj1 = {};
* original.obj2 = original.obj1;
*
* var copy = ObjectUtils.clone(original);
* copy.name = "Josuke";
*
* logging.log(original.name != copy.name); //true
* logging.log(copy instanceof MyObject()); //true (prototypes are set correctly)
* logging.log(copy instanceof MyObject()); //true, prototypes are set correctly
* logging.log(copy.obj1 === copy.obj2); //true, relative object references are kept
*/
Utils
.
clone
=
function
(
pObject
)
{
if
(
!
Utils
.
isObject
(
pObject
)
||
pObject
===
null
)
return
pObject
;
//Return the value if inObject is not an object
var
referenceMap
=
new
Map
();
return
_clone
(
pObject
);
var
clonedObject
=
Array
.
isArray
(
pObject
)
?
[]
:
Object
.
create
(
Object
.
getPrototypeOf
(
pObject
));
//set the prototype of the given object
for
(
let
key
in
pObject
)
function
_clone
(
pObject
)
{
var
value
=
pObject
[
key
];
clonedObject
[
key
]
=
Utils
.
clone
(
value
);
//Recursively (deep) copy for nested objects, including arrays
if
(
typeof
pObject
!==
"
object
"
||
pObject
===
null
)
return
pObject
;
//Return the value if inObject is not an object
if
(
referenceMap
.
has
(
pObject
))
return
referenceMap
.
get
(
pObject
);
var
clonedObject
=
Array
.
isArray
(
pObject
)
?
[]
:
Object
.
create
(
Object
.
getPrototypeOf
(
pObject
));
//set the prototype of the given object
/* keeps track of all encountered objects and maps the original to the copy, this makes it possible to:
- have the same relative references in the copy as in the original
- copy cyclic references without error */
referenceMap
.
set
(
pObject
,
clonedObject
);
for
(
let
key
in
pObject
)
{
var
value
=
pObject
[
key
];
clonedObject
[
key
]
=
_clone
(
value
);
//Recursively (deep) copy for nested objects, including arrays
}
return
clonedObject
;
}
return
clonedObject
;
}
/**
...
...
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