Skip to content

⬅️ Back to Table of Contents

📄 shallowEqual

📊 Analysis Summary

Metric Count
🔧 Functions 1

📚 Table of Contents

🛠️ File Location:

📂 packages/website/src/components/lib/shallowEqual.ts

Functions

shallowEqual(object1: T | null | undefined, object2: T | null | undefined): boolean

Shallowly compare two objects.

Raw JSDoc
/**
 * Shallowly compare two objects.
 */

Calls:

  • Object.keys

Internal Comments:

// We'd love to be all proper and use a type predicate earlier, but:
// https://github.com/microsoft/TypeScript/issues/26916
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

Code
export function shallowEqual<T extends Record<PropertyKey, unknown>>(
  object1: T | null | undefined,
  object2: T | null | undefined,
): boolean {
  if (object1 === object2) {
    return true;
  }
  const keys1 = Object.keys(object1 ?? {});
  const keys2 = Object.keys(object2 ?? {});
  if (keys1.length !== keys2.length) {
    return false;
  }
  for (const key of keys1) {
    // We'd love to be all proper and use a type predicate earlier, but:
    // https://github.com/microsoft/TypeScript/issues/26916
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    if (object1![key] !== object2![key]) {
      return false;
    }
  }
  return true;
}

Generated by Syntax Scribe