@@ -199,15 +199,32 @@ function setDefaultAttributeIds(attribute) {
199
199
return attribute ;
200
200
}
201
201
202
+ /**
203
+ * Merge array of attributes coming from the device with another one coming from a configuration. The latter will
204
+ * complete the information in the former if the attribute in the configuration:
205
+ * - does not have a conflicting object_id with any attribute of the device.
206
+ * - does not have a conflicting name with any other attribute in the device
207
+ *
208
+ * @param {Array } original List of attributes of the device.
209
+ * @param {Array } newArray List of attributes of the configuration.
210
+ * @return {Array } Merge of the attributes of the device and those of the configuration.
211
+ */
202
212
function mergeArrays ( original , newArray ) {
203
- var originalKeys = _ . pluck ( original , 'name' ) ,
204
- newKeys = _ . pluck ( newArray , 'name' ) ,
213
+ /* jshint camelcase: false */
214
+ var originalKeys = _ . pluck ( original , 'object_id' ) ,
215
+ newKeys = _ . pluck ( newArray , 'object_id' ) ,
205
216
addedKeys = _ . difference ( newKeys , originalKeys ) ,
206
217
differenceArray = newArray . filter ( function ( item ) {
207
- return addedKeys . indexOf ( item . name ) >= 0 ;
218
+ return addedKeys . indexOf ( item . object_id ) >= 0 ;
219
+ } ) ,
220
+ originalNames = _ . pluck ( original , 'name' ) ,
221
+ newNames = _ . pluck ( newArray , 'name' ) ,
222
+ addedNames = _ . difference ( newNames , originalNames ) ,
223
+ differenceNamesArray = newArray . filter ( function ( item ) {
224
+ return addedNames . indexOf ( item . name ) >= 0 && ( ! item . object_id || newKeys . indexOf ( item . object_id ) < 0 ) ;
208
225
} ) ;
209
226
210
- return original . concat ( differenceArray ) ;
227
+ return original . concat ( differenceArray ) . concat ( differenceNamesArray ) ;
211
228
}
212
229
213
230
/**
0 commit comments