npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@boundaries/elements

v1.1.2

Published

Element descriptors and matchers for @boundaries tools

Readme

Build status Coverage Status Quality Gate

Renovate Last commit Last release

NPM downloads License

Mutation testing badge

@boundaries/elements

Element descriptors and matchers for @boundaries tools, such as @boundaries/eslint-plugin.

Table of Contents

Introduction

@boundaries/elements provides a powerful and flexible system for defining and enforcing architectural boundaries in your JavaScript and TypeScript projects. It allows you to:

  • Define element types based on file path patterns (e.g., components, services, helpers)
  • Match elements against specific criteria
  • Validate dependencies between different parts of your codebase
  • Enforce architectural rules by checking if dependencies between elements are allowed

This package is part of the @boundaries ecosystem and uses Micromatch patterns for flexible and powerful pattern matching.

[!NOTE] This package does not read or analyze your codebase directly. It provides the core logic for defining and matching elements and dependencies, which can be integrated into other tools such as linters or build systems.

Features

  • Flexible pattern matching using Micromatch syntax
  • 🎯 Element type and category identification based on file paths
  • 📝 Template variables for dynamic selector matching
  • Built-in caching for optimal performance
  • 🔄 Support for multiple file matching modes (file, folder, full path)
  • 🎨 Capture path fragments for advanced matching scenarios

Installation

Install the package via npm:

npm install @boundaries/elements

Quick Start

Here's a quick example to get you started:

import { Elements } from '@boundaries/elements';

// Create an Elements instance
const elements = new Elements();

// Define element descriptors
const matcher = elements.getMatcher([
  {
    type: "component",
    category: "react",
    pattern: "src/components/*.tsx",
    mode: "file",
    capture: ["fileName"],
  },
  {
    type: "service",
    category: "data",
    pattern: "src/services/*.ts",
    mode: "file",
    capture: ["fileName"],
  },
]);

// Match an element
const isComponent = matcher.isMatch("src/components/Button.tsx", { 
  type: "component" 
}); // true

// Match a dependency
const isValidDependency = matcher.isMatch(
  {
    from: "src/components/Button.tsx",
    to: "src/services/Api.ts",
    source: "../services/Api",
    kind: "value",
    nodeKind: "ImportDeclaration",
  },
  {
    from: { category: "react" },
    to: { type: "service" },
  }
); // true

Usage

Configuration Options

When creating an Elements instance, you can provide configuration options that will be used as defaults for all matchers:

const elements = new Elements({
  ignorePaths: ["**/dist/**", "**/build/**", "**/node_modules/**"],
  includePaths: ["src/**/*"],
});

Available options:

  • ignorePaths: Micromatch pattern(s) to exclude certain paths from element matching (default: none)
  • includePaths: Micromatch pattern(s) to include only specific paths (default: all paths)
  • legacyTemplates: Whether to enable legacy template syntax support (default: true, but it will be false in future releases). This allows using ${variable} syntax in templates for backward compatibility.
  • cache: Whether to enable internal caching to improve performance (default: true)

Creating a Matcher

Use the getMatcher method to create a matcher with element descriptors:

const matcher = elements.getMatcher([
  {
    type: "component",
    pattern: "src/components/*",
    mode: "folder",
  },
  {
    type: "helper",
    pattern: "src/helpers/*.js",
    mode: "file",
  }
]);

💡 Tip: Matchers with identical descriptors and options share the same cache instance for improved performance.

You can override the default options when creating a matcher:

const matcher = elements.getMatcher(
  [/* descriptors */],
  {
    ignorePaths: ["**/*.test.ts"],
  }
);

Element Descriptors

