forked from montagejs/collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric-map.js
167 lines (145 loc) · 4.65 KB
/
generic-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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use strict";
var Object = require("./shim-object");
var MapChanges = require("./listen/map-changes");
var PropertyChanges = require("./listen/property-changes");
module.exports = GenericMap;
function GenericMap() {
throw new Error("Can't construct. GenericMap is a mixin.");
}
Object.addEach(GenericMap.prototype, MapChanges.prototype);
Object.addEach(GenericMap.prototype, PropertyChanges.prototype);
// all of these methods depend on the constructor providing a `store` set
GenericMap.prototype.addEach = function (values) {
if (values && Object(values) === values) {
if (typeof values.forEach === "function") {
// copy map-alikes
if (typeof values.keys === "function") {
values.forEach(function (value, key) {
this.set(key, value);
}, this);
// iterate key value pairs of other iterables
} else {
values.forEach(function (pair) {
this.set(pair[0], pair[1]);
}, this);
}
} else {
// copy other objects as map-alikes
Object.keys(values).forEach(function (key) {
this.set(key, values[key]);
}, this);
}
}
return this;
}
GenericMap.prototype.get = function (key, defaultValue) {
var item = this.store.get(new this.Item(key));
if (item) {
return item.value;
} else if (arguments.length > 1) {
return defaultValue;
} else {
return this.getDefault(key);
}
};
GenericMap.prototype.set = function (key, value) {
var item = new this.Item(key, value);
var found = this.store.get(item);
var grew = false;
if (found) { // update
if (this.dispatchesMapChanges) {
this.dispatchBeforeMapChange(key, found.value);
}
found.value = value;
if (this.dispatchesMapChanges) {
this.dispatchMapChange(key, value);
}
} else { // create
if (this.dispatchesMapChanges) {
this.dispatchBeforeMapChange(key, undefined);
}
if (this.store.add(item)) {
this.length++;
grew = true;
}
if (this.dispatchesMapChanges) {
this.dispatchMapChange(key, value);
}
}
return grew;
};
GenericMap.prototype.add = function (value, key) {
return this.set(key, value);
};
GenericMap.prototype.has = function (key) {
return this.store.has(new this.Item(key));
};
GenericMap.prototype['delete'] = function (key) {
var item = new this.Item(key);
if (this.store.has(item)) {
var from = this.store.get(item).value;
if (this.dispatchesMapChanges) {
this.dispatchBeforeMapChange(key, from);
}
this.store["delete"](item);
this.length--;
if (this.dispatchesMapChanges) {
this.dispatchMapChange(key, undefined);
}
return true;
}
return false;
};
GenericMap.prototype.clear = function () {
this.store.clear();
this.length = 0;
};
GenericMap.prototype.reduce = function (callback, basis, thisp) {
return this.store.reduce(function (basis, item) {
return callback.call(thisp, basis, item.value, item.key, this);
}, basis, this);
};
GenericMap.prototype.reduceRight = function (callback, basis, thisp) {
return this.store.reduceRight(function (basis, item) {
return callback.call(thisp, basis, item.value, item.key, this);
}, basis, this);
};
GenericMap.prototype.keys = function () {
return this.map(function (value, key) {
return key;
});
};
GenericMap.prototype.values = function () {
return this.map(Function.identity);
};
GenericMap.prototype.items = function () {
return this.map(function (value, key) {
return [key, value];
});
};
GenericMap.prototype.equals = function (that, equals) {
equals = equals || Object.equals;
if (this === that) {
return true;
} else if (Object.can(that, "every")) {
return that.length === this.length && that.every(function (value, key) {
return equals(this.get(key), value);
}, this);
} else {
var keys = Object.keys(that);
return keys.length === this.length && Object.keys(that).every(function (key) {
return equals(this.get(key), that[key]);
}, this);
}
};
GenericMap.prototype.Item = Item;
function Item(key, value) {
this.key = key;
this.value = value;
}
Item.prototype.equals = function (that) {
return Object.equals(this.key, that.key) && Object.equals(this.value, that.value);
};
Item.prototype.compare = function (that) {
return Object.compare(this.key, that.key);
};