82 lines
2.1 KiB
JavaScript
82 lines
2.1 KiB
JavaScript
/* IMPORT */
|
|
import Utils from './utils.js';
|
|
/* MAIN */
|
|
//TODO: Maybe publish this as a standalone module
|
|
class LazyMapSet {
|
|
constructor() {
|
|
/* VARIABLES */
|
|
this.map = new Map();
|
|
}
|
|
/* API */
|
|
clear() {
|
|
this.map.clear();
|
|
}
|
|
delete(key, value) {
|
|
if (Utils.lang.isUndefined(value)) {
|
|
return this.map.delete(key);
|
|
}
|
|
else if (this.map.has(key)) {
|
|
const values = this.map.get(key);
|
|
if (Utils.lang.isSet(values)) {
|
|
const deleted = values.delete(value);
|
|
if (!values.size) {
|
|
this.map.delete(key);
|
|
}
|
|
return deleted;
|
|
}
|
|
else if (values === value) {
|
|
this.map.delete(key);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
find(key, iterator) {
|
|
if (this.map.has(key)) {
|
|
const values = this.map.get(key);
|
|
if (Utils.lang.isSet(values)) {
|
|
return Array.from(values).find(iterator);
|
|
}
|
|
else if (iterator(values)) { //TSC
|
|
return values;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
get(key) {
|
|
return this.map.get(key);
|
|
}
|
|
has(key, value) {
|
|
if (Utils.lang.isUndefined(value)) {
|
|
return this.map.has(key);
|
|
}
|
|
else if (this.map.has(key)) {
|
|
const values = this.map.get(key);
|
|
if (Utils.lang.isSet(values)) {
|
|
return values.has(value);
|
|
}
|
|
else {
|
|
return (values === value);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
set(key, value) {
|
|
if (this.map.has(key)) {
|
|
const values = this.map.get(key);
|
|
if (Utils.lang.isSet(values)) {
|
|
values.add(value);
|
|
}
|
|
else if (values !== value) {
|
|
this.map.set(key, new Set([values, value])); //TSC
|
|
}
|
|
}
|
|
else {
|
|
this.map.set(key, value);
|
|
}
|
|
return this;
|
|
}
|
|
}
|
|
/* EXPORT */
|
|
export default LazyMapSet;
|