deep-extend.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-2018 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 = Buffer.alloc
  38. ? Buffer.alloc(val.length)
  39. : new Buffer(val.length);
  40. val.copy(x);
  41. return x;
  42. } else if (val instanceof Date) {
  43. return new Date(val.getTime());
  44. } else if (val instanceof RegExp) {
  45. return new RegExp(val);
  46. } else {
  47. throw new Error('Unexpected situation');
  48. }
  49. }
  50. /**
  51. * Recursive cloning array.
  52. */
  53. function deepCloneArray(arr) {
  54. var clone = [];
  55. arr.forEach(function (item, index) {
  56. if (typeof item === 'object' && item !== null) {
  57. if (Array.isArray(item)) {
  58. clone[index] = deepCloneArray(item);
  59. } else if (isSpecificValue(item)) {
  60. clone[index] = cloneSpecificValue(item);
  61. } else {
  62. clone[index] = deepExtend({}, item);
  63. }
  64. } else {
  65. clone[index] = item;
  66. }
  67. });
  68. return clone;
  69. }
  70. function safeGetProperty(object, property) {
  71. return property === '__proto__' ? undefined : object[property];
  72. }
  73. /**
  74. * Extening object that entered in first argument.
  75. *
  76. * Returns extended object or false if have no target object or incorrect type.
  77. *
  78. * If you wish to clone source object (without modify it), just use empty new
  79. * object as first argument, like this:
  80. * deepExtend({}, yourObj_1, [yourObj_N]);
  81. */
  82. var deepExtend = module.exports = function (/*obj_1, [obj_2], [obj_N]*/) {
  83. if (arguments.length < 1 || typeof arguments[0] !== 'object') {
  84. return false;
  85. }
  86. if (arguments.length < 2) {
  87. return arguments[0];
  88. }
  89. var target = arguments[0];
  90. // convert arguments to array and cut off target object
  91. var args = Array.prototype.slice.call(arguments, 1);
  92. var val, src, clone;
  93. args.forEach(function (obj) {
  94. // skip argument if isn't an object, is null, or is an array
  95. if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
  96. return;
  97. }
  98. Object.keys(obj).forEach(function (key) {
  99. src = safeGetProperty(target, key); // source value
  100. val = safeGetProperty(obj, key); // new value
  101. // recursion prevention
  102. if (val === target) {
  103. return;
  104. /**
  105. * if new value isn't object then just overwrite by new value
  106. * instead of extending.
  107. */
  108. } else if (typeof val !== 'object' || val === null) {
  109. target[key] = val;
  110. return;
  111. // just clone arrays (and recursive clone objects inside)
  112. } else if (Array.isArray(val)) {
  113. target[key] = deepCloneArray(val);
  114. return;
  115. // custom cloning and overwrite for specific objects
  116. } else if (isSpecificValue(val)) {
  117. target[key] = cloneSpecificValue(val);
  118. return;
  119. // overwrite by new value if source isn't object or array
  120. } else if (typeof src !== 'object' || src === null || Array.isArray(src)) {
  121. target[key] = deepExtend({}, val);
  122. return;
  123. // source value and new value is objects both, extending...
  124. } else {
  125. target[key] = deepExtend(src, val);
  126. return;
  127. }
  128. });
  129. });
  130. return target;
  131. };