-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.js
60 lines (50 loc) · 1.06 KB
/
Dictionary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var Dictionary = function() {
this.keys = {};
this.length = 0;
this.defaultValue = null;
};
Dictionary.prototype.store = function(key, value) {
console.log('store key:'+key);
this.keys[key] = value;
this.length++;
};
Dictionary.prototype.fetch = function(key) {
var value = this.keys[key];
if (value) {
return value;
} else {
if (this.defaultValue) return this.defaultValue;
return null;
}
};
Dictionary.prototype.hasKey = function(key) {
for (var k in this.keys) {
if (key == k) {
return true;
} else {
return false;
}
};
return false;
};
Dictionary.prototype.remove = function(key) {
if (this.keys[key]) {
delete this.keys[key];
this.length--;
}
};
Dictionary.prototype.reject = function(callback) {
for (var k in this.keys) {
if (callback(k, this.keys[k])) {
delete this.keys[k];
}
}
};
Dictionary.prototype.random = function() {
var keys = [];
for (var k in this.keys) {
keys.push(k);
}
return keys[Math.floor(Math.random() * keys.length)];
};
module.exports = Dictionary;