Deconvert

JTC.deconvert is the reverse operation to JTC.convert , it will convert class instance into plain object:

  • Remove all instance attributes: calculated fields, functions, symbols and will

  • Restore dates into primitive form

Basic

const APP_INFO = Symbol(`APP_INFO`);

class AppInfo {
   public isSelected = false;
}

class User {
   public id: number;
   public name: string;
   public dob: Date;

   public get short() { return `${this.id} - ${this.name}`; }

   public sayHi() {
      console.log(`Hi`);
   }

   public [APP_INFO]: AppInfo;
}

const USER_META = new ObjectMeta({
   builder: User,
   fields: {
      id: new NumberField(),
      name: new StringField(),
      dob: new StringDateField({ deconvert: date => date.toISOString() }),
      short: new StringField({ isCalculated: true }),
      sayHi: new FunctionField(),
      [APP_INFO]: () => new AppInfo(),
   }
});

const values = [
   {
      id: 1,
      name: `Vasya`,
      dob: `2021-04-11T15:38:20.371Z`,
   },
];

const [user] = JTC.convert({
   id: `Deconvert`,
   meta: new ObjectArrayMeta({ meta: USER_META }),
   values
}).converted.all;


const deconverted = JTC.deconvert({ value: user });

isDeleteEmpty

If isDeleteEmpty flag set to true during JTC.deconvert such fields marked as isNullable true will be removed in such cases:

  • Value is null or undefined

  • Value is an empty string

  • Value is boolean false

  • Value is an empty iterable

leaveAsIs

In some cases there are need to left some fields as class instances. For example, when working with database SDK (firebase...). leaveAsIs can be specified with array of builders which should NOT be deconverted into plain objects.

Last updated