dropped unused functions and tests fixed

This commit is contained in:
Wert_Lex 2015-10-30 21:36:50 +06:00
parent 2f31701655
commit d9918d526a
2 changed files with 17 additions and 58 deletions

View File

@ -1,26 +1,7 @@
module.exports.buildOneToOneIdentityMap = buildOneToOneIdentityMap;
module.exports.buildOneToManyIdentityMap = buildOneToManyIdentityMap;
module.exports.buildOneToOneIdentityMapWithOrigKeys = buildOneToOneIdentityMapWithOrigKeys;
module.exports.buildOneToManyIdentityMapWithOrigKeys = buildOneToManyIdentityMapWithOrigKeys;
module.exports.join = join;
module.exports.KVMap = KVMap;
/**
* Effectively builds associative map on id -> object relation.
* 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;
}
/**
* Effectively builds associative map on id -> object relation and stores original keys.
@ -40,26 +21,6 @@ function buildOneToOneIdentityMapWithOrigKeys(objs, idName) {
return kvMap;
}
/**
* 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;
}
function buildOneToManyIdentityMapWithOrigKeys(objs, idName) {
var kvMap = new KVMap();
for(var i = 0; i < objs.length; i++) {

View File

@ -4,16 +4,15 @@ var should = require("should");
var includeUtils = require("../lib/include_utils");
describe('include_util', function(){
describe('#buildOneToOneIdentityMap', function(){
describe('#buildOneToOneIdentityMapWithOrigKeys', function(){
it('should return an object with keys', function(){
var objs = [
{id: 11, letter: "A"},
{id: 22, letter: "B"}
];
var result = includeUtils.buildOneToOneIdentityMap(objs, "id");
result.should.be.an.instanceOf(Object);
result.should.have.property("11");
result.should.have.property("22");
var result = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, "id");
result.get(11).should.be.ok;
result.get(22).should.be.ok;
});
it('should overwrite keys in case of collision', function(){
@ -24,11 +23,12 @@ describe('include_util', function(){
{id: 11, letter: "HA!"}
];
var result = includeUtils.buildOneToOneIdentityMap(objs, "id");
result.should.be.an.instanceOf(Object);
result.should.have.keys("11", "22", "33");
result["11"]["letter"].should.equal("HA!");
result["33"]["letter"].should.equal("C");
var result = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, "id");
result.getKeys().should.containEql(11);
result.getKeys().should.containEql(22);
result.getKeys().should.containEql(33);
result.get(11)["letter"].should.equal("HA!");
result.get(33)["letter"].should.equal("C");
});
});
describe('#buildOneToOneIdentityMapWithOrigKeys', function(){
@ -49,9 +49,9 @@ describe('include_util', function(){
{id: 11, letter: "A"},
{id: 22, letter: "B"}
];
var result = includeUtils.buildOneToManyIdentityMap(objs, "id");
result.should.be.an.instanceOf(Object);
result.should.have.keys("11", "22");
var result = includeUtils.buildOneToManyIdentityMapWithOrigKeys(objs, "id");
result.exist(11).should.be.true;
result.exist(22).should.be.true;
});
it('should collect keys in case of collision', function(){
@ -62,12 +62,10 @@ describe('include_util', function(){
{fk_id: 11, letter: "HA!"}
];
var result = includeUtils.buildOneToManyIdentityMap(objs, "fk_id");
result.should.be.an.instanceOf(Object);
result.should.have.keys("11", "22", "33");
result["11"][0]["letter"].should.equal("A");
result["11"][1]["letter"].should.equal("HA!");
result["33"][0]["letter"].should.equal("C");
var result = includeUtils.buildOneToManyIdentityMapWithOrigKeys(objs, "fk_id");
result.get(11)[0]["letter"].should.equal("A");
result.get(11)[1]["letter"].should.equal("HA!");
result.get(33)[0]["letter"].should.equal("C");
});
});
});