@microsoft/teams.common
Version:
<p> <a href="https://www.npmjs.com/package/@microsoft/teams.common" target="_blank"> <img src="https://img.shields.io/npm/v/@microsoft/teams.common" /> </a> <a href="https://www.npmjs.com/package/@microsoft/teams.common?activeTab=code"
69 lines (67 loc) • 1.55 kB
JavaScript
'use strict';
class LocalStorage {
_store;
_keys;
_options;
get keys() {
return this._keys;
}
get size() {
return this._store.size;
}
constructor(data = {}, options = {}) {
this._store = new Map(Object.entries(data));
this._keys = Object.keys(data);
this._options = options;
}
get(key) {
this._hit(key);
return this._store.get(key);
}
set(key, value) {
if (!this._hit(key)) {
this._keys.push(key);
}
if (this._options.max) {
if (this._keys.length > this._options.max) {
const k = this._keys.shift();
if (k) {
this._store.delete(k);
}
}
}
this._store.set(key, value);
}
delete(key) {
const i = this._keys.findIndex((k) => key === k);
if (i > -1) {
this._keys.splice(i, 1);
}
this._store.delete(key);
}
toString() {
return JSON.stringify(
Array.from(this._store.entries()).map(([key, value]) => ({
key,
value
})),
null,
2
);
}
_hit(key) {
if (!this._store.has(key)) return false;
if (this._keys[this._keys.length - 1] === key) return true;
const idx = this._keys.findIndex((k) => key === k);
if (idx < 0) return false;
for (let i = idx + 1; i < this._keys.length; i++) {
const tmp = this._keys[i - 1];
this._keys[i - 1] = this._keys[i];
this._keys[i] = tmp;
}
return true;
}
}
exports.LocalStorage = LocalStorage;
//# sourceMappingURL=local-storage.js.map
//# sourceMappingURL=local-storage.js.map