Skip to content

Commit 789516c

Browse files
committed
Dictionary 🏁
1 parent 3441c6d commit 789516c

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

examples/datastructures/dictionary.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Dictionary {
2+
constructor() {
3+
this.dataStore = [];
4+
}
5+
6+
set(key,value) {
7+
this.dataStore[key] = value;
8+
}
9+
10+
get(key) {
11+
return this.dataStore[key];
12+
}
13+
14+
remove(key) {
15+
delete this.dataStore[key];
16+
}
17+
18+
showAll() {
19+
let str = "";
20+
for(let key of Object.keys(this.dataStore))
21+
str += key + " -> " + this.dataStore[key] + "\n";
22+
return str;
23+
}
24+
25+
clear() {
26+
this.dataStore = [];
27+
}
28+
}
29+
30+
module.exports = Dictionary;
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const expect = require('chai').expect;
2+
const Dictionary = require('../../examples/datastructures/dictionary');
3+
4+
describe('=> DICTIONARY', function() {
5+
let numbers;
6+
before(function () {
7+
numbers = new Dictionary();
8+
});
9+
10+
it('should create an Empty Dictionary', function(done) {
11+
expect(numbers.dataStore).to.deep.equal([]);
12+
done();
13+
});
14+
15+
it('should set a <key,value> pair in the Dictionary', function(done) {
16+
numbers.set("Champu", 3370123459);
17+
numbers.set("Zhampak", 123991283);
18+
numbers.set("Raju", 7118780921);
19+
expect(numbers.dataStore["Champu"]).to.equal(3370123459);
20+
expect(numbers.dataStore["Zhampak"]).to.equal(123991283);
21+
expect(numbers.dataStore["Raju"]).to.equal(7118780921);
22+
done();
23+
});
24+
25+
it('should get a value from the Dictionary', function(done) {
26+
expect(numbers.get("Champu")).to.equal(3370123459);
27+
expect(numbers.get("Zhampak")).to.equal(123991283);
28+
expect(numbers.get("Raju")).to.equal(7118780921);
29+
done();
30+
});
31+
32+
it('should show all values from the Dictionary', function(done) {
33+
expect(numbers.showAll()).to.equal("Champu -> 3370123459\nZhampak -> 123991283\nRaju -> 7118780921\n");
34+
done();
35+
});
36+
37+
it('should remove a value from the Dictionary', function(done) {
38+
numbers.remove("Raju");
39+
expect(numbers.get("Raju")).to.equal(undefined);
40+
done();
41+
});
42+
43+
describe('=> clear', function() {
44+
it('should clear the Dictionary', function(done) {
45+
numbers.clear();
46+
expect(numbers.dataStore).to.deep.equal([]);
47+
done();
48+
});
49+
});
50+
51+
});

0 commit comments

Comments
 (0)