Source: wk/util/clone.js

/**
 * @author    Wolfgang Kowarschick <kowa@hs-augsburg.de>
 * @copyright 2017-2018
 * @license   CC-BY-NC-SA-4.0
 */

/**
 * @module wk/util/clone
 */

/**
 * Clones a (JSON) object.
 *
 * @function
 * @static
 *
 * @param {*} p_object
 *   The object to be cloned.
 * @param {Boolean} [p_deep = true]
 *   Indicates whether a deep clone is to be created or not.
 * @return {*}
 *   The clone.
 */
function clone(p_object, p_deep = true)
{ // https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript
  if (p_deep === true)
  { return JSON.parse(JSON.stringify(p_object)); }

  if (   p_object == null
      || p_object instanceof String
      || typeof p_object !== 'object'
     )
  { return p_object; }

  if (p_object instanceof Date)
  { return new Date(p_object); }

  if (p_object instanceof Array)
  { return [...p_object]; }

  return {...p_object};
}

export {clone};
export default clone;