CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-enzyme-shallow-equal

Shallow equality comparison utility function adapted from React's shallowEqual implementation

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

enzyme-shallow-equal

enzyme-shallow-equal provides a shallow equality comparison utility function that compares objects by examining their top-level properties. It's adapted from React's shallowEqual implementation and is used internally by the Enzyme testing library for performance-critical scenarios where deep equality checks would be too expensive.

Package Information

  • Package Name: enzyme-shallow-equal
  • Package Type: npm
  • Language: JavaScript (ES6)
  • Installation: npm install enzyme-shallow-equal

Core Imports

import shallowEqual from "enzyme-shallow-equal";

For CommonJS:

const shallowEqual = require("enzyme-shallow-equal");

Basic Usage

import shallowEqual from "enzyme-shallow-equal";

// Compare simple objects
const obj1 = { name: "Alice", age: 25 };
const obj2 = { name: "Alice", age: 25 };
const obj3 = { name: "Bob", age: 30 };

console.log(shallowEqual(obj1, obj2)); // true
console.log(shallowEqual(obj1, obj3)); // false

// Compare with different references but same content
const a = { user: { id: 1 } };
const b = { user: { id: 1 } };
console.log(shallowEqual(a, b)); // false - shallow comparison only

// Compare primitives
console.log(shallowEqual(42, 42)); // true
console.log(shallowEqual("hello", "hello")); // true
console.log(shallowEqual(null, null)); // true

Capabilities

Shallow Equality Comparison

Performs shallow equality comparison between two values using SameValue equality for primitives and property-by-property comparison for objects.

/**
 * Performs shallow equality comparison between two objects
 * @param {any} objA - First value to compare
 * @param {any} objB - Second value to compare
 * @returns {boolean} true if values are shallowly equal, false otherwise
 */
function shallowEqual(objA, objB);

Comparison Algorithm:

  1. SameValue Check: Uses Object.is() for initial comparison (handles NaN, +0/-0, etc.)
  2. Type Validation: Returns false if either value is falsy or not an object
  3. Key Length Check: Compares number of enumerable properties
  4. Property Comparison: Checks each property using Object.is() and hasOwnProperty()
  5. Key Sorting: Sorts keys before comparison for consistent results

Handles Edge Cases:

  • Primitive values (numbers, strings, booleans, null, undefined)
  • Objects and arrays (shallow comparison only)
  • Functions and regular expressions
  • NaN and Infinity values
  • Empty objects
  • Objects with undefined properties
  • Different property orders

Usage Examples:

import shallowEqual from "enzyme-shallow-equal";

// Primitives - uses SameValue equality
shallowEqual(42, 42);                    // true
shallowEqual(NaN, NaN);                  // true
shallowEqual("hello", "hello");          // true
shallowEqual(null, null);                // true
shallowEqual(undefined, undefined);      // true

// Objects - shallow property comparison
shallowEqual({}, {});                    // true
shallowEqual({ a: 1 }, { a: 1 });       // true
shallowEqual({ a: 1, b: 2 }, { b: 2, a: 1 }); // true (sorted)

// Different lengths
shallowEqual({ a: 1 }, {});              // false
shallowEqual({}, { a: 1 });              // false

// Different values
shallowEqual({ a: 1 }, { a: 2 });        // false
shallowEqual({ a: 1 }, { b: 1 });        // false

// Nested objects (shallow only)
const nested1 = { user: { id: 1 } };
const nested2 = { user: { id: 1 } };
shallowEqual(nested1, nested2);          // false - different object references

// Same reference
const obj = { a: 1 };
shallowEqual(obj, obj);                  // true

// Arrays (treated as objects)
shallowEqual([1, 2], [1, 2]);            // true
shallowEqual([1, [2]], [1, [2]]);        // false - nested array has different reference

// Falsy values
shallowEqual(false, 0);                  // false
shallowEqual(null, undefined);           // false
shallowEqual("", 0);                     // false

// Mixed types
shallowEqual(1, "1");                    // false
shallowEqual({}, []);                    // false

Install with Tessl CLI

npx tessl i tessl/npm-enzyme-shallow-equal
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/enzyme-shallow-equal@1.0.x
Publish Source
CLI
Badge
tessl/npm-enzyme-shallow-equal badge