Element descriptors define how files are identified and categorized. Each descriptor is an object with the following properties:

  • pattern (string | string[]): Micromatch pattern(s) to match file paths
  • type (string): The element type to assign to matching files
  • category (string): Additional categorization for the element, providing another layer of classification
  • mode ("file" | "folder" | "full"): Matching mode (default: "folder")
    • "folder": Matches the first folder matching the pattern. The library will add **/* to the given pattern for matching files, because it needs to know exactly which folder has to be considered the element. So, you have to provide patterns matching the folder being the element, not the files directly.
    • "file": Matches files directly, but still matches progressively from the right. The provided pattern will not be modified.
    • "full": Matches the complete path.
  • basePattern (string): Additional pattern that must match from the project root. Use it when using file or folder modes and you want to capture fragments from the rest of the path.
  • capture (string[]): Array of keys to capture path fragments
  • baseCapture (string[]): Array of keys to capture fragments from basePattern. If the same key is defined in both capture and baseCapture, the value from capture takes precedence.

Selectors

Selectors are used to match elements and dependencies against specific criteria. They are objects where each property represents a matching condition.

Element Properties

All element selectors support the following properties:

  • type (string | string[]): Micromatch pattern(s) for the element type/s
  • category (string | string[]): Micromatch pattern(s) for the element category/categories
  • captured (object): Object with keys matching captured values. Each key can be a string or an array of strings representing micromatch patterns.
  • origin ("local" | "external" | "core"): Element origin
    • local: Files within the project
    • external: External dependencies (e.g., node_modules)
    • core: Core modules (e.g., Node.js built-ins)
  • path (string | string[]): Micromatch pattern(s) for the file path
  • elementPath (string | string[]): Pattern(s) for the element path
  • internalPath (string | string[]): Pattern(s) for the path within the element. For file elements, it's the same as elementPath; for folder elements, it's relative to the folder.
  • isIgnored (boolean): Whether the element is ignored
  • isUnknown (boolean): Whether the element type is unknown (i.e., doesn't match any descriptor)

Dependency Properties

When matching dependencies, the to selector can additionally use:

  • kind (string | string[]): Micromatch pattern(s) for the dependency kind
  • relationship (string | string[]): Element relationship. Micromatch pattern(s) for the relationship between source and target elements:
    • internal: Both files belong to the same element
    • child: Target is a child of source
    • parent: Target is a parent of source
    • sibling: Elements share the same parent
    • uncle: Target is a sibling of a source ancestor
    • nephew: Target is a child of a source sibling
    • descendant: Target is a descendant of source
    • ancestor: Target is an ancestor of source
  • specifiers (string | string[]): Pattern(s) for import/export specifiers (e.g., named imports)
  • nodeKind (string | string[]): Pattern(s) for the AST node type causing the dependency (e.g., "ImportDeclaration")
  • source (string | string[]): Pattern(s) to match the source of the dependency. (e.g., the import path).
  • baseSource (string | string[]): Pattern(s) for the base module name for external imports.

⚠️ Important: All properties in a selector must match for the selector to be considered a match (AND logic). Use multiple selectors for OR logic.

Note: You can also use the legacy selector syntax, but it’s deprecated and will be removed in a future release. See the Legacy Selectors section for more details.

Template Variables

Selectors support template variables using Handlebars syntax ({{ variableName }}). Templates are resolved at match time using:

  • Element properties (type, category, captured, etc.)
  • Dependency properties (from, to)

Available Template Data

When matching, the following data is automatically available:

For element matching:

  • Properties of the element under match are available in the element object (type, category, captured, origin, path, etc.)

For dependency matching:

  • from: Properties of the dependency source element
  • to: Properties of the dependency target element, and properties of the dependency itself (source, kind, nodeKind, specifiers, etc.)

Template Examples

// Using captured values in templates
const matcher = elements.getMatcher([
  {
    type: "component",
    pattern: "src/modules/(*)/**/*.component.tsx",
    capture: ["module", "elementName", "fileName"],
    mode: "file"
  }
]);

// Match components from specific module using template
const isAuthComponent = matcher.isMatch(
  "src/modules/auth/LoginForm.component.tsx",
  { 
    type: "component",
    captured: { module: "{{ element.captured.module }}" } // This will always match
  },
);

// Using templates in dependency selectors
const isDependencyMatch = matcher.isMatch(
  {
    from: "src/components/Button.tsx",
    to: "src/services/Api.ts",
    source: "../services/Api",
    kind: "type",
    nodeKind: "ImportDeclaration",
    specifiers: ["calculateSum", "calculateAvg"],
  },
  {
    from: { type: "{{ from.type }}", captured: { fileName: "{{ from.captured.fileName }}" } },
    to: { path: "{{ to.path }}", specifiers: "{{ lookup to.specifiers 0 }}", kind: "{{ to.kind }}" },
  }
);

Using Extra Template Data

You can provide additional template data using the extraTemplateData option in MatcherOptions:

// Using templates in selectors
const isMatch = matcher.isMatch(
  "src/components/UserProfile.tsx",
  { type: "{{ componentType }}" },
  {
    extraTemplateData: { componentType: "component" }
  }
);

Using Matchers

You can use element selectors with a created matcher to check if a given path corresponds to an element with specific properties, or if a dependency between two paths matches certain criteria.

Element Matching

To match an element, use the isMatch method of the matcher, providing the file path and an element selector.

const isElementMatch = matcher.isMatch("src/components/Button.tsx", { type: "component" });

[!TIP] You can also provide an array of selectors to the isMatch method. In this case, the method will return true if the element matches any of the provided selectors (OR logic).

Dependency Matching

To match a dependency, use the isMatch method of the matcher, providing the properties of the dependency and a dependency selector.

Dependency object properties:

  • from (string): Source file path
  • to (string): Target file path
  • source (string): Import/export source as written in code
  • kind (string): Import kind ("type", "value", "typeof")
  • nodeKind (string): AST node kind
  • specifiers (string[]): Imported/exported names

Dependency selector:

  • from: Element selector(s) for the source file
  • to: Dependency selector(s) for the target file
const isDependencyMatch = matcher.isMatch(
  { // Dependency properties
    from: "src/components/Button.tsx",
    to: "src/services/Api.ts",
    source: "../services/Api",
    kind: "type",
    nodeKind: "ImportDeclaration",
  },
  {
    from: { category: "react" }, // Dependency source selector/s
    to: { type: "service", nodeKind: "Import*" }, // Dependency target selector/s
  }
);

