chaijs / chaijs/deep-eql

Feature request: Add possibility to exclude/omit keys when comparing objects

Open
#92 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
112
Forks
45
Avg merge
2d 21h
Merged PRs (30d)
1

Description

I encountered the necessity to compare some generated data with test data, where those data sets include dynamically generated id values nested in different levels. So the goal was to test the object for equality by ignoring/ommiting those id_ keys.

E.G.

// test for equality but ignore the values of id
{
  id: 1234,
  a: 'foo',
  b: {
    id: 45678
    c:  'bar'
  }

}

I cerated my own - much simpler deepEql_ function

const deepEql = (a: unknown, b: unknown, omitKeys: string[] = []): boolean => {
    if (a === b) return true;

    omitKeys.forEach(key => delete a[key]);
    omitKeys.forEach(key => delete b[key]);

    if (typeof a !== 'object' || typeof b !== 'object' || a == null || b == null) return false;

    const keysA = Object.keys(a);
    const keysB = Object.keys(b);

    if (keysA.length !== keysB.length) return false;

    // eslint-disable-next-line no-restricted-syntax
    for (const key of keysA) {
      if (!keysB.includes(key)) return false;

      if (typeof a[key] === 'function' || typeof b[key] === 'function') {
        if (a[key].toString() !== b[key].toString()) return false;
      } else if (!this.deepEql(a[key], b[key], omitKeys)) return false;
    }

    return true;
  }

which works in my case, but I would love to see the possibility to specify which keys to exclude in the deep-eql api which seems obviously much more reliable than my solution

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the current deep-eql API and its recursive comparison behavior; the issue does not identify implementation files or tests. Define how callers specify omitted keys and how that option applies at nested levels without mutating inputs. Done means the API supports ignoring matching keys during deep comparisons and has coverage for nested generated ids.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.