|
| 1 | +var IsPlainObject = require('./IsPlainObject'); |
| 2 | + |
| 3 | +/** |
| 4 | +* This is a slightly modified version of http://api.jquery.com/jQuery.extend/ |
| 5 | +* |
| 6 | +* @method Phaser.Utils.extend |
| 7 | +* @param {boolean} deep - Perform a deep copy? |
| 8 | +* @param {object} target - The target object to copy to. |
| 9 | +* @return {object} The extended object. |
| 10 | +*/ |
| 11 | +var extend = function () |
| 12 | +{ |
| 13 | + var options, name, src, copy, copyIsArray, clone, |
| 14 | + target = arguments[0] || {}, |
| 15 | + i = 1, |
| 16 | + length = arguments.length, |
| 17 | + deep = false; |
| 18 | + |
| 19 | + // Handle a deep copy situation |
| 20 | + if (typeof target === "boolean") |
| 21 | + { |
| 22 | + deep = target; |
| 23 | + target = arguments[1] || {}; |
| 24 | + // skip the boolean and the target |
| 25 | + i = 2; |
| 26 | + } |
| 27 | + |
| 28 | + // extend Phaser if only one argument is passed |
| 29 | + if (length === i) |
| 30 | + { |
| 31 | + target = this; |
| 32 | + --i; |
| 33 | + } |
| 34 | + |
| 35 | + for (; i < length; i++) |
| 36 | + { |
| 37 | + // Only deal with non-null/undefined values |
| 38 | + if ((options = arguments[i]) != null) |
| 39 | + { |
| 40 | + // Extend the base object |
| 41 | + for (name in options) |
| 42 | + { |
| 43 | + src = target[name]; |
| 44 | + copy = options[name]; |
| 45 | + |
| 46 | + // Prevent never-ending loop |
| 47 | + if (target === copy) |
| 48 | + { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + // Recurse if we're merging plain objects or arrays |
| 53 | + if (deep && copy && (IsPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) |
| 54 | + { |
| 55 | + if (copyIsArray) |
| 56 | + { |
| 57 | + copyIsArray = false; |
| 58 | + clone = src && Array.isArray(src) ? src : []; |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + clone = src && IsPlainObject(src) ? src : {}; |
| 63 | + } |
| 64 | + |
| 65 | + // Never move original objects, clone them |
| 66 | + target[name] = Extend(deep, clone, copy); |
| 67 | + |
| 68 | + // Don't bring in undefined values |
| 69 | + } |
| 70 | + else if (copy !== undefined) |
| 71 | + { |
| 72 | + target[name] = copy; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Return the modified object |
| 79 | + return target; |
| 80 | +}; |
| 81 | + |
| 82 | +module.exports = Extend; |
0 commit comments