blob: bb87fd1308fb8f47dff0334571a6d2ddd213a583 [file] [log] [blame]
// based on https://github.com/montagejs/collections
function mangle(key) {
return '$' + key;
}
function unmangle(key) {
return key.substring(1);
}
function LazyMap() {
this._store = {};
}
LazyMap.prototype.get = function (key) {
var mangled = mangle(key);
return this._store[mangled];
};
LazyMap.prototype.set = function (key, value) {
var mangled = mangle(key);
this._store[mangled] = value;
return true;
};
LazyMap.prototype.has = function (key) {
var mangled = mangle(key);
return mangled in this._store;
};
LazyMap.prototype.delete = function (key) {
var mangled = mangle(key);
var res = mangled in this._store;
delete this._store[mangled];
return res;
};
LazyMap.prototype.forEach = function (cb) {
var keys = Object.keys(this._store);
for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i];
var value = this._store[key];
key = unmangle(key);
cb(value, key);
}
};
Object.defineProperty(LazyMap.prototype, 'size', {
get: function () {
return Object.keys(this._store).length;
}
});
function LazySet(array) {
this._store = new LazyMap();
// init with an array
if (array && Array.isArray(array)) {
for (var i = 0, len = array.length; i < len; i++) {
this.add(array[i]);
}
}
}
LazySet.prototype.add = function (key) {
return this._store.set(key, true);
};
LazySet.prototype.has = function (key) {
return this._store.has(key);
};
export {
LazySet as Set,
LazyMap as Map
};