import { HAS_MAP, m } from "../constants.js"; import setup from "./proto.js"; import { isIterable } from "../../../util.js"; import { _classCallCheck } from "../../../../shared.js"; function generateMap(fm: FakeMap, it: Iterable | undefined) { if (it == null) return; if (!isIterable(it)) throw new Error("value:" + String(it) + " is not iterable"); const len = (it as Array).length; for (let i = 0; i < len; i++) { const k = (it as Array)[i]; if (!k || k.length !== 2) throw new Error("invalid arg"); fm.set(k[0], k[1]); } } interface FakeMap { clear(): void; delete(key: K): boolean; forEach( callbackfn: (value: V, key: K, map: FakeMap) => void, thisArg?: any ): void; get(key: K): V | undefined; has(key: K): boolean; set(key: K, value: V): this; readonly size: number; [m]: Array<[K, V]>; [Symbol.toStringTag]: string; [Symbol.iterator](): IterableIterator<[K, V]>; values(): IterableIterator; keys(): IterableIterator; entries(): IterableIterator<[K, V]>; } export interface FakeMapConstructor { new (): FakeMap; new (entries?: ReadonlyArray | null): FakeMap; readonly prototype: FakeMap; [Symbol.species]: FakeMapConstructor; } /** * Map implementation * * This Implementation uses One single array to store keys and values in their own arrays * Key index - 0 * Value index - 1 * * we could have used 2 separate Arrays for storing keys and values in matching indices * @TODO do a benchmark */ const FakeMap = (function FakeMap( this: FakeMap, iterable?: Iterable<[K, V]>, forceUseCustomImplementation?: boolean ): FakeMap | Map { _classCallCheck(this, FakeMap); if (!forceUseCustomImplementation && HAS_MAP) return new Map(iterable as Iterable<[K, V]>); this[m] = []; generateMap(this, iterable); return this; } as any) as FakeMapConstructor; setup(FakeMap); FakeMap[Symbol.species] = FakeMap; export default FakeMap;