index.js 2.6 KB

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