Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 2.88 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.         jQuery.extend extracted from the jQuery source & optimised for NodeJS
  3.         Twitter: @FGRibreau / fgribreau.com
  4.  
  5.         Usage:
  6.                 var Extend = require('./Extend');
  7.  
  8.  
  9.                 // Extend
  10.                 var obj = Extend({opt1:true, opt2:true}, {opt1:false});
  11.  
  12.                 // Deep Copy
  13.                 var clonedObject = Extend(true, {}, myObject);
  14.                 var clonedArray = Extend(true, [], ['a',['b','c',['d']]]);
  15. */
  16. var toString = Object.prototype.toString,
  17.         hasOwn = Object.prototype.hasOwnProperty,
  18.         push = Array.prototype.push,
  19.         slice = Array.prototype.slice,
  20.         trim = String.prototype.trim,
  21.         indexOf = Array.prototype.indexOf,
  22.  
  23.         // [[Class]] -> type pairs
  24.         class2type = {};
  25.  
  26. // Populate the class2type map
  27. "Boolean Number String Function Array Date RegExp Object".split(" ").forEach(function(name) {
  28.         class2type[ "[object " + name + "]" ] = name.toLowerCase();
  29. });
  30.  
  31. function type(obj){
  32.         return obj == null ?
  33.                         String( obj ) :
  34.                         class2type[ toString.call(obj) ] || "object";
  35. }
  36.  
  37. function isPlainObject( obj ) {
  38.         if ( !obj || type(obj) !== "object") {
  39.                 return false;
  40.         }
  41.  
  42.         // Not own constructor property must be Object
  43.         if ( obj.constructor &&
  44.                 !hasOwn.call(obj, "constructor") &&
  45.                 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
  46.                 return false;
  47.         }
  48.  
  49.         // Own properties are enumerated firstly, so to speed up,
  50.         // if last one is own, then all properties are own.
  51.  
  52.         var key;
  53.         for ( key in obj ) {}
  54.  
  55.         return key === undefined || hasOwn.call( obj, key );
  56. }
  57.  
  58. module.exports = function extend(){
  59.         var options, name, src, copy, copyIsArray, clone,
  60.                 target = arguments[0] || {},
  61.                 i = 1,
  62.                 length = arguments.length,
  63.                 deep = false;
  64.  
  65.         // Handle a deep copy situation
  66.         if ( typeof target === "boolean" ) {
  67.                 deep = target;
  68.                 target = arguments[1] || {};
  69.                 // skip the boolean and the target
  70.                 i = 2;
  71.         }
  72.  
  73.         // Handle case when target is a string or something (possible in deep copy)
  74.         if ( typeof target !== "object" && type(target) !== "function") {
  75.                 target = {};
  76.         }
  77.  
  78.         // extend jQuery itself if only one argument is passed
  79.         if ( length === i ) {
  80.                 target = this;
  81.                 --i;
  82.         }
  83.  
  84.         for ( ; i < length; i++ ) {
  85.                 // Only deal with non-null/undefined values
  86.                 if ( (options = arguments[ i ]) != null ) {
  87.                         // Extend the base object
  88.                         for ( name in options ) {
  89.                                 src = target[ name ];
  90.                                 copy = options[ name ];
  91.  
  92.                                 // Prevent never-ending loop
  93.                                 if ( target === copy ) {
  94.                                         continue;
  95.                                 }
  96.  
  97.                                 // Recurse if we're merging plain objects or arrays
  98.                                 if ( deep && copy && ( isPlainObject(copy) || (copyIsArray = type(copy) === "array") ) ) {
  99.                                         if ( copyIsArray ) {
  100.                                                 copyIsArray = false;
  101.                                                 clone = src && type(src) === "array" ? src : [];
  102.  
  103.                                         } else {
  104.                                                 clone = src && isPlainObject(src) ? src : {};
  105.                                         }
  106.  
  107.                                         // Never move original objects, clone them
  108.                                         target[ name ] = extend( deep, clone, copy );
  109.  
  110.                                 // Don't bring in undefined values
  111.                                 } else if ( copy !== undefined ) {
  112.                                         target[ name ] = copy;
  113.                                 }
  114.                         }
  115.                 }
  116.         }
  117.  
  118.         // Return the modified object
  119.         return target;
  120. };