-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
50 lines (46 loc) · 1.08 KB
/
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
50
'use strict';
var getCID = require("../can-cid").get;
var helpers = require("../helpers");
var CIDMap;
if(typeof Map !== "undefined") {
CIDMap = Map;
} else {
var CIDMap = function(){
this.values = {};
};
CIDMap.prototype.set = function(key, value){
this.values[getCID(key)] = {key: key, value: value};
};
CIDMap.prototype["delete"] = function(key){
var has = getCID(key) in this.values;
if(has) {
delete this.values[getCID(key)];
}
return has;
};
CIDMap.prototype.forEach = function(cb, thisArg) {
helpers.each(this.values, function(pair){
return cb.call(thisArg || this, pair.value, pair.key, this);
}, this);
};
CIDMap.prototype.has = function(key) {
return getCID(key) in this.values;
};
CIDMap.prototype.get = function(key) {
var obj = this.values[getCID(key)];
return obj && obj.value;
};
CIDMap.prototype.clear = function() {
return this.values = {};
};
Object.defineProperty(CIDMap.prototype,"size",{
get: function(){
var size = 0;
helpers.each(this.values, function(){
size++;
});
return size;
}
});
}
module.exports = CIDMap;