Skip to content

Comparing data #1497

Description

@snatvb

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'
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestquestionFurther information is requested

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions