diff --git a/src/compat/predicate/isMatch.spec.ts b/src/compat/predicate/isMatch.spec.ts index 073ca4d79..58213df20 100644 --- a/src/compat/predicate/isMatch.spec.ts +++ b/src/compat/predicate/isMatch.spec.ts @@ -316,6 +316,24 @@ describe('isMatch', () => { delete numberProto.b; }); + it('should return `false` when target is primitive and source is object with undefined values (Issue #1399)', () => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + expect(isMatch('bar', { anyKey: undefined })).toBe(false); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + expect(isMatch(123, { anyKey: undefined })).toBe(false); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + expect(isMatch(true, { anyKey: undefined })).toBe(false); + }); + + it('should return `false` when target is primitive and source is empty object nested (Issue #1399)', () => { + expect(isMatch({ value: 'bar' }, { value: {} })).toBe(false); + expect(isMatch({ value: 123 }, { value: {} })).toBe(false); + expect(isMatch({ value: true }, { value: {} })).toBe(false); + }); + it(`should return \`false\` when \`object\` is nullish`, () => { // eslint-disable-next-line no-sparse-arrays const values = [, null, undefined]; diff --git a/src/compat/predicate/isMatchWith.ts b/src/compat/predicate/isMatchWith.ts index ddcaabdfe..823060a58 100644 --- a/src/compat/predicate/isMatchWith.ts +++ b/src/compat/predicate/isMatchWith.ts @@ -206,7 +206,14 @@ function isObjectMatch( const keys = Object.keys(source as any); if (target == null) { - return keys.length === 0; + if (stack && stack.size > 0 && !isObject(target)) { + return false; + } + return true; + } + + if (!isObject(target)) { + return false; } if (keys.length === 0) {