whet.extend.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Generated by CoffeeScript 1.3.3
  2. /*
  3. * whet.extend v0.9.7
  4. * Standalone port of jQuery.extend that actually works on node.js
  5. * https://github.com/Meettya/whet.extend
  6. *
  7. * Copyright 2012, Dmitrii Karpich
  8. * Released under the MIT License
  9. */
  10. (function() {
  11. var extend, _findValue, _isClass, _isOwnProp, _isPlainObj, _isPrimitiveType, _isTypeOf, _prepareClone,
  12. __slice = [].slice;
  13. module.exports = extend = function() {
  14. var args, copy, deep, name, options, target, _i, _len, _ref;
  15. deep = arguments[0], target = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
  16. if (!_isClass(deep, 'Boolean')) {
  17. args.unshift(target);
  18. _ref = [deep || {}, false], target = _ref[0], deep = _ref[1];
  19. }
  20. if (_isPrimitiveType(target)) {
  21. target = {};
  22. }
  23. for (_i = 0, _len = args.length; _i < _len; _i++) {
  24. options = args[_i];
  25. if (options != null) {
  26. for (name in options) {
  27. copy = options[name];
  28. target[name] = _findValue(deep, copy, target[name]);
  29. }
  30. }
  31. }
  32. return target;
  33. };
  34. /*
  35. Internal methods from now
  36. */
  37. _isClass = function(obj, str) {
  38. return ("[object " + str + "]") === Object.prototype.toString.call(obj);
  39. };
  40. _isOwnProp = function(obj, prop) {
  41. return Object.prototype.hasOwnProperty.call(obj, prop);
  42. };
  43. _isTypeOf = function(obj, str) {
  44. return str === typeof obj;
  45. };
  46. _isPlainObj = function(obj) {
  47. var key;
  48. if (!obj) {
  49. return false;
  50. }
  51. if (obj.nodeType || obj.setInterval || !_isClass(obj, 'Object')) {
  52. return false;
  53. }
  54. if (obj.constructor && !_isOwnProp(obj, 'constructor') && !_isOwnProp(obj.constructor.prototype, 'isPrototypeOf')) {
  55. return false;
  56. }
  57. for (key in obj) {
  58. key;
  59. }
  60. return key === void 0 || _isOwnProp(obj, key);
  61. };
  62. _isPrimitiveType = function(obj) {
  63. return !(_isTypeOf(obj, 'object') || _isTypeOf(obj, 'function'));
  64. };
  65. _prepareClone = function(copy, src) {
  66. if (_isClass(copy, 'Array')) {
  67. if (_isClass(src, 'Array')) {
  68. return src;
  69. } else {
  70. return [];
  71. }
  72. } else {
  73. if (_isPlainObj(src)) {
  74. return src;
  75. } else {
  76. return {};
  77. }
  78. }
  79. };
  80. _findValue = function(deep, copy, src) {
  81. var clone;
  82. if (deep && (_isClass(copy, 'Array') || _isPlainObj(copy))) {
  83. clone = _prepareClone(copy, src);
  84. return extend(deep, clone, copy);
  85. } else {
  86. return copy;
  87. }
  88. };
  89. }).call(this);