@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 1.23 kB
JavaScript
import{isIterable as r}from"./iteratorUtils.js";import{assumeNonNull as e}from"./maybe.js";class t{constructor(){this._groups=new Map}destroy(){this.removeAll()}get size(){let r=0;return this._groups.forEach((e=>{r+=e.length})),r}add(e,t){if(r(e)){const r=this._getOrCreateGroup(t);for(const t of e)this._isHandle(t)&&r.push(t)}else if(this._isHandle(e)){this._getOrCreateGroup(t).push(e)}return this}forEach(r,e){if("function"==typeof r)this._groups.forEach((e=>e.forEach(r)));else{const t=this._getGroup(r);t&&e&&t.forEach(e)}}has(r){return this._groups.has(this._ensureGroupKey(r))}remove(e){if("string"!=typeof e&&r(e)){for(const r of e)this.remove(r);return this}return this.has(e)?(this._removeAllFromGroup(this._getGroup(e)),this._groups.delete(this._ensureGroupKey(e)),this):this}removeAll(){return this._groups.forEach((r=>this._removeAllFromGroup(r))),this._groups.clear(),this}_isHandle(r){return r&&!!r.remove}_getOrCreateGroup(r){if(this.has(r))return this._getGroup(r);const e=[];return this._groups.set(this._ensureGroupKey(r),e),e}_getGroup(r){return e(this._groups.get(this._ensureGroupKey(r)))}_ensureGroupKey(r){return r||"_default_"}_removeAllFromGroup(r){r.forEach((r=>r.remove()))}}export{t as default};