Skip to content

Instantly share code, notes, and snippets.

@codeBelt
Last active June 3, 2019 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeBelt/450bb91c638fb0db07f560ca92bf2d43 to your computer and use it in GitHub Desktop.
Save codeBelt/450bb91c638fb0db07f560ca92bf2d43 to your computer and use it in GitHub Desktop.
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