Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comariang data #1497

Open
snatvb opened this issue Feb 11, 2025 · 7 comments
Open

Comariang data #1497

snatvb opened this issue Feb 11, 2025 · 7 comments
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@snatvb
Copy link

snatvb commented Feb 11, 2025

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'
}
@samchon
Copy link
Owner

samchon commented Feb 12, 2025

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 samchon self-assigned this Feb 12, 2025
@samchon samchon added enhancement New feature or request question Further information is requested labels Feb 12, 2025
@snatvb
Copy link
Author

snatvb commented Feb 12, 2025

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 😅

@samchon
Copy link
Owner

samchon commented Feb 12, 2025

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

@snatvb
Copy link
Author

snatvb commented Feb 12, 2025

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

@samchon
Copy link
Owner

samchon commented Feb 12, 2025

// 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 samchon assigned snatvb and unassigned samchon Feb 12, 2025
@snatvb
Copy link
Author

snatvb commented Feb 12, 2025

// src/compare.ts
export namespace compare {
export function covers(x: T, y: T): boolean;
export function equals(x: T, y: T): boolean;
export function less(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
Copy link
Author

snatvb commented Feb 12, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants