Skip to content

Commit 801294d

Browse files
committed
improved merge processor
1 parent 5240573 commit 801294d

1 file changed

Lines changed: 31 additions & 33 deletions

File tree

lib/processor/annotation/Merge.js

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
var sequence = require('../sequence');
99
var Merge;
1010

11-
1211
/**
1312
* @constructor
1413
*/
@@ -46,29 +45,33 @@ Merge.prototype = {
4645
return this._parameter;
4746
},
4847

49-
/**
50-
* mine merge strategy: mine params over their. If params is already defined it gets overriden.
51-
*/
52-
_mergeMine : function (mine, their) {
48+
_doMerge : function (mine, their, method) {
5349
var key;
5450

5551
for (key in their) {
5652
if (their.hasOwnProperty(key)) {
57-
mine[key] = their[key];
53+
method.call(this, key);
5854
}
5955
}
56+
},
57+
58+
/**
59+
* mine merge strategy: mine params over their. If params is already defined it gets overriden.
60+
*/
61+
_mergeMine : function (mine, their) {
62+
this._doMerge(mine, their, function(k){
63+
mine[k] = their[k];
64+
});
6065

6166
return mine;
6267
},
6368

6469
_mergeOnlyProperties : function (mine, their) {
65-
var key;
66-
67-
for (key in their) {
68-
if (their.hasOwnProperty(key) && typeof their[key] !== 'function') {
69-
mine[key] = their[key];
70+
this._doMerge(mine, their, function(k){
71+
if (typeof their[k] !== 'function'){
72+
mine[k] = their[k];
7073
}
71-
}
74+
});
7275

7376
return mine;
7477
},
@@ -80,20 +83,18 @@ Merge.prototype = {
8083
* otherwise it gets overriden with mine.
8184
*/
8285
_mergeDeepMine : function (mine, their) {
83-
return this._mergeDeep(mine, their, this._mergeMine);
86+
return this._mergeDeep(mine, their, '_mergeMine');
8487
},
8588

8689
/**
8790
* their merge strategy: their params over mine. If params is already defined it doesn't get overriden.
8891
*/
8992
_mergeTheir : function (mine, their) {
90-
var key;
91-
92-
for (key in their) {
93-
if (their.hasOwnProperty(key) && mine[key] === undefined ) {
94-
mine[key] = their[key];
93+
this._doMerge(mine, their, function(k){
94+
if (mine[k] === undefined) {
95+
mine[k] = their[k];
9596
}
96-
}
97+
});
9798

9899
return mine;
99100
},
@@ -106,28 +107,25 @@ Merge.prototype = {
106107
* otherwise it gets overriden with mine.
107108
*/
108109
_mergeDeepTheir : function (mine, their) {
109-
return this._mergeDeep(mine, their, this._mergeTheir);
110+
return this._mergeDeep(mine, their, '_mergeTheir');
110111
},
111112

112113
/**
113114
* runs the deep merge using the given strategy
114115
*/
115116
_mergeDeep: function (mine, their, strategy) {
116-
var key;
117-
118-
for (key in their) {
119-
if (their.hasOwnProperty(key)) {
120-
if (typeof their[key] === 'object') {
121-
if (their[key] instanceof Array) {
122-
mine[key] = [].concat(mine[key], their[key]);
123-
} else {
124-
mine[key] = strategy(mine[key], their[key]);
125-
}
126-
}else if (mine[key] === undefined ) {
127-
mine[key] = their[key];
117+
this._doMerge(mine, their, function(key){
118+
if (typeof their[key] === 'object') {
119+
if (their[key] instanceof Array) {
120+
mine[key] = [].concat(mine[key], their[key]);
121+
} else {
122+
mine[key] = this[strategy](mine[key], their[key]);
128123
}
124+
}else if (mine[key] === undefined ) {
125+
mine[key] = their[key];
129126
}
130-
}
127+
});
128+
131129
return mine;
132130
},
133131

0 commit comments

Comments
 (0)