@beenotung/tslib
Version:
utils library in Typescript
73 lines • 1.47 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleMapMap = exports.MapMap = void 0;
const polyfill_map_1 = require("./polyfill-map");
/**
* map of value or map, aka nested map
*
* example use cases:
* - memorized function parameter lookup
*
* K can be any type
* */
class MapMap {
constructor() {
if (typeof Map === 'function') {
this.m = new Map();
}
else {
this.m = new polyfill_map_1.PolyfillMap();
}
}
has(k) {
return this.m.has(k);
}
get(k) {
return this.m.get(k);
}
set(k, v) {
return this.m.set(k, v);
}
getMap(k) {
if (this.m.has(k)) {
return this.m.get(k);
}
const res = new MapMap();
this.m.set(k, res);
return res;
}
clear() {
this.m.clear();
}
}
exports.MapMap = MapMap;
/**
* Key can only be number, string or symbol
* */
class SimpleMapMap {
constructor() {
this.o = {};
}
has(k) {
return k in this.o;
}
get(k) {
return this.o[k];
}
set(k, v) {
this.o[k] = v;
}
getMap(k) {
if (k in this.o) {
return this.o[k];
}
const res = new SimpleMapMap();
this.o[k] = res;
return res;
}
clear() {
this.o = {};
}
}
exports.SimpleMapMap = SimpleMapMap;
//# sourceMappingURL=map-map.js.map