BREAKING CHANGE(core): refactored ObjectMap and introduced concat feature

This commit is contained in:
Philipp Kunz 2020-05-04 00:09:20 +00:00
parent 5159d7e4bf
commit de04d75e18
5 changed files with 69 additions and 13 deletions

View File

@ -11,7 +11,7 @@ interface ITestObject {
propOne: string;
propTwo: string;
}
let testObjectmap: lik.Objectmap<ITestObject>;
let testObjectmap: lik.ObjectMap<ITestObject>;
let testObject1: ITestObject = {
propOne: 'hello',
propTwo: 'hello2'
@ -22,8 +22,8 @@ let testObject2: ITestObject = {
};
tap.test('new lik.Objectmap() -> should correctly instantiate an Objectmap', async () => {
testObjectmap = new lik.Objectmap<ITestObject>();
expect(testObjectmap).be.instanceof(lik.Objectmap);
testObjectmap = new lik.ObjectMap<ITestObject>();
expect(testObjectmap).be.instanceof(lik.ObjectMap);
});
tap.test('lik.Objectmap.add() -> should correctly add an object to Objectmap', async () => {

View File

@ -10,8 +10,14 @@ export class FastMap<T> {
return this.mapObject[keyArg] ? false : true;
}
public addToMap(keyArg: string, objectArg: T): boolean {
if (this.isUniqueKey(keyArg)) {
public addToMap(
keyArg: string,
objectArg: T,
optionsArg?: {
force: boolean;
}
): boolean {
if (this.isUniqueKey(keyArg) || (optionsArg && optionsArg.force)) {
this.mapObject[keyArg] = objectArg;
return true;
} else {
@ -42,4 +48,35 @@ export class FastMap<T> {
public clean() {
this.mapObject = {};
}
/**
* returns a new Objectmap that includes
*/
public concat(fastMapArg: FastMap<T>) {
const concatedFastmap = new FastMap<T>();
for (const key of this.getKeys()) {
concatedFastmap.addToMap(key, this.getByKey(key));
}
for (const key of fastMapArg.getKeys()) {
concatedFastmap.addToMap(key, fastMapArg.getByKey(key), {
force: true
});
}
return concatedFastmap;
}
/**
* tries to merge another Objectmap
* Note: uniqueKeyCollisions will cause overwrite
* @param objectMapArg
*/
public addAllFromOther(fastMapArg: FastMap<T>) {
for (const key of fastMapArg.getKeys()) {
this.addToMap(key, fastMapArg.getByKey(key), {
force: true
});
}
}
}

View File

@ -1,5 +1,5 @@
import * as plugins from './lik.plugins';
import { Objectmap } from './lik.objectmap';
import { ObjectMap } from './lik.objectmap';
import { Observable } from 'rxjs';
@ -11,7 +11,7 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
/**
* stores interests that are currently fullfilled by the cache
*/
private interestObjectMap = new Objectmap<Interest<DTInterestId, DTInterestFullfillment>>();
private interestObjectMap = new ObjectMap<Interest<DTInterestId, DTInterestFullfillment>>();
/**
* a function to compare interests

View File

@ -1,9 +1,9 @@
import * as plugins from './lik.plugins';
import { Objectmap } from './lik.objectmap';
import { ObjectMap } from './lik.objectmap';
export class LoopTracker<T> {
referenceObjectMap = new Objectmap<any>();
referenceObjectMap = new ObjectMap<any>();
constructor() {
// nothing here
}

View File

@ -12,7 +12,7 @@ export interface IObjectmapFindFunction<T> {
/**
* allows keeping track of objects
*/
export class Objectmap<T> {
export class ObjectMap<T> {
private fastMap = new FastMap<T>();
// events
@ -186,4 +186,23 @@ export class Objectmap<T> {
this.fastMap.removeFromMap(keyArg);
}
}
/**
* returns a new Objectmap that includes
*/
public concat(objectMapArg: ObjectMap<T>) {
const concattedObjectMap = new ObjectMap<T>();
concattedObjectMap.fastMap.addAllFromOther(this.fastMap);
concattedObjectMap.fastMap.addAllFromOther(objectMapArg.fastMap);
return concattedObjectMap;
}
/**
* tries to merge another Objectmap
* Note: uniqueKeyCollisions will cause overwrite
* @param objectMapArg
*/
public addAllFromOther(objectMapArg: ObjectMap<T>) {
this.fastMap.addAllFromOther(objectMapArg.fastMap);
}
}