deep-extend.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*!
  2. * @description Recursive object extending
  3. * @author Viacheslav Lotsmanov <lotsmanov89@gmail.com>
  4. * @license MIT
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2013-2015 Viacheslav Lotsmanov
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  11. * this software and associated documentation files (the "Software"), to deal in
  12. * the Software without restriction, including without limitation the rights to
  13. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  14. * the Software, and to permit persons to whom the Software is furnished to do so,
  15. * subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in all
  18. * copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  22. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  23. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  24. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  25. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. 'use strict';
  28. function isSpecificValue(val) {
  29. return (
  30. val instanceof Buffer
  31. || val instanceof Date
  32. || val instanceof RegExp
  33. ) ? true : false;
  34. }
  35. function cloneSpecificValue(val) {
  36. if (val instanceof Buffer) {
  37. var x = new Buffer(val.length);
  38. val.copy(x);
  39. return x;
  40. } else if (val instanceof Date) {
  41. return new Date(val.getTime());
  42. } else if (val instanceof RegExp) {
  43. return new RegExp(val);
  44. } else {
  45. throw new Error('Unexpected situation');
  46. }
  47. }
  48. /**
  49. * Recursive cloning array.
  50. */
  51. function deepCloneArray(arr) {
  52. var clone = [];
  53. arr.forEach(function (item, index) {
  54. if (typeof item === 'object' && item !== null) {
  55. if (Array.isArray(item)) {
  56. clone[index] = deepCloneArray(item);
  57. } else if (isSpecificValue(item)) {
  58. clone[index] = cloneSpecificValue(item);
  59. } else {
  60. clone[index] = deepExtend({}, item);
  61. }
  62. } else {
  63. clone[index] = item;
  64. }
  65. });
  66. return clone;
  67. }
  68. /**
  69. * Extening object that entered in first argument.
  70. *
  71. * Returns extended object or false if have no target object or incorrect type.
  72. *
  73. * If you wish to clone source object (without modify it), just use empty new
  74. * object as first argument, like this:
  75. * deepExtend({}, yourObj_1, [yourObj_N]);
  76. */
  77. var deepExtend = module.exports = function (/*obj_1, [obj_2], [obj_N]*/) {
  78. if (arguments.length < 1 || typeof arguments[0] !== 'object') {
  79. return false;
  80. }
  81. if (arguments.length < 2) {
  82. return arguments[0];
  83. }
  84. var target = arguments[0];
  85. // convert arguments to array and cut off target object
  86. var args = Array.prototype.slice.call(arguments, 1);
  87. var val, src, clone;
  88. args.forEach(function (obj) {
  89. // skip argument if isn't an object, is null, or is an array
  90. if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
  91. return;
  92. }
  93. Object.keys(obj).forEach(function (key) {
  94. src = target[key]; // source value
  95. val = obj[key]; // new value
  96. // recursion prevention
  97. if (val === target) {
  98. return;
  99. /**
  100. * if new value isn't object then just overwrite by new value
  101. * instead of extending.
  102. */
  103. } else if (typeof val !== 'object' || val === null) {
  104. target[key] = val;
  105. return;
  106. // just clone arrays (and recursive clone objects inside)
  107. } else if (Array.isArray(val)) {
  108. target[key] = deepCloneArray(val);
  109. return;
  110. // custom cloning and overwrite for specific objects
  111. } else if (isSpecificValue(val)) {
  112. target[key] = cloneSpecificValue(val);
  113. return;
  114. // overwrite by new value if source isn't object or array
  115. } else if (typeof src !== 'object' || src === null || Array.isArray(src)) {
  116. target[key] = deepExtend({}, val);
  117. return;
  118. // source value and new value is objects both, extending...
  119. } else {
  120. target[key] = deepExtend(src, val);
  121. return;
  122. }
  123. });
  124. });
  125. return target;
  126. }