-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow rewriting date values (and possibly others)
- Loading branch information
Showing
6 changed files
with
95 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { | ||
module.exports = { setByKeyPath, getByKeyPath }; | ||
} | ||
|
||
/** | ||
* Set (nested) property of object using dot notation | ||
* | ||
* @param {object} target | ||
* @param {string} key | ||
* @param value | ||
*/ | ||
function setByKeyPath(target, key, value) { | ||
var parts = key.split('.'); | ||
|
||
for (var i = 0; i < parts.length; i++) { | ||
var part = parts[i]; | ||
|
||
if (i < parts.length -1) { | ||
if (typeof target[part] !== 'object') { | ||
target[part] = {}; | ||
} | ||
|
||
target = target[part]; | ||
} else { | ||
target[part] = value; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get (nested) property of object using dot notation | ||
* | ||
* @param {object} target | ||
* @param {string} key | ||
* @param defaultValue | ||
*/ | ||
function getByKeyPath(target, key, defaultValue) { | ||
if (!target || !key) return false; | ||
|
||
key = key.split('.'); | ||
var l = key.length, | ||
i = 0, | ||
p = ''; | ||
|
||
for (; i < l; ++i) { | ||
p = key[i]; | ||
|
||
if (target.hasOwnProperty(p)) target = target[p]; | ||
else return defaultValue; | ||
} | ||
|
||
return target; | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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