[!TIP] You can also provide an array of selectors both to the from and to properties of the dependency selector. In this case, the method will return true if the dependency matches any combination of the provided selectors (OR logic).

API Reference

Class: Elements

Constructor

Creates a new Elements instance with optional default configuration.

new Elements(options?: ConfigurationOptions);

Methods

getMatcher

Creates a new matcher instance.

  • Parameters:
    • descriptors: array<ElementDescriptor> Array of element descriptors to be used by the matcher.
    • options: ElementsOptions Optional. Configuration options for the matcher. These options will override the default options set in the Elements instance.
  • Returns: Matcher A new matcher instance.
const matcher = elements.getMatcher([
  {
    type: "component",
    pattern: "src/components/*",
    mode: "folder",
  },
  {
    type: "helper",
    pattern: "src/helpers/*.js",
    mode: "file",
  }
]);
clearCache

Clears all cached matcher instances and shared caches.

elements.clearCache();
serializeCache

Serializes all cached matcher instances and shared caches to a plain object.

const cache = elements.serializeCache();
setCacheFromSerialized

Sets the cached matcher instances and shared caches from a serialized object.

const cache = elements.serializeCache();
elements.setCacheFromSerialized(cache);

Matcher Instance Methods

isMatch

Checks if a given path matches the specified element or dependency selector.

const isElementMatch = matcher.isMatch("src/components/Button.tsx", [{ type: "component" }]);

const isDependencyMatch = matcher.isMatch(
  {
    from: "src/components/Button.tsx",
    to: "src/services/Api.ts",
    source: "../services/Api",
    kind: "type",
    nodeKind: "ImportDeclaration",
  },
  {
    from: [{ category: "react" }],
    to: { type: "service", nodeKind: "Import*" },
  }
);
  • Parameters:
    • path:
    • selector: ElementSelector | DependencySelector The selector to match against. It can be either an element selector (for path matching) or a dependency selector (for dependency matching).
      • If path is a string, selector should be an ElementSelector or an array of ElementSelector.
      • If path are dependency properties, selector should be a DependencySelector or an array of DependencySelector.
    • options: MatcherOptions Optional. Additional options for matching:
      • extraTemplateData: object Optional. Extra data to pass to selector templates. When using template variables in selectors, this data will be available for rendering.

getSelectorMatching

Returns the first matching selector or null.

const matchingSelector = matcher.getSelectorMatching("src/components/Button.tsx", [{ type: "component" }]);

Parameters are the same as isMatch, but instead of returning a boolean, it returns the first matching selector or null if none match.

describeElement

Returns a detailed description of an element.

const elementDescription = matcher.describeElement("src/components/Button.tsx");
  • Parameters:
    • path: string The path of the element to describe.

describeDependency

Returns a detailed description of a dependency.

const dependencyDescription = matcher.describeDependency({
  from: "src/components/Button.tsx",
  to: "src/services/Api.ts",
  source: "../services/Api",
  kind: "type",
  nodeKind: "ImportDeclaration",
});

getSelectorMatchingDescription

Matches a description against selectors. As first argument, it should receive the result of describeElement or describeDependency.

const elementDescription = matcher.describeElement("src/components/Button.tsx");
const matchingSelector = matcher.getSelectorMatchingDescription(elementDescription, [{ type: "component" }]);

clearCache

Clears the matcher's internal cache.

matcher.clearCache();

[!WARNING] This only clears the internal cache for this matcher instance. Shared cache for micromatch results, regex and captures is not affected. You can clear all caches using Elements.clearCache().

serializeCache

Serializes the matcher's cache.

const cache = matcher.serializeCache();

setCacheFromSerialized

Restores the matcher's cache from a serialized object.

// Serialize cache to a serializable object
const cache = matcher.serializeCache();

// Clear current cache
matcher.clearCache();

// Restore cache from serialized object
matcher.setCacheFromSerialized(cache);

Legacy Selectors

Legacy selectors are defined using a different syntax and are provided for backward compatibility. However, this syntax is deprecated and will be removed in a future release. It is recommended to migrate to the new selector syntax.

Selectors can be defined as either a string or an array of strings representing the element type(s):

// Legacy selector using a string
const isElementMatch = matcher.isMatch("src/components/Button.tsx", "component");
// Legacy selector using an array of strings
const isElementMatch = matcher.isMatch("src/components/Button.tsx", ["component", "service"]);

They can also be defined as an array where the first element is the type and the second element is an object containing captured values:

// Legacy selector with captured values
const isElementMatch = matcher.isMatch(
  "src/modules/auth/LoginForm.component.tsx",
  ["component", { module: "auth" }]
);

⚠️ Warning: Avoid mixing legacy selectors with the new selector syntax in the same project, as this can lead to ambiguity. In particular, if you define a top-level array selector with two elements and the second one is an object containing a type or category key, it will be interpreted as legacy options rather than two separate selectors.

Contributing

Contributors are welcome. Please read the contributing guidelines and code of conduct.

License

MIT, see LICENSE for details.