forked from montagejs/collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric-map.js
211 lines (187 loc) · 5.94 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"use strict";
var Object = require("./shim-object");
var Iterator = require("./iterator");
module.exports = GenericMap;
function GenericMap() {
throw new Error("Can't construct. GenericMap is a mixin.");
}
// all of these methods depend on the constructor providing a `store` set
GenericMap.prototype.isMap = true;
GenericMap.prototype.addEach = function (values) {
var i;
if (values && Object(values) === values) {
if (typeof values.forEach === "function") {
// copy map-alikes
if (values.isMap === true) {
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 if (typeof values.length === "number") {
// Array-like objects that do not implement forEach, ergo,
// Arguments
for (i = 0; i < values.length; i++) {
this.add(values[i], i);
}
} else {
// copy other objects as map-alikes
Object.keys(values).forEach(function (key) {
this.set(key, values[key]);
}, this);
}
} else if (values && typeof values.length === "number") {
// String
for (i = 0; i < values.length; i++) {
this.add(values[i], i);
}
}
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) {
console.log("Use of a second argument as default value is deprecated to match standards");
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 this;
};
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 () {
var keys, key;
if (this.dispatchesMapChanges) {
this.forEach(function (value, key) {
this.dispatchBeforeMapChange(key, value);
}, this);
keys = this.keysArray();
}
this.store.clear();
this.length = 0;
if (this.dispatchesMapChanges) {
for(var i=0;(key = keys[i]);i++) {
this.dispatchMapChange(key);
}
// keys.forEach(function (key) {
// this.dispatchMapChange(key);
// }, this);
}
};
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.keysArray = function () {
return this.map(function (value, key) {
return key;
});
};
GenericMap.prototype.keys = function () {
return new Iterator(this.keysArray(), true);
};
GenericMap.prototype.valuesArray = function () {
return this.map(Function.identity);
};
GenericMap.prototype.values = function () {
return new Iterator(this.valuesArray(), true);
};
GenericMap.prototype.entriesArray = function () {
return this.map(function (value, key) {
return [key, value];
});
};
GenericMap.prototype.entries = function () {
return new Iterator(this.entriesArray(), true);
};
// XXX deprecated
GenericMap.prototype.items = function () {
return this.entriesArray();
};
GenericMap.prototype.equals = function (that, equals) {
equals = equals || Object.equals;
if (this === that) {
return true;
} else if (that && typeof that.every === "function") {
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.toJSON = function () {
return this.entriesArray();
};
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);
};