clone.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var baseClone = require('../internal/baseClone'),
  2. bindCallback = require('../internal/bindCallback'),
  3. isIterateeCall = require('../internal/isIterateeCall');
  4. /**
  5. * Creates a clone of `value`. If `isDeep` is `true` nested objects are cloned,
  6. * otherwise they are assigned by reference. If `customizer` is provided it's
  7. * invoked to produce the cloned values. If `customizer` returns `undefined`
  8. * cloning is handled by the method instead. The `customizer` is bound to
  9. * `thisArg` and invoked with up to three argument; (value [, index|key, object]).
  10. *
  11. * **Note:** This method is loosely based on the
  12. * [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm).
  13. * The enumerable properties of `arguments` objects and objects created by
  14. * constructors other than `Object` are cloned to plain `Object` objects. An
  15. * empty object is returned for uncloneable values such as functions, DOM nodes,
  16. * Maps, Sets, and WeakMaps.
  17. *
  18. * @static
  19. * @memberOf _
  20. * @category Lang
  21. * @param {*} value The value to clone.
  22. * @param {boolean} [isDeep] Specify a deep clone.
  23. * @param {Function} [customizer] The function to customize cloning values.
  24. * @param {*} [thisArg] The `this` binding of `customizer`.
  25. * @returns {*} Returns the cloned value.
  26. * @example
  27. *
  28. * var users = [
  29. * { 'user': 'barney' },
  30. * { 'user': 'fred' }
  31. * ];
  32. *
  33. * var shallow = _.clone(users);
  34. * shallow[0] === users[0];
  35. * // => true
  36. *
  37. * var deep = _.clone(users, true);
  38. * deep[0] === users[0];
  39. * // => false
  40. *
  41. * // using a customizer callback
  42. * var el = _.clone(document.body, function(value) {
  43. * if (_.isElement(value)) {
  44. * return value.cloneNode(false);
  45. * }
  46. * });
  47. *
  48. * el === document.body
  49. * // => false
  50. * el.nodeName
  51. * // => BODY
  52. * el.childNodes.length;
  53. * // => 0
  54. */
  55. function clone(value, isDeep, customizer, thisArg) {
  56. if (isDeep && typeof isDeep != 'boolean' && isIterateeCall(value, isDeep, customizer)) {
  57. isDeep = false;
  58. }
  59. else if (typeof isDeep == 'function') {
  60. thisArg = customizer;
  61. customizer = isDeep;
  62. isDeep = false;
  63. }
  64. return typeof customizer == 'function'
  65. ? baseClone(value, isDeep, bindCallback(customizer, thisArg, 3))
  66. : baseClone(value, isDeep);
  67. }
  68. module.exports = clone;