UNPKG

@beenotung/tslib

Version:
93 lines 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.polyfillMap = exports.PolyfillMap = void 0; const array_1 = require("./array"); const runtime_1 = require("./runtime"); class PolyfillMap { constructor() { this.ks = []; this.vs = []; } get size() { return this.ks.length; } clear() { array_1.clearArray(this.ks); array_1.clearArray(this.vs); } delete(key) { const idx = this.getIndex(key); if (idx === -1) { return false; } array_1.removeByIdx(this.ks, idx); array_1.removeByIdx(this.vs, idx); return true; } forEach(callbackfn, thisArg = this) { for (let i = thisArg.size; i >= 0; i--) { callbackfn(thisArg.vs[i], thisArg.ks[i], thisArg); } } get(key) { const idx = this.getIndex(key); return idx === -1 ? undefined : this.vs[idx]; } has(key) { return this.getIndex(key) !== -1; } set(key, value) { let idx = this.getIndex(key); // const res = idx === -1 ? undefined : this.vs[idx]; if (idx === -1) { idx = this.ks.length; } this.ks[idx] = key; this.vs[idx] = value; return this; } [Symbol.iterator]() { let idx = 0; return { next: () => { const k = this.ks[idx]; const v = this.vs[idx]; idx++; const value = [k, v]; return { done: idx >= this.size, value, }; }, [Symbol.iterator]() { return this; }, }; } entries() { return this[Symbol.iterator](); } keys() { return this.ks[Symbol.iterator](); } values() { return this.vs[Symbol.iterator](); } /** * return -1 if not found * */ getIndex(key) { return this.ks.indexOf(key); } } exports.PolyfillMap = PolyfillMap; function polyfillMap() { if (typeof Map === 'function') { return Map; } const parent = runtime_1.getWindowOrGlobal(); Object.assign(parent, { Map: PolyfillMap }); return PolyfillMap; } exports.polyfillMap = polyfillMap; //# sourceMappingURL=polyfill-map.js.map