Skip to content

Commit 962c900

Browse files
committed
* Utils.Object.DeepCopy is a new function that will recursively deep copy an array of object.
1 parent e2c36e1 commit 962c900

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/utils/object/DeepCopy.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* Deep Copy the given object or array.
9+
*
10+
* @function Phaser.Utils.Objects.DeepCopy
11+
* @since 3.50.0
12+
*
13+
* @param {object} obj - The object to deep copy.
14+
*
15+
* @return {object} A deep copy of the original object.
16+
*/
17+
var DeepCopy = function (inObject)
18+
{
19+
var outObject;
20+
var value;
21+
var key;
22+
23+
if (typeof inObject !== 'object' || inObject === null)
24+
{
25+
// inObject is not an object
26+
return inObject;
27+
}
28+
29+
// Create an array or object to hold the values
30+
outObject = Array.isArray(inObject) ? [] : {};
31+
32+
for (key in inObject)
33+
{
34+
value = inObject[key];
35+
36+
// Recursively (deep) copy for nested objects, including arrays
37+
outObject[key] = DeepCopy(value);
38+
}
39+
40+
return outObject;
41+
};
42+
43+
module.exports = DeepCopy;

src/utils/object/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
module.exports = {
1212

1313
Clone: require('./Clone'),
14+
DeepCopy: require('./DeepCopy'),
1415
Extend: require('./Extend'),
1516
GetAdvancedValue: require('./GetAdvancedValue'),
1617
GetFastValue: require('./GetFastValue'),

0 commit comments

Comments
 (0)