tmatch is a utility module designed to facilitate deep and flexible object matching, primarily used by the `t.match()` method in the `tap` test framework. Currently at version 5.0.0, it provides a comprehensive algorithm for comparing a target value against a pattern, supporting various data types including objects, arrays, regular expressions, dates, buffers, and constructor functions. Its matching logic goes beyond shallow equality, handling nested structures and specific type-based comparisons. For instance, it can match strings against regular expressions, check if an object is an `instanceof` a given constructor, or assert the absence of a property using `{propertyName: null}`. While its release cadence is tied to `tap`, it is generally stable. Key differentiators include its detailed, multi-step matching algorithm that accounts for many edge cases and its utility in robust assertion scenarios, offering a more nuanced comparison than standard deep equality checks.
npm install tmatchVerified import paths — ran on the pinned version, not inferred.
Demonstrates `tmatch` for deep object comparison, including regex matching and asserting property absence with `null`.
If your intention is to ensure a property is missing, set its value in the pattern to `null` (e.g., `{ foo: null }`). If you want to match a property that exists and is `undefined`, use `{ foo: undefined }`.Be aware that patterns like `{ myValue: String }` will check `object.myValue instanceof String`, which is true for `new String('hi')` but false for `'hi'` (a string primitive). If you intend to match primitive types, specify the primitive value directly or use a `RegExp` for strings.If strict equality is desired for non-object types or `null`/`undefined`, ensure the pattern and object values are explicitly identical, or use a custom matching function if `tmatch`'s specific loose equality behavior is not suitable.
For patterns requiring anything beyond simple substring presence, use a regular expression. For example, to match a string starting with 'prefix', use `{ myString: /^prefix/ }` instead of `{ myString: 'prefix' }`.Use a default import for ESM (`import tmatch from 'tmatch';`) or assign the direct module export for CommonJS (`const tmatch = require('tmatch');`).To assert that a property `foo` is completely absent from the target object, set its value in the pattern to `null`: `{ foo: null }`. `tmatch` resolves missing keys to `undefined` during comparison, allowing `null` to effectively act as a 'does not exist' assertion.No dependency data recorded yet.