]> arthur.barton.de Git - netdata.git/blob - node.d/node_modules/extend.js
ab-debian 0.20170316.01-0ab1, upstream v1.5.0-620-ge1d37e04
[netdata.git] / node.d / node_modules / extend.js
1 // https://github.com/justmoon/node-extend
2
3 'use strict';
4
5 var hasOwn = Object.prototype.hasOwnProperty;
6 var toStr = Object.prototype.toString;
7
8 var isArray = function isArray(arr) {
9         if (typeof Array.isArray === 'function') {
10                 return Array.isArray(arr);
11         }
12
13         return toStr.call(arr) === '[object Array]';
14 };
15
16 var isPlainObject = function isPlainObject(obj) {
17         if (!obj || toStr.call(obj) !== '[object Object]') {
18                 return false;
19         }
20
21         var hasOwnConstructor = hasOwn.call(obj, 'constructor');
22         var hasIsPrototypeOf = obj.constructor && obj.constructor.prototype && hasOwn.call(obj.constructor.prototype, 'isPrototypeOf');
23         // Not own constructor property must be Object
24         if (obj.constructor && !hasOwnConstructor && !hasIsPrototypeOf) {
25                 return false;
26         }
27
28         // Own properties are enumerated firstly, so to speed up,
29         // if last one is own, then all properties are own.
30         var key;
31         for (key in obj) { /**/ }
32
33         return typeof key === 'undefined' || hasOwn.call(obj, key);
34 };
35
36 module.exports = function extend() {
37         var options, name, src, copy, copyIsArray, clone;
38         var target = arguments[0];
39         var i = 1;
40         var length = arguments.length;
41         var deep = false;
42
43         // Handle a deep copy situation
44         if (typeof target === 'boolean') {
45                 deep = target;
46                 target = arguments[1] || {};
47                 // skip the boolean and the target
48                 i = 2;
49         } else if ((typeof target !== 'object' && typeof target !== 'function') || target == null) {
50                 target = {};
51         }
52
53         for (; i < length; ++i) {
54                 options = arguments[i];
55                 // Only deal with non-null/undefined values
56                 if (options != null) {
57                         // Extend the base object
58                         for (name in options) {
59                                 src = target[name];
60                                 copy = options[name];
61
62                                 // Prevent never-ending loop
63                                 if (target !== copy) {
64                                         // Recurse if we're merging plain objects or arrays
65                                         if (deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) {
66                                                 if (copyIsArray) {
67                                                         copyIsArray = false;
68                                                         clone = src && isArray(src) ? src : [];
69                                                 } else {
70                                                         clone = src && isPlainObject(src) ? src : {};
71                                                 }
72
73                                                 // Never move original objects, clone them
74                                                 target[name] = extend(deep, clone, copy);
75
76                                         // Don't bring in undefined values
77                                         } else if (typeof copy !== 'undefined') {
78                                                 target[name] = copy;
79                                         }
80                                 }
81                         }
82                 }
83         }
84
85         // Return the modified object
86         return target;
87 };