@beenotung/tslib
Version:
utils library in Typescript
72 lines (71 loc) • 1.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleMapMap = exports.MapMap = void 0;
/**
* map of value or map, aka nested map
*
* example use cases:
* - memorized function parameter lookup
*
* K can be any type
* */
class MapMap {
m;
constructor() {
this.m = new Map();
}
has(k) {
return this.m.has(k);
}
get(k) {
return this.m.get(k);
}
set(k, v) {
return this.m.set(k, v);
}
/**
* @returns true if an element in the Map existed and has been removed, or false if the element does not exist.
*/
delete(k) {
return this.m.delete(k);
}
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 {
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;