2015-08-24 12:41:04 +00:00
|
|
|
module.exports.buildOneToOneIdentityMap = buildOneToOneIdentityMap;
|
|
|
|
module.exports.buildOneToManyIdentityMap = buildOneToManyIdentityMap;
|
2015-10-10 14:21:06 +00:00
|
|
|
module.exports.buildOneToOneIdentityMapWithOrigKeys = buildOneToOneIdentityMapWithOrigKeys;
|
2015-10-30 15:15:48 +00:00
|
|
|
module.exports.buildOneToManyIdentityMapWithOrigKeys = buildOneToManyIdentityMapWithOrigKeys;
|
2015-08-24 12:41:04 +00:00
|
|
|
module.exports.join = join;
|
2015-10-30 15:15:48 +00:00
|
|
|
module.exports.KVMap = KVMap;
|
2015-08-24 12:41:04 +00:00
|
|
|
/**
|
2015-10-10 14:21:06 +00:00
|
|
|
* Effectively builds associative map on id -> object relation.
|
2015-08-24 12:41:04 +00:00
|
|
|
* Map returned in form of object with ids in keys and object as values.
|
|
|
|
* @param objs array of objects to build from
|
|
|
|
* @param idName name of property to be used as id. Such property considered to be unique across array.
|
|
|
|
* In case of collisions last wins. For non-unique ids use buildOneToManyIdentityMap()
|
|
|
|
* @returns {{}} object where keys are ids and values are objects itself
|
|
|
|
*/
|
|
|
|
function buildOneToOneIdentityMap(objs, idName) {
|
|
|
|
var idMap = {};
|
|
|
|
for(var i = 0; i < objs.length; i++) {
|
|
|
|
var obj = objs[i];
|
|
|
|
var id = obj[idName].toString();
|
|
|
|
idMap[id] = obj;
|
|
|
|
}
|
|
|
|
return idMap;
|
|
|
|
}
|
|
|
|
|
2015-10-10 14:21:06 +00:00
|
|
|
/**
|
|
|
|
* Effectively builds associative map on id -> object relation and stores original keys.
|
|
|
|
* Map returned in form of object with ids in keys and object as values.
|
|
|
|
* @param objs array of objects to build from
|
|
|
|
* @param idName name of property to be used as id. Such property considered to be unique across array.
|
|
|
|
* In case of collisions last wins. For non-unique ids use buildOneToManyIdentityMap()
|
2015-10-30 15:15:48 +00:00
|
|
|
* @returns {} object where keys are ids and values are objects itself
|
2015-10-10 14:21:06 +00:00
|
|
|
*/
|
|
|
|
function buildOneToOneIdentityMapWithOrigKeys(objs, idName) {
|
2015-10-30 15:15:48 +00:00
|
|
|
var kvMap = new KVMap();
|
2015-10-10 14:21:06 +00:00
|
|
|
for(var i = 0; i < objs.length; i++) {
|
|
|
|
var obj = objs[i];
|
|
|
|
var id = obj[idName];
|
2015-10-30 15:15:48 +00:00
|
|
|
kvMap.set(id, obj);
|
2015-10-10 14:21:06 +00:00
|
|
|
}
|
2015-10-30 15:15:48 +00:00
|
|
|
return kvMap;
|
2015-10-10 14:21:06 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 12:41:04 +00:00
|
|
|
/**
|
|
|
|
* Effectively builds associate map on id -> Array[Object].
|
|
|
|
* Map returned in form of object with ids in keys and array of objects with given id.
|
|
|
|
* @param objs array of objects to build from
|
|
|
|
* @param idName name of property to be used as id
|
|
|
|
*/
|
|
|
|
function buildOneToManyIdentityMap(objs, idName) {
|
|
|
|
var idMap = {};
|
|
|
|
for(var i = 0; i < objs.length; i++) {
|
|
|
|
var obj = objs[i];
|
|
|
|
var id = obj[idName].toString();
|
|
|
|
if(id in idMap) {
|
|
|
|
idMap[id].push(obj);
|
|
|
|
} else {
|
|
|
|
idMap[id] = [obj];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idMap;
|
|
|
|
}
|
|
|
|
|
2015-10-10 16:00:00 +00:00
|
|
|
function buildOneToManyIdentityMapWithOrigKeys(objs, idName) {
|
2015-10-30 15:15:48 +00:00
|
|
|
var kvMap = new KVMap();
|
2015-10-10 16:00:00 +00:00
|
|
|
for(var i = 0; i < objs.length; i++) {
|
|
|
|
var obj = objs[i];
|
|
|
|
var id = obj[idName];
|
2015-10-30 15:15:48 +00:00
|
|
|
var value = kvMap.get(id) || [];
|
|
|
|
value.push(obj);
|
|
|
|
kvMap.set(id, value);
|
2015-10-10 16:00:00 +00:00
|
|
|
}
|
2015-10-30 15:15:48 +00:00
|
|
|
return kvMap;
|
2015-10-10 16:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 12:41:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Yeah, it joins. You need three things id -> obj1 map, id -> [obj2] map and merge function.
|
|
|
|
* This functions will take each obj1, locate all data to join in map2 and call merge function.
|
|
|
|
* @param oneToOneIdMap
|
|
|
|
* @param oneToManyIdMap
|
|
|
|
* @param mergeF function(obj, objectsToMergeIn)
|
|
|
|
*/
|
|
|
|
function join(oneToOneIdMap, oneToManyIdMap, mergeF) {
|
2015-10-30 15:15:48 +00:00
|
|
|
var ids = oneToOneIdMap.getKeys();
|
2015-08-24 12:41:04 +00:00
|
|
|
for(var i = 0; i < ids.length; i++) {
|
|
|
|
var id = ids[i];
|
2015-10-30 15:15:48 +00:00
|
|
|
var obj = oneToOneIdMap.get(id);
|
|
|
|
var objectsToMergeIn = oneToManyIdMap.get(id) || [];
|
2015-08-24 12:41:04 +00:00
|
|
|
mergeF(obj, objectsToMergeIn);
|
|
|
|
}
|
|
|
|
}
|
2015-10-30 15:15:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function KVMap(){
|
|
|
|
var _originalKeyFieldName = 'originalKey';
|
|
|
|
var _valueKeyFieldName = 'value';
|
|
|
|
var _dict = {};
|
|
|
|
var keyToString = function(key){ return key.toString() };
|
|
|
|
var mapImpl = {
|
|
|
|
set: function(key, value){
|
|
|
|
var recordObj = {};
|
|
|
|
recordObj[_originalKeyFieldName] = key;
|
|
|
|
recordObj[_valueKeyFieldName] = value;
|
|
|
|
_dict[keyToString(key)] = recordObj;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
get: function(key){
|
|
|
|
var storeObj = _dict[keyToString(key)];
|
|
|
|
if(storeObj) {
|
|
|
|
return storeObj[_valueKeyFieldName];
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remove: function(key){
|
|
|
|
delete _dict[keyToString(key)];
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
exist: function(key) {
|
|
|
|
var result = _dict.hasOwnProperty(keyToString(key));
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
getKeys: function(){
|
|
|
|
var result = [];
|
|
|
|
for(var key in _dict) {
|
|
|
|
result.push(_dict[key][_originalKeyFieldName]);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
return mapImpl;
|
|
|
|
}
|