Skip to content

Instantly share code, notes, and snippets.

@edouardmisset
Created January 2, 2024 15:11
Show Gist options
  • Save edouardmisset/239aec9d1f13209eb4d282ae92ce2638 to your computer and use it in GitHub Desktop.
Save edouardmisset/239aec9d1f13209eb4d282ae92ce2638 to your computer and use it in GitHub Desktop.
The code snippet defines a function called `shallowComparison` that performs a shallow comparison between two objects of the same type. It checks if both objects have the same keys and if the values for these keys are the same in both objects

Shallow Object Comparison

Preview:
import { ObjectType } from './type-helpers'

/**
 * @description Performs a shallow comparison between two objects of the same type.
 * It checks if both objects have the same keys and if the values for these keys are the same in both objects.
 *
 * @template T The type of the objects to compare.
 * @param {T} leftObject The first object to compare.
 * @param {T} rightObject The second object to compare.
 * @returns {boolean} True if the objects have the same keys and the same values for each key, false otherwise.
 *
 * @example
 * const obj1 = { a: 1, b: 2 };
 * const obj2 = { b: 2, a: 1 };
 * const obj3 = { a: 1, b: 3 };
 * console.log(shallowComparison(obj1, obj2)); // Outputs: true
 * console.log(shallowComparison(obj1, obj3)); // Outputs: false
 */
export const shallowComparison = <T extends ObjectType>(
  leftObject: T,
  rightObject: T,
): boolean => {
  const leftKeys = Object.keys(leftObject).sort()
  const rightKeys = Object.keys(rightObject).sort()

  if (leftKeys.length !== rightKeys.length) return false

  return leftKeys.every(
    (key, index) =>
      key === rightKeys[index] && leftObject[key] === rightObject[key],
  )
}

/**
 * @description Alias for the {@link shallowComparison} function.
 */
export const isEqual = shallowComparison
Associated Context
Type Code Snippet ( .ts )
Associated Tags shallow-comparison.ts GitHub: my-learning-curve type-helpers shallow comparison objectKeys ObjectType template T leftObject rightObject isEqual sort method reactjs
Associated Commit Messages refactor: remove function
object shallow comparison
refactor: refactor shallowComparison function and remove duplicate
💡 Smart Description This code snippet defines a function called isEqual that takes two objects as input. It checks if both objects have the same keys and values for each key in those objects are the same in both objects, then compares them by comparing their corresponding
The code snippet defines a function called shallowComparison that performs a shallow comparison between two objects of the same type. It checks if both objects have the same keys and if the values for these keys are the same in both objects. The function
🔎 Suggested Searches How to compare two objects of a specific type in TypeScript?
What is the purpose of shallowComparison function in TypeScript?
How to check if two objects have same keys and their values for each key in an object using JavaScript?
how to use ObjectKeys() method with deep equality testing ?
To find whether two objects can be compared by different keys or not
How to perform a shallow comparison between two objects in TypeScript
TypeScript function for comparing objects by keys and values
TypeScript shallowComparison function example
TypeScript isEqual function definition
TypeScript import statement for objectKeys and ObjectType
Related Links https://github.com/edouardmisset/my-learning-curve
https://github.com/edouardmisset/my-learning-curve/blob/bbdc343e81e85cc30e018a85bfee00f721c0b5b2/typescript/shallow-comparison.ts
https://github.com/edouardmisset/my-learning-curve/commits
https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/tutorial/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
https://www.typescriptlang.org/docs/handbook/utility-types.html#objectkeys
https://www.typescriptlang.org/docs/handbook/2/objects.html#object-types-as-function-parameters
https://www.typescriptlang.org/docs/handbook/2/objects.html#object-types-as-maps
https://www.typescriptlang.org/docs/handbook/2/objects.html#object-types
https://www.typescriptlang.org/docs/handbook/2/objects.html#object-types-as-function-return-types
https://www.typescriptlang.org/docs/handbook/2/objects.html#keyof-type-operators
https://www.typescriptlang.org/docs/handbook/2/objects.html#object-types-as-dictionaries
https://www.npmjs.com/package/object-keys
https://www.npmjs.com/package/@types/object-keys
Related People Edouard Misset, Edouard
Sensitive Information No Sensitive Information Detected
Shareable Link https://edouardmisset.pieces.cloud/?p=44f647a7d5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment