UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

34 lines (33 loc) 757 B
/** * Like Map, but serializes to JSON as an object. * * Fixes the "issue" of stock Map being json-serialized as `{}`. * * @experimental */ export class Map2 extends Map { /** * Convenience way to create Map2 from object. */ static of(obj) { return new Map2(Object.entries(obj)); } /** * Allows to set multiple key-value pairs at once. */ setMany(obj) { for (const [k, v] of Object.entries(obj)) { this.set(k, v); } return this; } toObject() { return Object.fromEntries(this); } toJSON() { return Object.fromEntries(this); } toString() { return `Map2(${this.size}) ${JSON.stringify(Object.fromEntries(this))}`; } }