Skip to content

isMatch() function

Performs a partial deep comparison between object and source to determine if object contains all property values from source.

Used as the internal comparison logic for the matches function. source only needs to match a subset of object's properties.

Supported types: primitives, Object (including nested), Array, Date, RegExp, Map, Set.

Signature:

typescript
declare function isMatch(object: unknown, source: unknown): boolean;

Example

ts
// Partial object matching
isMatch({ a: 1, b: 2 }, { a: 1 }); // true
isMatch({ a: 1 }, { a: 1, b: 2 }); // false - object is missing 'b'

// Nested object matching
isMatch({ user: { name: "John", age: 30 } }, { user: { name: "John" } }); // true
isMatch({ user: { name: "John" } }, { user: { name: "Jane" } }); // false

// Array matching (partial, prefix-based)
isMatch([1, 2, 3], [1, 2, 3]); // true
isMatch([1, 2, 3], [1, 2]); // true
isMatch([1, 2], [1, 2, 3]); // false

// Map matching
const map1 = new Map([["key1", "value1"], ["key2", "value2"]]);
const map2 = new Map([["key1", "value1"]]);
isMatch(map1, map2); // true

// Set matching
const set1 = new Set([1, 2, 3]);
const set2 = new Set([1, 2]);
isMatch(set1, set2); // true

// Date type matching
const now = Date.now();
isMatch(new Date(now), new Date(now)); // true

// RegExp type matching
isMatch(/abc/gi, /abc/gi); // true

// Empty source always returns true
isMatch({ a: 1, b: 2 }, {}); // true

Open Source Code

Released under the Apache-2.0 License.