Symbols

If there are need to add some additional data on converted objects symbol fields can be specified, during JTC.convert they will be initialized depending on corresponding object.

INIT (default)

In this strategy symbol fields will be initialized during conversion

const APP_INFO = Symbol(`APP_INFO`);

class AppInfo {
   public isSelected: boolean;

   constructor(
      public id: number,
   ) { }
}

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

   public [APP_INFO]: AppInfo;
}

const USER_META = new ObjectMeta<User>({
   builder: User,
   fields: {
      id: new NumberField(),
      name: new StringField(),
      [APP_INFO]: ({ id }) => new AppInfo(id),
   }
});

const values = [
   {
      id: 1,
      name: `Vasya`,
   },
   {
      id: 2,
      name: `Masha`,
   },
];

const result = JTC.convert({
   id: `Symbols`,
   meta: new ObjectArrayMeta({ meta: USER_META }),
   values
});

const users = result.converted.all;

console.log(users);

console.log(users[0][APP_INFO].id === 1); // true
console.log(users[1][APP_INFO].id === 2); // true

SHARE

When converting an object which already have initialized symbols, converted one will share same symbol value with origin

Last updated