object.js 826 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. /*
  3. Deep-clone an object.
  4. */
  5. function clone(obj)
  6. {
  7. if (obj instanceof Object)
  8. {
  9. var clonedObj = (obj instanceof Array) ? [] : {};
  10. for (var i in obj)
  11. {
  12. if ( obj.hasOwnProperty(i) )
  13. {
  14. clonedObj[i] = clone( obj[i] );
  15. }
  16. }
  17. return clonedObj;
  18. }
  19. return obj;
  20. }
  21. /*
  22. https://github.com/jonschlinkert/is-plain-object
  23. */
  24. function isPlainObject(obj)
  25. {
  26. return !!obj && typeof obj==="object" && obj.constructor===Object;
  27. }
  28. /*
  29. Shallow-merge two objects.
  30. */
  31. function shallowMerge(target, source)
  32. {
  33. if (target instanceof Object && source instanceof Object)
  34. {
  35. for (var i in source)
  36. {
  37. if ( source.hasOwnProperty(i) )
  38. {
  39. target[i] = source[i];
  40. }
  41. }
  42. }
  43. return target;
  44. }
  45. module.exports =
  46. {
  47. clone: clone,
  48. isPlainObject: isPlainObject,
  49. shallowMerge: shallowMerge
  50. };