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
18 changes: 18 additions & 0 deletions src/compat/_internal/isDeepKey.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,23 @@ describe('isDeepKey function', () => {
it('returns false for non-deep keys', () => {
expect(isDeepKey('a')).toBe(false);
expect(isDeepKey(123)).toBe(false);
expect(isDeepKey(Symbol('a'))).toBe(false);
});

it('returns false for invalid deep key patterns', () => {
expect(isDeepKey('a.')).toBe(false);
expect(isDeepKey('.a')).toBe(false);
expect(isDeepKey('a[b')).toBe(false);
expect(isDeepKey('a]b]')).toBe(false);
expect(isDeepKey('a][b')).toBe(false);
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated tests cover a handful of invalid patterns, but they don't cover several false positives the new implementation still allows (e.g. a..b, a.b., a[b][c, a[b]], a[b]c, a.b[). Adding cases like these would prevent regressions and ensure the “strict pattern” behavior described in the PR.

Suggested change
expect(isDeepKey('a][b')).toBe(false);
expect(isDeepKey('a][b')).toBe(false);
expect(isDeepKey('a..b')).toBe(false);
expect(isDeepKey('a.b.')).toBe(false);
expect(isDeepKey('a[b][c')).toBe(false);
expect(isDeepKey('a[b]]')).toBe(false);
expect(isDeepKey('a[b]c')).toBe(false);
expect(isDeepKey('a.b[')).toBe(false);

Copilot uses AI. Check for mistakes.
});

it('returns false for empty string', () => {
expect(isDeepKey('')).toBe(false);
});

it('returns true for valid bracket patterns', () => {
expect(isDeepKey('a[0]')).toBe(true);
expect(isDeepKey('[a]')).toBe(true);
});
});
18 changes: 17 additions & 1 deletion src/compat/_internal/isDeepKey.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/** Matches any deep property path. Examples: `a.b`, `a[0]`, `a["b"]` */
const regexIsDeepProp = /\.|(\[(?:[^[\]]*|(["'])(?:(?!\2)[^\\]|\\.)*?\2)\])/;

/**
* Checks if a given key is a deep key.
*
Expand All @@ -14,6 +17,14 @@
* isDeepKey(123) // false
* isDeepKey('a.b.c') // true
* isDeepKey('a[b][c]') // true
* isDeepKey('a.') // false
* isDeepKey('.a') // false
* isDeepKey('a[b') // false
* isDeepKey('a]b]') // false
* isDeepKey('a][b') // false
* isDeepKey('') // false
* isDeepKey('a[0]') // true
*
*/
export function isDeepKey(key: PropertyKey): boolean {
switch (typeof key) {
Expand All @@ -22,7 +33,12 @@ export function isDeepKey(key: PropertyKey): boolean {
return false;
}
case 'string': {
return key.includes('.') || key.includes('[') || key.includes(']');
if (key === '' || key.startsWith('.') || key.endsWith('.')) return false;

return regexIsDeepProp.test(key);
}
default: {
return false;
}
}
}