Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,11 @@ diff(old, new, { arrayIdentityKeys: { tags: '$value' } });
#### Path Skipping

```typescript
// Skip an exact path and all its children
diff(old, new, { keysToSkip: ['characters.metadata'] });

// Skip all children of a path, but still detect ADD/REMOVE of the node itself
diff(old, new, { keysToSkip: ['characters.metadata.*'] });
```

#### Type Change Handling
Expand Down
6 changes: 6 additions & 0 deletions src/jsonDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ const compare = (oldObj: any, newObj: any, path: any, keyPath: any, options: Opt
return true;
}

// The current path is inside an excluded parent by wildcard
if (skipPath.endsWith('.*')) {
const basePath = skipPath.slice(0, -2);
return currentPath.startsWith(basePath + '.');
}

// The current path is a parent of the skip path
if (skipPath.includes('.') && skipPath.startsWith(currentPath + '.')) {
return false; // Don't skip, we need to process the parent
Expand Down
47 changes: 47 additions & 0 deletions tests/jsonDiff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,53 @@ describe('jsonDiff#diff', () => {
]);
});

describe('wildcard keysToSkip (property.*)', () => {
const base = {
property: {
name: 'Alice',
address: {
formattedAddress: '123 Main St',
utcOffset: 0,
}
}
};

it('ignores property changes inside the wildcarded key', () => {
const withChangedAddress = {
property: {
name: 'Alice',
address: {
formattedAddress: 'New Address',
utcOffset: 5,
}
}
};
expect(diff(base, withChangedAddress, { keysToSkip: ['property.address.*'] })).toEqual([]);
});

it('detects removal of the wildcarded key itself', () => {
const withoutAddress = { property: { name: 'Alice' } };
expect(diff(base, withoutAddress, { keysToSkip: ['property.address.*'] })).toEqual([
{
type: 'UPDATE',
key: 'property',
changes: [{ type: 'REMOVE', key: 'address', value: base.property.address }]
}
]);
});

it('detects addition of the wildcarded key itself', () => {
const withoutAddress = { property: { name: 'Alice' } };
expect(diff(withoutAddress, base, { keysToSkip: ['property.address.*'] })).toEqual([
{
type: 'UPDATE',
key: 'property',
changes: [{ type: 'ADD', key: 'address', value: base.property.address }]
}
]);
});
});

it.each(fixtures.assortedDiffs)(
'correctly diffs $oldVal with $newVal',
({ oldVal, newVal, expectedReplacement, expectedUpdate }) => {
Expand Down
Loading