1 | /*! |
||
2 | * Node.JS module "Deep Extend" |
||
3 | * @description Recursive object extending. |
||
4 | * @author Viacheslav Lotsmanov (unclechu) <[email protected]> |
||
5 | * @license MIT |
||
6 | * |
||
7 | * The MIT License (MIT) |
||
8 | * |
||
9 | * Copyright (c) 2013 Viacheslav Lotsmanov |
||
10 | * |
||
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy of |
||
12 | * this software and associated documentation files (the "Software"), to deal in |
||
13 | * the Software without restriction, including without limitation the rights to |
||
14 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
||
15 | * the Software, and to permit persons to whom the Software is furnished to do so, |
||
16 | * subject to the following conditions: |
||
17 | * |
||
18 | * The above copyright notice and this permission notice shall be included in all |
||
19 | * copies or substantial portions of the Software. |
||
20 | * |
||
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
||
23 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
||
24 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
||
25 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
||
26 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||
27 | */ |
||
28 | |||
29 | /** |
||
30 | * Extening object that entered in first argument. |
||
31 | * Returns extended object or false if have no target object or incorrect type. |
||
32 | * If you wish to clone object, simply use that: |
||
33 | * deepExtend({}, yourObj_1, [yourObj_N]) - first arg is new empty object |
||
34 | */ |
||
35 | var deepExtend = module.exports = function (/*obj_1, [obj_2], [obj_N]*/) { |
||
36 | if (arguments.length < 1 || typeof arguments[0] !== 'object') { |
||
37 | return false; |
||
38 | } |
||
39 | |||
40 | if (arguments.length < 2) return arguments[0]; |
||
0 ignored issues
–
show
|
|||
41 | |||
42 | var target = arguments[0]; |
||
43 | |||
44 | // convert arguments to array and cut off target object |
||
45 | var args = Array.prototype.slice.call(arguments, 1); |
||
46 | |||
47 | var key, val, src, clone, tmpBuf; |
||
48 | |||
49 | args.forEach(function (obj) { |
||
50 | if (typeof obj !== 'object') return; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
51 | |||
52 | for (key in obj) { |
||
53 | if ( ! (key in obj)) continue; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
54 | |||
55 | src = target[key]; |
||
56 | val = obj[key]; |
||
57 | |||
58 | if (val === target) continue; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
59 | |||
60 | if (typeof val !== 'object' || val === null) { |
||
61 | target[key] = val; |
||
62 | continue; |
||
63 | } else if (val instanceof Buffer) { |
||
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
64 | tmpBuf = new Buffer(val.length); |
||
65 | val.copy(tmpBuf); |
||
0 ignored issues
–
show
|
|||
66 | target[key] = tmpBuf; |
||
67 | continue; |
||
68 | } else if (val instanceof Date) { |
||
69 | target[key] = new Date(val.getTime()); |
||
70 | continue; |
||
71 | } |
||
72 | |||
73 | if (typeof src !== 'object' || src === null) { |
||
0 ignored issues
–
show
|
|||
74 | clone = (Array.isArray(val)) ? [] : {}; |
||
75 | target[key] = deepExtend(clone, val); |
||
0 ignored issues
–
show
|
|||
76 | continue; |
||
77 | } |
||
78 | |||
79 | if (Array.isArray(val)) { |
||
80 | clone = (Array.isArray(src)) ? src : []; |
||
81 | } else { |
||
82 | clone = (!Array.isArray(src)) ? src : {}; |
||
83 | } |
||
84 | |||
85 | target[key] = deepExtend(clone, val); |
||
86 | } |
||
87 | }); |
||
88 | |||
89 | return target; |
||
90 | } |
||
91 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.