11/** Deep Copy
22 * Return a copy of the object passed as param, doing (recursivelly) copys of
33 * all the objects the initial has.
4- *
5- * Caution!!!
4+ *
5+ * Caution!!!
66 * This function is recursive. Copying a very deep object might concern heavy
77 * performance issues. Use your brain before the function.
8- *
8+ *
99 * @param oldObj {Object} Object to clone
1010 */
1111function deepCopy ( oldObj ) {
12- var newObj = oldObj ;
13- if ( oldObj && typeof oldObj === 'object' ) {
14- newObj = Object . prototype . toString . call ( oldObj ) === '[object Array]' ? [ ] : { } ;
15- for ( var i in oldObj ) {
16- newObj [ i ] = deepCopy ( oldObj [ i ] ) ;
17- }
12+ var newObj = oldObj ;
13+ if ( oldObj && typeof oldObj === 'object' ) {
14+ newObj = Object . prototype . toString . call ( oldObj ) === '[object Array]' ? [ ] : { } ;
15+ for ( var i in oldObj ) {
16+ newObj [ i ] = deepCopy ( oldObj [ i ] ) ;
1817 }
19- return newObj ;
18+ }
19+ return newObj ;
2020}
2121
2222/** Merge
2323 * Perform a complete merge of two objects.
2424 *
25- * Caution!!!
25+ * Caution!!!
2626 * A third parameter, avoidDeepCopy is also included to avoid creating a deep
2727 * copy of the objects. With this parameter to true, original objects may get
2828 * updated, linked or similar unexpected behaviour.
@@ -32,21 +32,22 @@ function deepCopy(oldObj) {
3232 * @param avoidDeepCopy {booleam} Seting to true will update the "old" object
3333 */
3434function merge ( old , obj , avoidDeepCopy ) {
35- let dest , orig ;
36-
37- if ( avoidDeepCopy ) {
38- dest = old ;
39- orig = obj ;
40- } else {
41- dest = deepCopy ( old ) ;
42- orig = deepCopy ( obj ) ;
43- }
44-
45- for ( let prop in orig ) {
46- dest [ prop ] = orig [ prop ] ;
47- }
35+ let dest ,
36+ orig ;
37+
38+ if ( avoidDeepCopy ) {
39+ dest = old ;
40+ orig = obj ;
41+ } else {
42+ dest = deepCopy ( old ) ;
43+ orig = deepCopy ( obj ) ;
44+ }
45+
46+ for ( const prop in orig ) {
47+ dest [ prop ] = orig [ prop ] ;
48+ }
4849
49- return dest ;
50+ return dest ;
5051}
5152
5253/** Equal
@@ -69,7 +70,7 @@ function equal(a, b) {
6970
7071 if ( arrA && arrB ) {
7172 if ( a . length !== b . length ) {
72- return false ;
73+ return false ;
7374 }
7475
7576 for ( i = 0 ; i < a . length ; i ++ ) {
@@ -103,7 +104,7 @@ function equal(a, b) {
103104
104105 var regexpA = a instanceof RegExp ;
105106 var regexpB = b instanceof RegExp ;
106- if ( regexpA && regexpB ) {
107+ if ( regexpA && regexpB ) {
107108 return a . toString ( ) === b . toString ( ) ;
108109 }
109110 if ( regexpA !== regexpB ) {
@@ -117,7 +118,7 @@ function equal(a, b) {
117118 }
118119
119120 for ( i = 0 ; i < keys . length ; i ++ ) {
120- if ( ! equal ( a [ keys [ i ] ] , b [ keys [ i ] ] ) ) {
121+ if ( ! equal ( a [ keys [ i ] ] , b [ keys [ i ] ] ) ) {
121122 return false ;
122123 }
123124 }
@@ -131,5 +132,5 @@ function equal(a, b) {
131132module . exports = {
132133 merge : merge ,
133134 equal : equal ,
134- clone : deepCopy
135- } ;
135+ clone : deepCopy ,
136+ } ;
0 commit comments