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; propOne: string;
propTwo: string; propTwo: string;
} }
let testObjectmap: lik.Objectmap<ITestObject>; let testObjectmap: lik.ObjectMap<ITestObject>;
let testObject1: ITestObject = { let testObject1: ITestObject = {
propOne: 'hello', propOne: 'hello',
propTwo: 'hello2' propTwo: 'hello2'
@ -22,8 +22,8 @@ let testObject2: ITestObject = {
}; };
tap.test('new lik.Objectmap() -> should correctly instantiate an Objectmap', async () => { tap.test('new lik.Objectmap() -> should correctly instantiate an Objectmap', async () => {
testObjectmap = new lik.Objectmap<ITestObject>(); testObjectmap = new lik.ObjectMap<ITestObject>();
expect(testObjectmap).be.instanceof(lik.Objectmap); expect(testObjectmap).be.instanceof(lik.ObjectMap);
}); });
tap.test('lik.Objectmap.add() -> should correctly add an object to Objectmap', async () => { 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; return this.mapObject[keyArg] ? false : true;
} }
public addToMap(keyArg: string, objectArg: T): boolean { public addToMap(
if (this.isUniqueKey(keyArg)) { keyArg: string,
objectArg: T,
optionsArg?: {
force: boolean;
}
): boolean {
if (this.isUniqueKey(keyArg) || (optionsArg && optionsArg.force)) {
this.mapObject[keyArg] = objectArg; this.mapObject[keyArg] = objectArg;
return true; return true;
} else { } else {
@ -32,9 +38,9 @@ export class FastMap<T> {
public getKeys() { public getKeys() {
const keys: string[] = []; const keys: string[] = [];
for (const keyArg in this.mapObject) { for (const keyArg in this.mapObject) {
if (this.mapObject[keyArg]) { if (this.mapObject[keyArg]) {
keys.push(keyArg); keys.push(keyArg);
} }
} }
return keys; return keys;
} }
@ -42,4 +48,35 @@ export class FastMap<T> {
public clean() { public clean() {
this.mapObject = {}; 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 * as plugins from './lik.plugins';
import { Objectmap } from './lik.objectmap'; import { ObjectMap } from './lik.objectmap';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
@ -11,7 +11,7 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
/** /**
* stores interests that are currently fullfilled by the cache * 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 * a function to compare interests

View File

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

View File

@ -12,7 +12,7 @@ export interface IObjectmapFindFunction<T> {
/** /**
* allows keeping track of objects * allows keeping track of objects
*/ */
export class Objectmap<T> { export class ObjectMap<T> {
private fastMap = new FastMap<T>(); private fastMap = new FastMap<T>();
// events // events
@ -186,4 +186,23 @@ export class Objectmap<T> {
this.fastMap.removeFromMap(keyArg); 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);
}
} }