NaturalCycles/js-lib

View on GitHub
src/object/map2.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
/**
 * Like Map, but serializes to JSON as an object.
 *
 * Fixes the "issue" of stock Map being json-serialized as `{}`.
 *
 * @experimental
 */
export class Map2<K = any, V = any> extends Map<K, V> {
  /**
   * Convenience way to create Map2 from object.
   */
  static of<V>(obj: Record<any, V>): Map2<string, V> {
    return new Map2(Object.entries(obj))
  }

  toObject(): Record<string, V> {
    return Object.fromEntries(this)
  }

  toJSON(): Record<string, V> {
    return Object.fromEntries(this)
  }

  // consider more helpful .toString() ?
}