Last active
August 29, 2015 14:11
-
-
Save bultas/a80a81d86540a906ed14 to your computer and use it in GitHub Desktop.
Set value to Object property (string path)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Return true/false if key is found in Object (one level) | |
* @param {object} object where to try find key | |
* @param {string} key - string to find in object | |
* @return {boolean} true/false if key is found | |
*/ | |
var foundInObject = function (object, key) { | |
var keyFound = null; | |
for (var prop in object) { | |
if (prop === key) { | |
keyFound = true; | |
break; | |
} else { | |
keyFound = false; | |
} | |
} | |
return keyFound; | |
}; | |
module.exports = foundInObject; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Replace Object to another object | |
* @param {object} object1 - object to change | |
* @param {object} object2 - object to replace object1 | |
*/ | |
var replaceObj = function(object1, object2) { | |
for (var i in object1) { | |
delete(object1[i]); | |
} | |
for (var y in object2) { | |
object1[y] = object2[y]; | |
} | |
}; | |
module.exports = replaceObj; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var replaceObj = require("utils/replaceObj"); | |
var foundInObj = require("utils/foundInObj"); | |
/** | |
* Set Value to Object path as String | |
* where arrary reference is: item[0] | |
* @param {path} path - ex.: name.name.item[0].name | |
*/ | |
var setToValue = function(obj, value, path, addToObj) { | |
if (!path) { | |
return replaceObj(obj, value); | |
} | |
var pathAsArray = path.split('.'); | |
// convert splited path to valid object keys | |
// fix if path contains array[0] like strings | |
var keys = []; | |
var arrayRegExp = /([a-zA-Z]+)(\[(\d)\])+/; // matches "item[0]" | |
for (var i = 0; i < pathAsArray.length; i++) { | |
var arrayKey = arrayRegExp.exec(pathAsArray[i]); | |
if (arrayKey) { | |
keys.push(arrayKey[1]); | |
keys.push(arrayKey[3]); | |
} else { | |
keys.push(pathAsArray[i]); | |
} | |
} | |
function setToObject(object, value, keys, start, addToObj) { | |
var key = keys[start]; | |
var end = keys.length - 1; | |
var keyFound = foundInObj(object, key); | |
if (keyFound) { | |
if (start === end) { | |
object[key] = value; | |
return object; | |
} else { | |
setToObject( object[key], value, keys, start + 1, addToObj); | |
} | |
} | |
if (!keyFound && addToObj) { | |
object[key] = value; | |
} | |
} | |
setToObject(obj, value, keys, 0, addToObj); | |
}; | |
module.exports = setToValue; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require("assert"); | |
var clone = require("source/utils/clone"); | |
var setToValue = require("source/utils/setToValue"); | |
var _testObject = { | |
level0: "Value0", | |
deep: { | |
level1: "Value1", | |
deeper: { | |
level2: "Value2", | |
deepest: "deepestValue" | |
} | |
}, | |
variants: [ | |
{name: "variant A"}, | |
{name: "variant B"} | |
] | |
}; | |
describe('SetToValue function', function() { | |
it('basic deeper object value change', function() { | |
var testObject = clone(_testObject); | |
var value = "changed"; | |
var path = "deep.deeper.deepest"; | |
assert.deepEqual(testObject, _testObject); | |
setToValue(testObject, value, path); | |
assert.equal(testObject.deep.deeper.deepest, value); | |
}); | |
it('deeper object value change with array', function() { | |
var testObject = clone(_testObject); | |
var value = "changed"; | |
var path = "variants[0].name"; | |
assert.deepEqual(testObject, _testObject); | |
setToValue(testObject, value, path); | |
assert.equal(testObject.variants[0].name, value); | |
}); | |
it('calling with argument addToObj = true', function() { | |
var testObject = clone(_testObject); | |
var value = "changed"; | |
var path = "deep.deeper.test"; | |
assert.deepEqual(testObject, _testObject); | |
setToValue(testObject, value, path, true); | |
assert.equal(testObject.deep.deeper.test, value); | |
}); | |
it('calling with argument addToObj = false || null', function() { | |
var testObject = clone(_testObject); | |
var value = "changed"; | |
var path = "deep.deeper.test"; | |
assert.deepEqual(testObject, _testObject); | |
setToValue(testObject, value, path); | |
assert.deepEqual(testObject, _testObject); | |
setToValue(testObject, value, path, false); | |
assert.deepEqual(testObject, _testObject); | |
}); | |
it('calling without path replace whole object', function() { | |
var testObject = clone(_testObject); | |
var newObject = {newProp: "value"}; | |
assert.deepEqual(testObject, _testObject); | |
setToValue(testObject, newObject); | |
assert.deepEqual(testObject, newObject); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment