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

class User {
   public id: number;
   public name: string;
   public isAlive: boolean;
   public marks: number[] = [];
}

const USER_META = new ObjectMeta({
   builder: User,
   fields: {
      id: new NumberField(),
      name: new StringField({ isNullable: true }),
      isAlive: new BooleanField({ isNullable: true }),
      marks: new NumberArrayField({ isNullable: true }),
   }
});


const values = [
   {
      id: 1,
      name: `Vasya`,
      isAlive: false,
   },
   {
      id: 2,
      name: ``,
      isAlive: true,
      marks: [1]
   },
   {
      id: 3,
      name: `Masha`,
      marks: [],
      isAlive: null,
   }
];


const result = JTC.convert({
   id: `Deconvert`,
   meta: new ObjectArrayMeta({ meta: USER_META }),
   values,
});
const users = result.converted.all;
const deconverted = users.map(value => JTC.deconvert({ value, isDeleteEmpty: true }));

console.log(users);
console.log(deconverted);

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.

class Coords {
   public lat: number;
   public lng: number;
}

const COORDS_META = new ObjectMeta({
   builder: Coords,
   fields: {
      lat: new NumberField(),
      lng: new NumberField(),
   }
});

class User {
   public id: number;
   public name: string;
   public coords: Coords;
}

const USER_META = new ObjectMeta({
   builder: User,
   fields: {
      id: new NumberField(),
      name: new StringField({ isNullable: true }),
      coords: new ObjectField({ meta: COORDS_META }),
   }
});

const values = [
   {
      id: 1,
      name: `Vasya`,
      coords: {
         lng: 0,
         lat: 0,
      }
   },
];

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

console.log(user);
console.log(deconverted);

Last updated

Was this helpful?