added selection methods
This commit is contained in:
@@ -4,12 +4,14 @@ class Entity {
|
|||||||
|
|
||||||
constructor(){
|
constructor(){
|
||||||
this.data = new Map();
|
this.data = new Map();
|
||||||
|
this.selectedData = null;
|
||||||
this.count = 0;
|
this.count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
addData(key ,value){
|
addData(key ,value){
|
||||||
if(!this.isData(key)){
|
if(!this.isData(key)){
|
||||||
this.data.set(key, value);
|
this.data.set(key, value);
|
||||||
|
this.selectedData = key;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -26,16 +28,40 @@ class Entity {
|
|||||||
deleteData(key){
|
deleteData(key){
|
||||||
if(this.isData(key)){
|
if(this.isData(key)){
|
||||||
this.data.delete(key);
|
this.data.delete(key);
|
||||||
|
if(this.selectedData == key){
|
||||||
|
this.selectedData = null;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
clearData(){
|
clearData(){
|
||||||
|
this.selectedData = null;
|
||||||
this.data = new Map();
|
this.data = new Map();
|
||||||
this.count = 0;
|
this.count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Select a specific data with its key
|
||||||
|
* @param {string} key
|
||||||
|
* @returns true if a data is selected
|
||||||
|
* @returns false if data is null
|
||||||
|
*/
|
||||||
|
selectData(key){
|
||||||
|
if(this.isData(key))
|
||||||
|
{
|
||||||
|
this.selectedData = key;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} key
|
||||||
|
* @returns the value mapped to the key
|
||||||
|
*/
|
||||||
getData(key){
|
getData(key){
|
||||||
if(this.isData(key)){
|
if(this.isData(key)){
|
||||||
return this.data.get(key);
|
return this.data.get(key);
|
||||||
@@ -43,6 +69,9 @@ class Entity {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns a map of all data
|
||||||
|
*/
|
||||||
getAllData(){
|
getAllData(){
|
||||||
return this.data;
|
return this.data;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,4 +65,53 @@ describe('Entity', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('Test: selectData()', () => {
|
||||||
|
let a = new Entity();
|
||||||
|
a.addData('hello world', new Website('hello world'));
|
||||||
|
a.addData('gada', new Website('gada'));
|
||||||
|
a.addData('pagol', new Website('pagol'));
|
||||||
|
|
||||||
|
a.selectData('gada');
|
||||||
|
|
||||||
|
expect(a.selectedData).to.equals('gada');
|
||||||
|
});
|
||||||
|
|
||||||
|
it.only('Test: selectedData() = false', () => {
|
||||||
|
let a = new Entity();
|
||||||
|
a.addData('hello world', new Website('hello world'));
|
||||||
|
a.addData('gada', new Website('gada'));
|
||||||
|
a.addData('pagol', new Website('pagol'));
|
||||||
|
a.selectData('tara');
|
||||||
|
expect(a.selectedData).to.equals('pagol');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Test: selectedData when addData() is used', () => {
|
||||||
|
let a = new Entity();
|
||||||
|
a.addData('hello world', new Website('hello world'));
|
||||||
|
a.addData('gada', new Website('gada'));
|
||||||
|
a.addData('pagol', new Website('pagol'));
|
||||||
|
|
||||||
|
expect(a.selectedData).to.equals('pagol');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('Test : selectedData when clearData()', () => {
|
||||||
|
let a = new Entity();
|
||||||
|
a.addData('hello world', new Website('hello world'));
|
||||||
|
a.addData('gada', new Website('gada'));
|
||||||
|
a.addData('pagol', new Website('pagol'));
|
||||||
|
|
||||||
|
a.clearData();
|
||||||
|
|
||||||
|
expect(a.count).to.equal(0);
|
||||||
|
expect(a.isData("kruta")).to.false;
|
||||||
|
expect(a.isData("hello")).to.false;
|
||||||
|
expect(a.isData('pagol')).to.false;
|
||||||
|
expect(a.isData('hello world')).to.false;
|
||||||
|
expect(a.selectedData).to.null;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user