forked from montagejs/collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_dict.js
206 lines (180 loc) · 5.58 KB
/
_dict.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
"use strict";
var Shim = require("./shim");
var GenericCollection = require("./generic-collection");
var GenericMap = require("./generic-map");
// Burgled from https://github.com/domenic/dict
module.exports = Dict;
function Dict(values, getDefault) {
if (!(this instanceof Dict)) {
return new Dict(values, getDefault);
}
getDefault = getDefault || Function.noop;
this.getDefault = getDefault;
this.store = Object.create(null);
this.length = 0;
this.addEach(values);
}
Dict.Dict = Dict; // hack so require("dict").Dict will work in MontageJS.
Object.addEach(Dict.prototype, GenericCollection.prototype);
Object.addEach(Dict.prototype, GenericMap.prototype);
Dict.from = GenericCollection.from;
Dict.prototype.constructClone = function (values) {
return new this.constructor(values, this.getDefault);
};
Dict.prototype.assertString = function (key) {
if (typeof key !== "string") {
throw new TypeError("key must be a string but Got " + key);
}
}
Object.defineProperty(Dict.prototype,"$__proto__",{writable:true});
Object.defineProperty(Dict.prototype,"_hasProto",{
get:function() {
return this.hasOwnProperty("$__proto__") && typeof this._protoValue !== "undefined";
}
});
Object.defineProperty(Dict.prototype,"_protoValue",{
get:function() {
return this["$__proto__"];
},
set: function(value) {
this["$__proto__"] = value;
}
});
Object.defineProperty(Dict.prototype,"size",GenericCollection._sizePropertyDescriptor);
Dict.prototype.get = function (key, defaultValue) {
this.assertString(key);
if (key === "__proto__") {
if (this._hasProto) {
return this._protoValue;
} else if (arguments.length > 1) {
return defaultValue;
} else {
return this.getDefault(key);
}
}
else {
if (key in this.store) {
return this.store[key];
} else if (arguments.length > 1) {
return defaultValue;
} else {
return this.getDefault(key);
}
}
};
Dict.prototype.set = function (key, value) {
this.assertString(key);
var isProtoKey = (key === "__proto__");
if (isProtoKey ? this._hasProto : key in this.store) { // update
if (this.dispatchesMapChanges) {
this.dispatchBeforeMapChange(key, isProtoKey ? this._protoValue : this.store[key]);
}
isProtoKey
? this._protoValue = value
: this.store[key] = value;
if (this.dispatchesMapChanges) {
this.dispatchMapChange(key, value);
}
return false;
} else { // create
if (this.dispatchesMapChanges) {
this.dispatchBeforeMapChange(key, undefined);
}
this.length++;
isProtoKey
? this._protoValue = value
: this.store[key] = value;
if (this.dispatchesMapChanges) {
this.dispatchMapChange(key, value);
}
return true;
}
};
Dict.prototype.has = function (key) {
this.assertString(key);
return key === "__proto__" ? this._hasProto : key in this.store;
};
Dict.prototype["delete"] = function (key) {
this.assertString(key);
if (key === "__proto__") {
if (this._hasProto) {
if (this.dispatchesMapChanges) {
this.dispatchBeforeMapChange(key, this._protoValue);
}
this._protoValue = undefined;
this.length--;
if (this.dispatchesMapChanges) {
this.dispatchMapChange(key, undefined);
}
return true;
}
return false;
}
else {
if (key in this.store) {
if (this.dispatchesMapChanges) {
this.dispatchBeforeMapChange(key, this.store[key]);
}
delete this.store[key];
this.length--;
if (this.dispatchesMapChanges) {
this.dispatchMapChange(key, undefined);
}
return true;
}
return false;
}
};
Dict.prototype.clear = function () {
var key;
if (this._hasProto) {
if (this.dispatchesMapChanges) {
this.dispatchBeforeMapChange("__proto__", this._protoValue);
}
this._protoValue = undefined;
if (this.dispatchesMapChanges) {
this.dispatchMapChange("__proto__", undefined);
}
}
for (key in this.store) {
if (this.dispatchesMapChanges) {
this.dispatchBeforeMapChange(key, this.store[key]);
}
delete this.store[key];
if (this.dispatchesMapChanges) {
this.dispatchMapChange(key, undefined);
}
}
this.length = 0;
};
Dict.prototype.reduce = function (callback, basis, thisp) {
if(this._hasProto) {
basis = callback.call(thisp, basis, "$__proto__", "__proto__", this);
}
var store = this.store;
for (var key in this.store) {
basis = callback.call(thisp, basis, store[key], key, this);
}
return basis;
};
Dict.prototype.reduceRight = function (callback, basis, thisp) {
var self = this;
var store = this.store;
basis = Object.keys(this.store).reduceRight(function (basis, key) {
return callback.call(thisp, basis, store[key], key, self);
}, basis);
if(this._hasProto) {
return callback.call(thisp, basis, this._protoValue, "__proto__", self);
}
return basis;
};
Dict.prototype.one = function () {
var key;
for (key in this.store) {
return this.store[key];
}
return this._protoValue;
};
Dict.prototype.toJSON = function () {
return this.toObject();
};