UNPKG

@mousepox/util

Version:

Miscellaneous utilities

146 lines (145 loc) 4.73 kB
"use strict"; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Collection = void 0; var Signal_1 = require("./Signal"); var Collection = (function () { function Collection() { this.onAdd = new Signal_1.Signal(); this.onRemove = new Signal_1.Signal(); this.things = new Map(); } Object.defineProperty(Collection.prototype, "size", { get: function () { return this.things.size; }, enumerable: false, configurable: true }); Collection.prototype.clear = function () { this.things.clear(); }; Collection.prototype.dispose = function () { this.onAdd.purge(); this.onRemove.purge(); this.clear(); }; Collection.prototype.add = function (key, thing) { this.things.set(key, thing); this.onAdd.emit(thing); }; Collection.prototype.remove = function (key) { var thing = this.things.get(key); if (thing !== undefined) { this.things.delete(key); this.onRemove.emit(thing); } }; Collection.prototype.get = function (key) { return this.things.get(key); }; Collection.prototype.each = function (fn, filter) { var e_1, _a; try { for (var _b = __values(this.things), _c = _b.next(); !_c.done; _c = _b.next()) { var _d = __read(_c.value, 2), key = _d[0], thing = _d[1]; if (filter === undefined || filter(thing)) { fn(thing, key); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } }; Collection.prototype.count = function (filter) { var e_2, _a; var count = 0; try { for (var _b = __values(this.things), _c = _b.next(); !_c.done; _c = _b.next()) { var _d = __read(_c.value, 2), thing = _d[1]; if (filter(thing)) { count++; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } return count; }; Collection.prototype.filter = function (filter) { var e_3, _a; var things = []; try { for (var _b = __values(this.things), _c = _b.next(); !_c.done; _c = _b.next()) { var _d = __read(_c.value, 2), thing = _d[1]; if (filter === undefined || filter(thing)) { things.push(thing); } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_3) throw e_3.error; } } return things; }; Collection.prototype.first = function (filter) { var e_4, _a; try { for (var _b = __values(this.things), _c = _b.next(); !_c.done; _c = _b.next()) { var _d = __read(_c.value, 2), thing = _d[1]; if (filter(thing)) { return thing; } } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_4) throw e_4.error; } } }; return Collection; }()); exports.Collection = Collection;