get-flat

Turn class instances into plain objects based on accessors

Usage no npm install needed!

<script type="module">
  import getFlat from 'https://cdn.skypack.dev/get-flat';
</script>

README

get-flat

Turn class instances into plain js objects making use of the instance's getters.

Turn this:

class Hat {
  private _hatName: string;

  constructor(hatName: string) {
    this._hatName = hatName;
  }

  public get hatName() {
    return this._hatName;
  }
}

Into this:

{
  hatName: 'name';
}

By doing this:

import { ObjectSerializer } from 'get-flat';

const serializer = new ObjectSerializer();

const hat = new Hat('name');
const flatHat = serializer.toPlainObject(hat);