typia icon indicating copy to clipboard operation
typia copied to clipboard

Comariang data

Open snatvb opened this issue 1 year ago • 7 comments

Hello, I use typia and noticed that it would be great to have able to compare A and B How it could be:

import typia from 'typia'

type User = {
  name: string
  age: number
  address: {
    room?: number
    city: string
    street: string
  }
}

const ivan: User = {
  name: 'ivan',
  age: 19,
  address: {
    street: 'Ivanovskaya',
    city: 'Berlin'
  }
}

let cache: User | null = null

function heavyFunction(user: User) {
  if (t.compare<User>(user, cache)) {
    return 'cached'
  }
  // heavy operation
  cache = user
  return 'non-cached'
}

heavyFunction(ivan) // -> 'non-cached'
heavyFunction(ivan) // -> 'cached'

Compare unwraps in:

if (
 user === cache || (
   user.name === cache.name &&
   user.age === cache.age &&
  (user.address === cache.address || 
      (user.address.room === cache.address.room && user.address.city === cache.address.city && user.address.street === cache.address.street)
  )
)) {
  return 'cached'
}

snatvb avatar Feb 11 '25 11:02 snatvb

https://github.com/samchon/nestia/blob/master/packages/e2e/src/internal/json_equal_to.ts

Not okay with this function? Transformed comparator is really required?

samchon avatar Feb 12 '25 03:02 samchon

Not okay with this function? Transformed comparator is really required?

Usually I use deepEquals / shallowEquals. But directly comparing should be faster I think

Maybe I didn't get what you mean, could you rephrase your message? Sorry I am not native english speaker 😅

snatvb avatar Feb 12 '25 11:02 snatvb

Oops, missed link https://github.com/samchon/nestia/blob/master/packages/e2e/src/internal/json_equal_to.ts

I can design the new function interface what you want, but I am concentrating the AI chatbot feature development, so if you want that feature, you should challenge the implementation by yourself

samchon avatar Feb 12 '25 11:02 samchon

Oops, missed link https://github.com/samchon/nestia/blob/master/packages/e2e/src/internal/json_equal_to.ts

I can design the new function interface what you want, but I am concentrating the AI chatbot feature development, so if you want that feature, you should challenge the implementation by yourself

I can't use the function because it's internal and it can throw exception I'll investigate how to add it, if you agree that it's good function for your library But I am not sure that I'll rich success 😅 but I can try

snatvb avatar Feb 12 '25 14:02 snatvb

// src/compare.ts
export namespace compare {
  export function covers<T>(x: T, y: T): boolean;
  export function equals<T>(x: T, y: T): boolean;
  export function less<T>(x: T, y: T): boolean;
}

// test.ts
typia.compare.covers(x, y);

I think this interface seems suitable for your feature.

samchon avatar Feb 12 '25 16:02 samchon

// src/compare.ts export namespace compare { export function covers<T>(x: T, y: T): boolean; export function equals<T>(x: T, y: T): boolean; export function less<T>(x: T, y: T): boolean; }

// test.ts typia.compare.covers(x, y); I think this interface seems suitable for your feature.

could you explain how must work "covers" and "less" with objects / arrays?

snatvb avatar Feb 12 '25 17:02 snatvb

I think let's start with equals. I've started and it's harder that I've thought :D I faced with a lot of abstractions

snatvb avatar Feb 12 '25 20:02 snatvb