Last active
June 3, 2019 01:46
-
-
Save codeBelt/450bb91c638fb0db07f560ca92bf2d43 to your computer and use it in GitHub Desktop.
For Medium Article: https://medium.com/swlh/dont-let-api-data-structure-your-javascript-application-7fa7fd5a590f
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
export default class Util { | |
/** | |
* Recursive function that makes a clone of an object. | |
* | |
* @param src {Object} The object you to clone. | |
* @param renamePropertyNameFunction {(keyName: string) => string} Optional function to rename property names | |
* @returns {any} Returns a clone object of the one passed in. | |
* @example | |
* let cloneOfObject = Util.clone(obj); | |
*/ | |
static clone(src, renamePropertyNameFunction) { | |
if (src == null || typeof src !== "object") { | |
return src; | |
} | |
if (src instanceof Date) { | |
return new Date(src.getTime()); | |
} | |
if (src instanceof RegExp) { | |
return new RegExp(src); | |
} | |
if (src instanceof Array) { | |
return src.map((item) => Util.clone(item, renamePropertyNameFunction)); | |
} | |
if (src instanceof Object) { | |
const hasRenameFunction = typeof renamePropertyNameFunction === 'function'; | |
return Object.keys(src).reduce((newObject, propertyName) => { | |
const name = hasRenameFunction | |
? renamePropertyNameFunction(propertyName) | |
: propertyName; | |
newObject[name] = Util.clone(src[propertyName], renamePropertyNameFunction); | |
return newObject; | |
}, {}) | |
} | |
throw new Error(`Unable to copy. ${src} isn't supported.`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment