* Set an object property based on "path" (namespace) supplied creating * ... intermediary objects if they do not exist. * @param object {Object} An object where the properties specified on path should be set. * @param path {String} A string representing the property to be set, e.g. "user.st
(object, path, value)
| 9 | * ... is not a valid Object, in which case the property cannot be set. No exceptions are thrown. |
| 10 | */ |
| 11 | static set(object, path, value) { |
| 12 | let components = ObjectPath.getPathComponents(path), |
| 13 | length = components !== null ? components.length : 0, |
| 14 | result = false; |
| 15 | |
| 16 | if (length > 0 && ObjectPath.isValidObject(object)) { |
| 17 | let i = 0, |
| 18 | last = length - 1, |
| 19 | currentObject = object; |
| 20 | |
| 21 | while (i < last) { |
| 22 | let field = components[i]; |
| 23 | |
| 24 | if (field in currentObject) { |
| 25 | if (!ObjectPath.isValidObject(currentObject[field])) { |
| 26 | break; |
| 27 | } |
| 28 | } else { |
| 29 | currentObject[field] = {}; |
| 30 | } |
| 31 | |
| 32 | currentObject = currentObject[field]; |
| 33 | i++; |
| 34 | } |
| 35 | |
| 36 | if (i === last) { |
| 37 | currentObject[components[last]] = value; |
| 38 | result = true; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get an object property based on "path" (namespace) supplied traversing the object |
no test coverage detected