|
5 | 5 | *
|
6 | 6 | * The MIT License (MIT)
|
7 | 7 | *
|
8 |
| - * Copyright (c) 2013-2015 Viacheslav Lotsmanov |
| 8 | + * Copyright (c) 2013-2016 Viacheslav Lotsmanov |
| 9 | + * Copyright (c) 2016 Andrew Fatkulin |
9 | 10 | *
|
10 | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy of
|
11 | 12 | * this software and associated documentation files (the "Software"), to deal in
|
|
25 | 26 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
26 | 27 | */
|
27 | 28 |
|
28 |
| -'use strict'; |
29 |
| - |
30 |
| -function isSpecificValue(val) { |
31 |
| - return ( |
32 |
| - val instanceof Buffer |
33 |
| - || val instanceof Date |
34 |
| - || val instanceof RegExp |
35 |
| - ) ? true : false; |
36 |
| -} |
37 |
| - |
38 |
| -function cloneSpecificValue(val) { |
39 |
| - if (val instanceof Buffer) { |
40 |
| - var x = new Buffer(val.length); |
41 |
| - val.copy(x); |
42 |
| - return x; |
43 |
| - } else if (val instanceof Date) { |
44 |
| - return new Date(val.getTime()); |
45 |
| - } else if (val instanceof RegExp) { |
46 |
| - return new RegExp(val); |
47 |
| - } else { |
48 |
| - throw new Error('Unexpected situation'); |
49 |
| - } |
50 |
| -} |
51 | 29 |
|
52 |
| -/** |
53 |
| - * Recursive cloning array. |
54 |
| - */ |
55 |
| -function deepCloneArray(arr) { |
56 |
| - var clone = []; |
57 |
| - arr.forEach(function (item, index) { |
58 |
| - if (typeof item === 'object' && item !== null) { |
59 |
| - if (Array.isArray(item)) { |
60 |
| - clone[index] = deepCloneArray(item); |
61 |
| - } else if (isSpecificValue(item)) { |
62 |
| - clone[index] = cloneSpecificValue(item); |
63 |
| - } else { |
64 |
| - clone[index] = deepExtend({}, item); |
65 |
| - } |
66 |
| - } else { |
67 |
| - clone[index] = item; |
68 |
| - } |
69 |
| - }); |
70 |
| - return clone; |
71 |
| -} |
72 | 30 |
|
73 |
| -/** |
74 |
| - * Extening object that entered in first argument. |
75 |
| - * |
76 |
| - * Returns extended object or false if have no target object or incorrect type. |
77 |
| - * |
78 |
| - * If you wish to clone source object (without modify it), just use empty new |
79 |
| - * object as first argument, like this: |
80 |
| - * deepExtend({}, yourObj_1, [yourObj_N]); |
81 |
| - */ |
82 |
| -var deepExtend = module.exports = function (/*obj_1, [obj_2], [obj_N]*/) { |
83 |
| - if (arguments.length < 1 || typeof arguments[0] !== 'object') { |
84 |
| - return false; |
85 |
| - } |
| 31 | +(function (name, definition) { |
86 | 32 |
|
87 |
| - if (arguments.length < 2) { |
88 |
| - return arguments[0]; |
89 |
| - } |
| 33 | + if (typeof module !== 'undefined' && module.exports) module.exports = definition(); |
| 34 | + else if (typeof define !== 'undefined' && typeof define == 'function' && typeof define.amd == 'object') define(definition); |
| 35 | + else window[name] = definition(); |
90 | 36 |
|
91 |
| - var target = arguments[0]; |
| 37 | +})('deepExtend', function () { |
92 | 38 |
|
93 |
| - // convert arguments to array and cut off target object |
94 |
| - var args = Array.prototype.slice.call(arguments, 1); |
| 39 | + 'use strict'; |
95 | 40 |
|
96 |
| - var val, src, clone; |
| 41 | + function isSpecificValue(val) { |
| 42 | + return ( |
| 43 | + typeof Buffer !== 'undefined' && val instanceof Buffer |
| 44 | + || val instanceof Date |
| 45 | + || val instanceof RegExp |
| 46 | + ) ? true : false; |
| 47 | + } |
97 | 48 |
|
98 |
| - args.forEach(function (obj) { |
99 |
| - // skip argument if it is array or isn't object |
100 |
| - if (typeof obj !== 'object' || Array.isArray(obj)) { |
101 |
| - return; |
| 49 | + function cloneSpecificValue(val) { |
| 50 | + if (typeof Buffer !== 'undefined' && val instanceof Buffer) { |
| 51 | + var x = new Buffer(val.length); |
| 52 | + val.copy(x); |
| 53 | + return x; |
| 54 | + } else if (val instanceof Date) { |
| 55 | + return new Date(val.getTime()); |
| 56 | + } else if (val instanceof RegExp) { |
| 57 | + return new RegExp(val); |
| 58 | + } else { |
| 59 | + throw new Error('Unexpected situation'); |
102 | 60 | }
|
| 61 | + } |
103 | 62 |
|
104 |
| - Object.keys(obj).forEach(function (key) { |
105 |
| - src = target[key]; // source value |
106 |
| - val = obj[key]; // new value |
| 63 | + /** |
| 64 | + * Recursive cloning array. |
| 65 | + */ |
| 66 | + function deepCloneArray(arr) { |
| 67 | + var clone = []; |
| 68 | + arr.forEach(function (item, index) { |
| 69 | + if (typeof item === 'object' && item !== null) { |
| 70 | + if (Array.isArray(item)) { |
| 71 | + clone[index] = deepCloneArray(item); |
| 72 | + } else if (isSpecificValue(item)) { |
| 73 | + clone[index] = cloneSpecificValue(item); |
| 74 | + } else { |
| 75 | + clone[index] = deepExtend({}, item); |
| 76 | + } |
| 77 | + } else { |
| 78 | + clone[index] = item; |
| 79 | + } |
| 80 | + }); |
| 81 | + return clone; |
| 82 | + } |
107 | 83 |
|
108 |
| - // recursion prevention |
109 |
| - if (val === target) { |
110 |
| - return; |
| 84 | + /** |
| 85 | + * Extening object that entered in first argument. |
| 86 | + * |
| 87 | + * Returns extended object or false if have no target object or incorrect type. |
| 88 | + * |
| 89 | + * If you wish to clone source object (without modify it), just use empty new |
| 90 | + * object as first argument, like this: |
| 91 | + * deepExtend({}, yourObj_1, [yourObj_N]); |
| 92 | + */ |
| 93 | + var deepExtend = function (/*obj_1, [obj_2], [obj_N]*/) { |
| 94 | + if (arguments.length < 1 || typeof arguments[0] !== 'object') { |
| 95 | + return false; |
| 96 | + } |
111 | 97 |
|
112 |
| - /** |
113 |
| - * if new value isn't object then just overwrite by new value |
114 |
| - * instead of extending. |
115 |
| - */ |
116 |
| - } else if (typeof val !== 'object' || val === null) { |
117 |
| - target[key] = val; |
118 |
| - return; |
| 98 | + if (arguments.length < 2) { |
| 99 | + return arguments[0]; |
| 100 | + } |
119 | 101 |
|
120 |
| - // just clone arrays (and recursive clone objects inside) |
121 |
| - } else if (Array.isArray(val)) { |
122 |
| - target[key] = deepCloneArray(val); |
123 |
| - return; |
| 102 | + var target = arguments[0]; |
124 | 103 |
|
125 |
| - // custom cloning and overwrite for specific objects |
126 |
| - } else if (isSpecificValue(val)) { |
127 |
| - target[key] = cloneSpecificValue(val); |
128 |
| - return; |
| 104 | + // convert arguments to array and cut off target object |
| 105 | + var args = Array.prototype.slice.call(arguments, 1); |
129 | 106 |
|
130 |
| - // overwrite by new value if source isn't object or array |
131 |
| - } else if (typeof src !== 'object' || src === null || Array.isArray(src)) { |
132 |
| - target[key] = deepExtend({}, val); |
133 |
| - return; |
| 107 | + var val, src, clone; |
134 | 108 |
|
135 |
| - // source value and new value is objects both, extending... |
136 |
| - } else { |
137 |
| - target[key] = deepExtend(src, val); |
| 109 | + args.forEach(function (obj) { |
| 110 | + // skip argument if it is array or isn't object |
| 111 | + if (typeof obj !== 'object' || Array.isArray(obj)) { |
138 | 112 | return;
|
139 | 113 | }
|
| 114 | + |
| 115 | + Object.keys(obj).forEach(function (key) { |
| 116 | + src = target[key]; // source value |
| 117 | + val = obj[key]; // new value |
| 118 | + |
| 119 | + // recursion prevention |
| 120 | + if (val === target) { |
| 121 | + return; |
| 122 | + |
| 123 | + /** |
| 124 | + * if new value isn't object then just overwrite by new value |
| 125 | + * instead of extending. |
| 126 | + */ |
| 127 | + } else if (typeof val !== 'object' || val === null) { |
| 128 | + target[key] = val; |
| 129 | + return; |
| 130 | + |
| 131 | + // just clone arrays (and recursive clone objects inside) |
| 132 | + } else if (Array.isArray(val)) { |
| 133 | + target[key] = deepCloneArray(val); |
| 134 | + return; |
| 135 | + |
| 136 | + // custom cloning and overwrite for specific objects |
| 137 | + } else if (isSpecificValue(val)) { |
| 138 | + target[key] = cloneSpecificValue(val); |
| 139 | + return; |
| 140 | + |
| 141 | + // overwrite by new value if source isn't object or array |
| 142 | + } else if (typeof src !== 'object' || src === null || Array.isArray(src)) { |
| 143 | + target[key] = deepExtend({}, val); |
| 144 | + return; |
| 145 | + |
| 146 | + // source value and new value is objects both, extending... |
| 147 | + } else { |
| 148 | + target[key] = deepExtend(src, val); |
| 149 | + return; |
| 150 | + } |
| 151 | + }); |
140 | 152 | });
|
141 |
| - }); |
142 | 153 |
|
143 |
| - return target; |
144 |
| -} |
| 154 | + return target; |
| 155 | + } |
| 156 | + |
| 157 | + return deepExtend; |
| 158 | + |
| 159 | + |
| 160 | +}); |
0 commit comments