Skip to content

Instantly share code, notes, and snippets.

@dkarmalita
Created October 2, 2020 15:57
Show Gist options
  • Save dkarmalita/918e56917f0a6ee70194e64a6edf98e9 to your computer and use it in GitHub Desktop.
Save dkarmalita/918e56917f0a6ee70194e64a6edf98e9 to your computer and use it in GitHub Desktop.
// 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