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 src/compat/predicate/isMatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
expect(isMatch({ a: { b: { c: 1, d: 2 }, e: 3 }, f: 4 }, { a: { b: { c: 1 } } })).toBe(true);
});

it('should return `false` when nested source primitive does not match object target', () => {
expect(isMatch({ a: { b: 2 } }, { a: 1 })).toBe(false);
});

it(`should match boolean values`, () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
Expand Down Expand Up @@ -60,7 +64,7 @@

const Foo = function Foo(this: Foo) {
this.a = 1;
} as any as FooConstructor;

Check warning on line 67 in src/compat/predicate/isMatch.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Foo.prototype.b = 2;

Expand All @@ -80,7 +84,7 @@

const Foo = function Foo(this: Foo) {
this.a = 1;
} as any as FooConstructor;

Check warning on line 87 in src/compat/predicate/isMatch.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Foo.prototype.b = 2;

Expand Down Expand Up @@ -150,7 +154,7 @@

const Foo = function Foo(this: Foo, object: Partial<Foo>) {
Object.assign(this, object);
} as any as FooConstructor;

Check warning on line 157 in src/compat/predicate/isMatch.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

// eslint-disable-next-line
// @ts-ignore
Expand Down Expand Up @@ -285,16 +289,16 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
expect(isMatch(1, { b: undefined })).toBe(true);
} catch (e: any) {

Check warning on line 292 in src/compat/predicate/isMatch.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
expect(false, e.message);

Check warning on line 293 in src/compat/predicate/isMatch.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Expect must have a corresponding matcher call
}

try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
expect(isMatch(1, { a: 1, b: undefined })).toBe(true);
} catch (e: any) {

Check warning on line 300 in src/compat/predicate/isMatch.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
expect(false, e.message);

Check warning on line 301 in src/compat/predicate/isMatch.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Expect must have a corresponding matcher call
}

// eslint-disable-next-line
Expand All @@ -304,8 +308,8 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
expect(isMatch(1, { a: { c: undefined } })).toBe(true);
} catch (e: any) {

Check warning on line 311 in src/compat/predicate/isMatch.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
expect(false, e.message);

Check warning on line 312 in src/compat/predicate/isMatch.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Expect must have a corresponding matcher call
}

// eslint-disable-next-line
Expand Down
4 changes: 4 additions & 0 deletions src/compat/predicate/isMatchWith.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
describe('isMatchWith', () => {
it('should provide correct `customizer` arguments', () => {
const argsList: unknown[] = [];
const object1: any = { a: [1, 2], b: null };

Check warning on line 15 in src/compat/predicate/isMatchWith.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const object2: any = { a: [1, 2], b: null };

object1.b = object2;
Expand Down Expand Up @@ -338,6 +338,10 @@
expect(isMatchWith({}, {}, rootCustomizer)).toBe(true);
});

it('should return `false` when nested source primitive does not match object target', () => {
expect(isMatchWith({ a: { b: 2 } }, { a: 1 }, () => undefined)).toBe(false);
});

it('should handle empty collections', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
Expand Down
20 changes: 13 additions & 7 deletions src/compat/predicate/isMatchWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ export function isMatchWith(
return Boolean(isEqual);
}

return isMatchWithInternal(objValue, srcValue, doesMatch, stack);
return isMatchWithInternal(objValue, srcValue, doesMatch, stack, false);
},
new Map()
new Map(),
true
);
}

Expand All @@ -141,7 +142,8 @@ function isMatchWithInternal(
source: any,
stack?: Map<any, any>
) => boolean | undefined,
stack?: Map<any, any>
stack?: Map<any, any>,
isRoot = false
): boolean {
if (source === target) {
return true;
Expand All @@ -155,7 +157,7 @@ function isMatchWithInternal(
const sourceKeys = Object.keys(source);

if (sourceKeys.length > 0) {
return isMatchWithInternal(target, { ...source }, compare, stack);
return isMatchWithInternal(target, { ...source }, compare, stack, isRoot);
}

return eq(target, source);
Expand All @@ -165,11 +167,15 @@ function isMatchWithInternal(
return eq(target, source);
}

if (typeof source === 'string') {
return source === '';
if (isRoot) {
if (typeof source === 'string') {
return source === '';
}

return true;
}

return true;
return eq(target, source);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/compat/predicate/matches.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ describe('matches', () => {
expect(isMatch5({ a: { b: { c: 1, d: 2 }, e: 3 }, f: 4 })).toBe(true);
});

it('should return `false` when nested source primitive does not match object target', () => {
expect(matches({ a: 1 })({ a: { b: 2 } })).toBe(false);
});

it(`should match inherited string keyed \`object\` properties`, () => {
interface Foo {
a: number;
Expand Down
Loading