export function addKeyToObject ( destObject , path ) { return _addKeyToObject ( destObject , path . split ( /(?<!\\)\./ ) ) ; } function _addKeyToObject ( destObject , pathComponents ) { let topKey = pathComponents . shift ( ) ; if ( ! destObject . hasOwnProperty ( topKey ) ) { destObject [ topKey ] = { } ; } if ( pathComponents . length > 0 ) { return _addKeyToObject ( destObject [ topKey ] , pathComponents ) ; } else { return destObject [ topKey ] ; } } export function setValue ( destObject , path , value ) { return _setValue ( destObject , path . split ( /(?<!\\)\./ ) , value ) ; } function _setValue ( destObject , pathComponents , value ) { let topKey = pathComponents . shift ( ) ; if ( pathComponents . length == 0 ) { destObject [ topKey ] = value ; return destObject [ topKey ] ; } else { if ( ! destObject . hasOwnProperty ( topKey ) ) { destObject [ topKey ] = { } ; } return _setValue ( destObject [ topKey ] , pathComponents , value ) ; } } export function addValue ( destObject , path , amountToAdd ) { let val = getValue ( destObject , path , 0 ) ; if ( isNaN ( val ) ) { throw `${ val } at path ${ path } is Not a Number. Cannot Increment` ; } val += amountToAdd ; return setValue ( destObject , path , val ) ; } export function incrementValue ( destObject , path ) { return addValue ( destObject , path , 1 ) ; } export function getValue ( srcObject , path , defaultValue = null ) { return _getValue ( srcObject , path . split ( /(?<!\\)\./ ) , defaultValue ) ; } function _getValue ( srcObject , pathComponents , defaultValue = null ) { let topKey = pathComponents . shift ( ) ; if ( srcObject . hasOwnProperty ( topKey ) ) { if ( pathComponents . length > 0 ) { return _getValue ( srcObject [ topKey ] , pathComponents , defaultValue ) ; } else { return srcObject [ topKey ] ; } } else { return defaultValue ; } }