isMatch

Deep partial match: does target contain everything described by pattern?

bool isMatch(Object? target, Object? pattern)

Lecture

isMatch recurses through pattern and checks that target "contains" it, with different rules per shape:

Maps match partially — every key in pattern must exist in target with a recursively-matching value, but target is free to have extra keys the pattern doesn't mention. Lists/iterables match pairwise from the front, and here's the twist worth remembering: the pattern only has to be a prefix of the target. [1, 2, 3] matches the pattern [1, 2], but not the pattern [1, 2, 3, 4] — the pattern can't be longer than the target. Anything else (numbers, strings, booleans, …) is compared with plain ==.

Because the rules nest, you get deep matching almost for free: a pattern like {'address': {'city': 'seoul'}} matches a user map with a much larger, deeply nested address value, as long as city is 'seoul' somewhere inside it.

Demo 1 · Map matching

Demo 2 · List prefix matching, and filtering with it

Try it yourself

Exercise: use isMatch to check whether order matches {'status': 'shipped'}.

Related: matches — the curried, filter-ready version of this · pickBy — often paired for shape-based filtering · find — locate the first matching element · omitBy — drop entries by predicate