fix(isMatch): handle primitive targets with object source patterns correctly #1432
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Correct isMatch primitive target vs object pattern matching
Fixes #1399
🐛 Problem
Two incorrect matches versus Lodash were identified.
Bug 1: Top-level primitive target vs object source (with undefined)
Case:
Observed behavior before fix:
Root cause:
undefined:These two
undefinedvalues accidentally compare as equal, yielding a false positive.Bug 2: Nested level primitive vs empty object
Case:
Observed behavior before fix:
Why it is confusing:
isMatch('bar', {})should be true (empty object = no constraints)isMatch('bar', {})must be false (type mismatch vs object pattern)Lodash intent:
ES-Toolkit previously treated empty objects as truthy matches at all levels.
🔧 Solution
Fixes Bug 1:
Rationale:
stack.size === 0),{}means "no constraints" -> allow any targetstack.size > 0),{}still implies object type -> primitives must failImplementation detail:
isMatchWithcreates a new stack (new Map()) at the entry point.stack.set(...)), sostack.sizenaturally reflects depth.🧪 Tests
Added to
src/compat/predicate/isMatch.spec.ts.Result:
📊 Summary of Changes
Modified files:
src/compat/predicate/isMatchWith.ts(logic updates)src/compat/predicate/isMatch.spec.ts(new test cases)Code added (conceptually):
Only a few lines, but fully aligns with Lodash behavior.
🧭 Behavior vs Lodash
Before (incorrect):
After (fixed):
✅ Checklist
isMatchfromes-toolkit/compatdoes not behave likelodash-es/isMatch#1399