Skip to content

Commit 440f1d0

Browse files
author
fn-andrw
committed
Make lib cross-compatible
1 parent 08e3935 commit 440f1d0

File tree

1 file changed

+113
-98
lines changed

1 file changed

+113
-98
lines changed

lib/deep-extend.js

Lines changed: 113 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -25,120 +25,135 @@
2525
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626
*/
2727

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-
}
5128

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-
}
7229

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-
}
30+
(function (name, definition) {
8631

87-
if (arguments.length < 2) {
88-
return arguments[0];
89-
}
32+
if (typeof module !== 'undefined' && module.exports) module.exports = definition();
33+
else if (typeof define !== 'undefined' && typeof define == 'function' && typeof define.amd == 'object') define(definition);
34+
else window[name] = definition();
9035

91-
var target = arguments[0];
36+
})('deepExtend', function () {
9237

93-
// convert arguments to array and cut off target object
94-
var args = Array.prototype.slice.call(arguments, 1);
38+
'use strict';
9539

96-
var val, src, clone;
40+
function isSpecificValue(val) {
41+
return (
42+
val instanceof Buffer
43+
|| val instanceof Date
44+
|| val instanceof RegExp
45+
) ? true : false;
46+
}
9747

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;
48+
function cloneSpecificValue(val) {
49+
if (Buffer !== void 0 && val instanceof Buffer) {
50+
var x = new Buffer(val.length);
51+
val.copy(x);
52+
return x;
53+
} else if (val instanceof Date) {
54+
return new Date(val.getTime());
55+
} else if (val instanceof RegExp) {
56+
return new RegExp(val);
57+
} else {
58+
throw new Error('Unexpected situation');
10259
}
60+
}
10361

104-
Object.keys(obj).forEach(function (key) {
105-
src = target[key]; // source value
106-
val = obj[key]; // new value
62+
/**
63+
* Recursive cloning array.
64+
*/
65+
function deepCloneArray(arr) {
66+
var clone = [];
67+
arr.forEach(function (item, index) {
68+
if (typeof item === 'object' && item !== null) {
69+
if (Array.isArray(item)) {
70+
clone[index] = deepCloneArray(item);
71+
} else if (isSpecificValue(item)) {
72+
clone[index] = cloneSpecificValue(item);
73+
} else {
74+
clone[index] = deepExtend({}, item);
75+
}
76+
} else {
77+
clone[index] = item;
78+
}
79+
});
80+
return clone;
81+
}
10782

108-
// recursion prevention
109-
if (val === target) {
110-
return;
83+
/**
84+
* Extening object that entered in first argument.
85+
*
86+
* Returns extended object or false if have no target object or incorrect type.
87+
*
88+
* If you wish to clone source object (without modify it), just use empty new
89+
* object as first argument, like this:
90+
* deepExtend({}, yourObj_1, [yourObj_N]);
91+
*/
92+
var deepExtend = function (/*obj_1, [obj_2], [obj_N]*/) {
93+
if (arguments.length < 1 || typeof arguments[0] !== 'object') {
94+
return false;
95+
}
11196

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;
97+
if (arguments.length < 2) {
98+
return arguments[0];
99+
}
119100

120-
// just clone arrays (and recursive clone objects inside)
121-
} else if (Array.isArray(val)) {
122-
target[key] = deepCloneArray(val);
123-
return;
101+
var target = arguments[0];
124102

125-
// custom cloning and overwrite for specific objects
126-
} else if (isSpecificValue(val)) {
127-
target[key] = cloneSpecificValue(val);
128-
return;
103+
// convert arguments to array and cut off target object
104+
var args = Array.prototype.slice.call(arguments, 1);
129105

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;
106+
var val, src, clone;
134107

135-
// source value and new value is objects both, extending...
136-
} else {
137-
target[key] = deepExtend(src, val);
108+
args.forEach(function (obj) {
109+
// skip argument if it is array or isn't object
110+
if (typeof obj !== 'object' || Array.isArray(obj)) {
138111
return;
139112
}
113+
114+
Object.keys(obj).forEach(function (key) {
115+
src = target[key]; // source value
116+
val = obj[key]; // new value
117+
118+
// recursion prevention
119+
if (val === target) {
120+
return;
121+
122+
/**
123+
* if new value isn't object then just overwrite by new value
124+
* instead of extending.
125+
*/
126+
} else if (typeof val !== 'object' || val === null) {
127+
target[key] = val;
128+
return;
129+
130+
// just clone arrays (and recursive clone objects inside)
131+
} else if (Array.isArray(val)) {
132+
target[key] = deepCloneArray(val);
133+
return;
134+
135+
// custom cloning and overwrite for specific objects
136+
} else if (isSpecificValue(val)) {
137+
target[key] = cloneSpecificValue(val);
138+
return;
139+
140+
// overwrite by new value if source isn't object or array
141+
} else if (typeof src !== 'object' || src === null || Array.isArray(src)) {
142+
target[key] = deepExtend({}, val);
143+
return;
144+
145+
// source value and new value is objects both, extending...
146+
} else {
147+
target[key] = deepExtend(src, val);
148+
return;
149+
}
150+
});
140151
});
141-
});
142152

143-
return target;
144-
}
153+
return target;
154+
}
155+
156+
return deepExtend;
157+
158+
159+
});

0 commit comments

Comments
 (0)