Skip to content

Commit 4c95d69

Browse files
committed
Added new SetValue function for property setting to any depth
1 parent 3759714 commit 4c95d69

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Notes:
9696
* The `Container.setScrollFactor` method has a new optional argument `updateChildren`. If set, it will change the `scrollFactor` values of all the Container children as well as the Container. Fix #4466 #4475 (thanks @pinkkis @enriqueto)
9797
* There is a new webpack config `FEATURE_SOUND` which is set to `true` by default, but if set to `false` it will exclude the Sound Manager and all of its systems into the build files. Fix #4428 (thanks @goldfire)
9898
* `Scene.Systems.renderer` is a new property that is a reference to the current renderer the game is using.
99+
* `Utils.Objects.SetValue` is a new function that allows you to set a value in an object by specifying a property key. The function can set a value to any depth by using dot-notation for the key, i.e. `SetValue(data, 'world.position.x', 100)`.
99100

100101
### Updates
101102

src/utils/object/SetValue.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* Sets a value in an object, allowing for dot notation to control the depth of the property.
9+
*
10+
* For example:
11+
*
12+
* ```javascript
13+
* var data = {
14+
* world: {
15+
* position: {
16+
* x: 200,
17+
* y: 100
18+
* }
19+
* }
20+
* };
21+
*
22+
* SetValue(data, 'world.position.y', 300);
23+
*
24+
* console.log(data.world.position.y); // 300
25+
* ```
26+
*
27+
* @function Phaser.Utils.Objects.SetValue
28+
* @since 3.17.0
29+
*
30+
* @param {object} source - The object to set the value in.
31+
* @param {string} key - The name of the property in the object. If a property is nested, the names of its preceding properties should be separated by a dot (`.`)
32+
* @param {any} value - The value to set into the property, if found in the source object.
33+
*
34+
* @return {boolean} `true` if the property key was valid and the value was set, otherwise `false`.
35+
*/
36+
var SetValue = function (source, key, value)
37+
{
38+
if (!source || typeof source === 'number')
39+
{
40+
return false;
41+
}
42+
else if (source.hasOwnProperty(key))
43+
{
44+
source[key] = value;
45+
46+
return true;
47+
}
48+
else if (key.indexOf('.') !== -1)
49+
{
50+
var keys = key.split('.');
51+
var parent = source;
52+
var prev = source;
53+
54+
// Use for loop here so we can break early
55+
for (var i = 0; i < keys.length; i++)
56+
{
57+
if (parent.hasOwnProperty(keys[i]))
58+
{
59+
// Yes it has a key property, let's carry on down
60+
prev = parent;
61+
parent = parent[keys[i]];
62+
}
63+
else
64+
{
65+
return false;
66+
}
67+
}
68+
69+
prev[keys[keys.length - 1]] = value;
70+
71+
return true;
72+
}
73+
74+
return false;
75+
};
76+
77+
module.exports = SetValue;

src/utils/object/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports = {
2121
HasValue: require('./HasValue'),
2222
IsPlainObject: require('./IsPlainObject'),
2323
Merge: require('./Merge'),
24-
MergeRight: require('./MergeRight')
24+
MergeRight: require('./MergeRight'),
25+
SetValue: require('./SetValue')
2526

2627
};

0 commit comments

Comments
 (0)