deap.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var typeOf = require('./typeof'),
  2. slice = Array.prototype.slice;
  3. module.exports = {
  4. clone: deepClone,
  5. cloneShallow: clone,
  6. extend: deepExtend,
  7. extendShallow: extend,
  8. update: deepUpdate,
  9. updateShallow: update,
  10. merge: deepMerge,
  11. mergeShallow: merge
  12. };
  13. function clone(val) {
  14. switch(typeOf(val)) {
  15. case 'object':
  16. var args = slice.call(arguments);
  17. args.unshift({});
  18. return extend.apply(null, args);
  19. case 'array':
  20. return [].concat(val);
  21. case 'date':
  22. return new Date(val.getTime());
  23. case 'regexp':
  24. return new RegExp(val);
  25. default:
  26. return val;
  27. }
  28. }
  29. function deepClone(val) {
  30. switch(typeOf(val)) {
  31. case 'object':
  32. var args = slice.call(arguments);
  33. args.unshift({});
  34. return deepExtend.apply(null, args);
  35. case 'array':
  36. return val.map(function(v) { return deepClone(v); });
  37. default:
  38. return clone(val);
  39. }
  40. }
  41. function extend(a, b /*, [b2..n] */) {
  42. slice.call(arguments, 1).forEach(function(b) {
  43. Object.keys(b).forEach(function(p) {
  44. a[p] = b[p];
  45. });
  46. });
  47. return a;
  48. }
  49. function deepExtend(a, b /*, [b2..n] */) {
  50. slice.call(arguments, 1).forEach(function(b) {
  51. Object.keys(b).forEach(function(p) {
  52. if(typeOf(b[p]) === 'object' && typeOf(a[p]) === 'object')
  53. deepExtend(a[p], b[p]);
  54. else
  55. a[p] = deepClone(b[p]);
  56. });
  57. });
  58. return a;
  59. }
  60. function update(a, b /*, [b2..n] */) {
  61. slice.call(arguments, 1).forEach(function(b) {
  62. Object.keys(b).forEach(function(p) {
  63. if(a.hasOwnProperty(p)) a[p] = b[p];
  64. });
  65. });
  66. return a;
  67. }
  68. function deepUpdate(a, b /*, [b2..n] */) {
  69. slice.call(arguments, 1).forEach(function(b) {
  70. var ap, bp, ta, tb;
  71. Object.keys(b).forEach(function(p) {
  72. if(a.hasOwnProperty(p)) {
  73. ap = a[p];
  74. bp = b[p];
  75. ta = typeOf(ap);
  76. tb = typeOf(bp);
  77. if(tb === 'object' && ta === 'object')
  78. deepUpdate(ap, bp);
  79. else if(tb === 'array' && ta === 'array') {
  80. ap.length = 0;
  81. ap.push.apply(ap, bp.map(function(v) { return deepClone(v); }));
  82. } else
  83. a[p] = deepClone(bp);
  84. }
  85. });
  86. });
  87. return a;
  88. }
  89. function merge(a, b /*, [b2..n] */) {
  90. slice.call(arguments, 1).forEach(function(b) {
  91. Object.keys(b).forEach(function(p) {
  92. if(!a.hasOwnProperty(p)) a[p] = b[p];
  93. });
  94. });
  95. return a;
  96. }
  97. function deepMerge(a, b /*, [b2..n] */) {
  98. slice.call(arguments, 1).forEach(function(b) {
  99. var ap, bp, ta, tb;
  100. Object.keys(b).forEach(function(p) {
  101. ap = a[p];
  102. bp = b[p];
  103. ta = typeOf(ap);
  104. tb = typeOf(bp);
  105. if(tb === 'object' && ta === 'object')
  106. deepMerge(ap, bp);
  107. else if(!a.hasOwnProperty(p))
  108. a[p] = deepClone(bp);
  109. });
  110. });
  111. return a;
  112. }