Get Hash

In order to check duplicates inside objects getHash function can be specified, it should return some string which expected to be unique inside the data set, if there are duplicates it will be considered as corruption.

Valid

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

const USER_META = new ObjectMeta<User>({
   builder: User,
   getHash: ({ id }) => id,
   fields: {
      id: new StringField(),
      name: new StringField(),
   }
});

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

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

const users = result.converted.all;

console.log(users);

Corrupted

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

const USER_META = new ObjectMeta<User>({
   builder: User,
   getHash: ({ id }) => id,
   fields: {
      id: new StringField(),
      name: new StringField(),
   }
});

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

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

const users = result.converted.all;

console.log(JTC.log.asString(result.tree)); // 0, 2 -> 1 | Duplicated hash
console.log(users);
/**/
console.log(result.origin.duplicates);
console.log(result.converted.duplicates);

Last updated

Was this helpful?