Created
October 2, 2020 15:57
-
-
Save dkarmalita/918e56917f0a6ee70194e64a6edf98e9 to your computer and use it in GitHub Desktop.
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
// v20200514 + extendVal | |
/* eslint-disable no-restricted-syntax, guard-for-in, */ | |
module.exports = function cloneObject( | |
aObject, | |
extendVal = x => x, | |
) { | |
if ( | |
!aObject | |
|| (typeof aObject !== 'object') | |
) { | |
return aObject; | |
} | |
let v; | |
const bObject = Array.isArray(aObject) ? [] : {}; | |
for (const k in aObject) { | |
v = aObject[k]; | |
bObject[k] = (typeof v === 'object') | |
? cloneObject(v, extendVal) | |
: extendVal(v); | |
} | |
return bObject; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment