@web-atoms/core-docs
Version:
82 lines • 2.83 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
if (typeof Map === "undefined") {
class AtomMap {
constructor() {
this.map = [];
}
get size() {
return this.map.length;
}
clear() {
this.map.length = 0;
}
delete(key) {
return this.map.remove((x) => x.key === key);
}
forEach(callbackfn, thisArg) {
for (const iterator of this.map) {
callbackfn.call(thisArg, iterator.value, iterator.key, this);
}
}
get(key) {
const item = this.getItem(key, false);
return item ? item.value : undefined;
}
has(key) {
return this.map.find((x) => x.key === key) != null;
}
set(key, value) {
const item = this.getItem(key, true);
item.value = value;
return this;
}
// public [Symbol.iterator](): IterableIterator<[K, V]> {
// throw new Error("Method not implemented.");
// }
// public keys(): IterableIterator<K> {
// throw new Error("Method not implemented.");
// }
// public values(): IterableIterator<V> {
// throw new Error("Method not implemented.");
// }
// public get [Symbol.toStringTag](): string {
// return "[Map]";
// }
getItem(key, create = false) {
for (const iterator of this.map) {
if (iterator.key === key) {
return iterator;
}
}
if (create) {
const r = { key, value: undefined };
this.map.push(r);
return r;
}
}
}
// tslint:disable-next-line:no-string-literal
window["Map"] = AtomMap;
}
// tslint:disable-next-line:only-arrow-functions
Map.prototype.getOrCreate = function (key, factory) {
let item = this.get(key);
if (item === undefined) {
item = factory(key);
this.set(key, item);
}
return item;
};
exports.default = Map;
});
//# sourceMappingURL=AtomMap.js.map