department-of-veterans-affairs/vets-website

View on GitHub
src/platform/utilities/data/clone.js

Summary

Maintainability
B
4 hrs
Test Coverage
/**
 * Returns a shallow clone of an object.
 *
 * @param {Object} object
 * @return {Object}
 */
export default function clone(object) {
  /* eslint-disable no-prototype-builtins */
  if (typeof object === 'object') {
    if (Array.isArray(object)) {
      return object.slice();
    } else if (Set.prototype.isPrototypeOf(object)) {
      return new Set(object);
    } else if (Map.prototype.isPrototypeOf(object)) {
      return new Map(object);
    }
    // Probably just ye olde object
    return Object.assign({}, object);
  } else if (typeof object === 'undefined') {
    return undefined;
  }
  throw new Error(`Unknown type in clone: ${typeof object}`);
  /* eslint-enable no-prototype-builtins */
}