
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
tree-matcher
Advanced tools
tree-matcher
Yet another (very small) npm package for testing structural equality.
Intended mainly for cases when you have the "skeleton" of a tree and want to check that some object conforms to that skeleton, possibly with additional parts. For example, it works well for matching ASTs.
This module exports a single function which takes a matcher and a value, and returns true if the matcher matches the value according to the rules below.
const tm = require('tree-matcher');
const treeOne = {
type: 'BinaryExpression',
left: {
type: 'LiteralNumericExpression',
value: 0,
},
right: {
type: 'LiteralNumericExpression',
value: 1,
},
};
const treeTwo = {
type: 'BinaryExpression',
left: {
type: 'LiteralNumericExpression',
value: 0,
},
right: {
type: 'LiteralNumericExpression',
value: 2,
},
};
const matcher = {
type: 'BinaryExpression',
left: node => node.type !== 'LiteralStringExpression',
right: {
value: 1,
},
};
tm(matcher, treeOne); // true
tm(matcher, treeTwo); // false
A matcher matches a value as follows:
A primitive p
matches a value x
if Object.is(p, x)
is true. For example, null
matches null
, NaN
matches NaN
, 1
does not match '1'
, and 0
does not match -0
.
A function f
matches a value x
if f(x)
is truthy. For example, Array.isArray
matches []
.
A regex r
matches a value x
if x
is a regex and x
has the same .source
and .flags
as r
.
An array a
matches a value x
if x
is an array, the two are of the same length, and for every non-hole index i
in a
, a[i]
matches x[i]
. Holes in the matcher array match anything; holes in x
are matched by undefined
. For example, [1]
matches [1]
, [1,,]
matches [1,2]
, [1,void 0]
matches [1,,]
, [1]
does not match [1, 2]
, and [1, 2]
does not match [1]
.
Any other object o
matches a value x
if x
is an object and for every enumerable own key k
in o
, o[k]
matches x[k]
. For example, { a: { b: 0 }, c: 1 }
matches { a: { b: 0 } }
, and { a: { b: 0 } }
does not match { a: { b: 0 }, c: 1 }
.
FAQs
Match objects based on structural equality
The npm package tree-matcher receives a total of 19 weekly downloads. As such, tree-matcher popularity was classified as not popular.
We found that tree-matcher demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.