mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-27 13:29:34 +07:00
* Remove docs folder. This was a redirect from a GitHub pages site that does not appear to be in use. * Rename api folder to api-samples. * Move examples to functional-samples folder. * Move cookbook sample to functional-samples. * Move tutorials to functional-samples folder. * Move mv2 and apps folders to _archive. * Rename tools folder to .repo. * Move reference folder to functional-samples. * Update README. Update README with new relative links for reorg. * Update README.md Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com> --------- Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>
92 lines
2.1 KiB
JavaScript
92 lines
2.1 KiB
JavaScript
var Collection = (function () {
|
|
function Collection() {
|
|
this._array = [];
|
|
this._map = {};
|
|
this.length = 0;
|
|
}
|
|
|
|
Collection.prototype.get = function (key) {
|
|
if (this._map.hasOwnProperty(key)) {
|
|
return this._array[this._map[key]].value;
|
|
}
|
|
return undefined;
|
|
};
|
|
|
|
Collection.prototype.getByIndex = function (index) {
|
|
if (this.length <= index) {
|
|
return undefined;
|
|
}
|
|
return this._array[index].value;
|
|
};
|
|
|
|
Collection.prototype.put = function (key, object) {
|
|
if (typeof object === 'undefined') {
|
|
this.remove(key);
|
|
} else if (!this._map.hasOwnProperty(key)) {
|
|
this._map[key] = this._array.length;
|
|
this._array.push({
|
|
value: object,
|
|
key: key
|
|
});
|
|
this.length++;
|
|
} else {
|
|
this._array[this._map[key]].value = object;
|
|
}
|
|
};
|
|
|
|
Collection.prototype.remove = function (key) {
|
|
if (this._map.hasOwnProperty(key)) {
|
|
var index = this._map[key];
|
|
delete this._map[key];
|
|
for (var name in this._map) {
|
|
if (this._map.hasOwnProperty(name) && this._map[name] >= index) {
|
|
this._map[name]--;
|
|
}
|
|
}
|
|
this._array.splice(index, 1);
|
|
this.length--;
|
|
}
|
|
};
|
|
|
|
Collection.prototype.forEach = function (callback, thisObject) {
|
|
if (!thisObject) {
|
|
thisObject = this;
|
|
}
|
|
for (var i = 0, arr = this._array, ln = arr.length; i < ln; i++) {
|
|
if (typeof arr[i].value !== 'undefined') {
|
|
callback.call(thisObject, arr[i].value, i, arr[i].key, this);
|
|
}
|
|
}
|
|
};
|
|
|
|
function defaultCompare(a, b) {
|
|
if (a == b) {
|
|
return 0;
|
|
} else if (a < b) {
|
|
return -1;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
Collection.prototype.sortByKeys = function (cmp) {
|
|
if (!cmp) {
|
|
cmp = defaultCompare;
|
|
}
|
|
this._array.sort(function (a, b) {
|
|
return cmp(a.key, b.key);
|
|
});
|
|
for (var i = 0, arr = this._array, ln = arr.length; i < ln; i++) {
|
|
this._map[arr[i].key] = i;
|
|
}
|
|
};
|
|
|
|
Collection.prototype.keys = function () {
|
|
var result = [];
|
|
this.forEach(function (value, key) {
|
|
result.push(key);
|
|
});
|
|
return result;
|
|
};
|
|
|
|
return Collection;
|
|
})(); |