-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
49 lines (41 loc) · 1000 Bytes
/
map.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
'use strict';
var arrayRemove = require('./arrayRemove');
module.exports = function Map() {
var entries = [];
var self = this;
self.getEntry = function (key) {
for(var i = 0; i < entries.length; i ++) {
if (entries[i][0] === key) {
return entries[i];
}
}
return null;
};
self.get = function (key) {
var entry = self.getEntry(key);
return (entry !== null)? entry[1]: null;
};
self.set = function (key, value) {
var entry = self.getEntry(key);
if (entry)
entry[1] = value;
else
entries.push([key, value]);
};
self.remove = function (key) {
var entry = self.getEntry(key);
if (! entry)
return null;
arrayRemove(entries, entry);
return entry[1];
};
self.size = function () {
return entries.length;
};
self.forEach = function (callback) {
var tmp = [].concat(entries);
for (var i = 0; i < tmp.length; i ++) {
callback(tmp[i][0], tmp[i][1]);
}
};
};