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 🙏

© 2025 – Pkg Stats / Ryan Hefner

bpc-graph

v0.0.66

Published

A box pin color (BPC) graph is a specialized graph structure where:

Readme

bpc-graph

A box pin color (BPC) graph is a specialized graph structure where:

  • Boxes contain pins
  • Pins belong to a network
  • Pins are coloured to describe signal type

Boxes can be fixed (they know their position) or floating (no position yet). Each pin stores an offset relative to its box. When all pins on a network are visualised, a schematic like connection graph is produced.

This repository provides utilities for manipulating and comparing BPC graphs.

Contents

Where BPC graphs are used

When automatically laying out schematics the tools in this repo convert an initial "floating" design into a fixed layout. Networks can be split, boxes can be adapted to a template and the resulting graph can be rendered with a force directed solver.

Installation

bun add bpc-graph

Quick Example

import { getGraphicsForBpcGraph } from "bpc-graph"
import { getSvgFromGraphicsObject } from "graphics-debug"

const graph = {
  boxes: [
    { boxId: "A", kind: "fixed", center: { x: 0, y: 0 } },
    { boxId: "B", kind: "fixed", center: { x: 2, y: 0 } },
  ],
  pins: [
    {
      boxId: "A",
      pinId: "P1",
      offset: { x: 0.5, y: 0 },
      color: "red",
      networkId: "N1",
    },
    {
      boxId: "A",
      pinId: "P2",
      offset: { x: 0.5, y: -0.5 },
      color: "blue",
      networkId: "N1",
    },
    {
      boxId: "B",
      pinId: "P1",
      offset: { x: -0.5, y: 0 },
      color: "red",
      networkId: "N1",
    },
    {
      boxId: "B",
      pinId: "CENTER",
      offset: { x: 0, y: 0 },
      color: "gray",
      networkId: "N2",
    },
  ],
}

const svg = getSvgFromGraphicsObject(getGraphicsForBpcGraph(graph), {
  backgroundColor: "white",
  includeTextLabels: true,
})

Basic graph

Library

getGraphBounds(graph)

getGraphBounds(g)
// { minX, minY, maxX, maxY }

getPinDirection(graph, boxId, pinId)

Determines which direction a pin is facing (and which side of the box it is on) by examining its offset relative to the bounds of the box.

getPinDirection(g, "A", "P1")
// x-" | "x+" | "y-" | "y+" | null

assignFloatingBoxPositions(graph)

Infers positions for floating boxes based on the positions of any connected pins

Starting with floating boxes (no fixed positions), the layout solver can automatically assign positions:

import { assignFloatingBoxPositions } from "bpc-graph"

const floatingGraph = {
  boxes: [
    { boxId: "A", kind: "floating" },
    { boxId: "B", kind: "floating" },
  ],
  pins: [
    /* ... */
  ],
}

// Convert floating boxes to fixed positions
const fixedGraph = assignFloatingBoxPositions(floatingGraph)

The image shows floating boxes (left) being automatically positioned into a fixed layout (right):

Floating boxes to fixed layout

netAdaptBpcGraph(source, target)

Adapt the source graph so that on a network, connection and pin count level it matches the target graph. This function will insert or remove pins and boxes or change network ids until there is a 1:1 matching for boxes and pins between the source and target.

After net adapt, the graph will have floating boxes- we use assignFloatingBoxPositions to infer positions for these boxes below.

Net adapt example

renetworkWithCondition(graph, predicate)

Change the networks of a graph based on a predicate.

const { renetworkedGraph } = renetworkWithCondition(
  ogGraph,
  (from, to, networkId) => {
    if (!from.box.center || !to.box.center) return true
    const fromSide =
      from.box.center.x + from.pin.offset.x < component0Center.x
        ? "left"
        : "right"
    const toSide =
      to.box.center.x + to.pin.offset.x < component0Center.x ? "left" : "right"
    return fromSide === toSide
  }
)

Renetwork result

getBpcGraphWlDistance(a, b)

Compute graph distance based on the "bag of colors" from a Weisfeiler-Leman algorithm. This algorithm runs several iterations of "color hashes" to create a "bag of colors" for each graph, a Jaccard index is then computed between the bags of colors.

Weisfeiler-Leman distance example

ForceDirectedLayoutSolver

Physics based solver for positioning boxes

All type definitions can be imported from bpc-graph as well and are located in lib/types.ts.

getBoxSideSubgraph({ bpcGraph, boxId, side })

Get the subgraph of one side of a box with all connections

Box Side Subgraph

mergeBoxSideSubgraphs(graphs)

Merge two subgraphs into a single graph

import { mergeBoxSideSubgraphs } from "bpc-graph"

const mergedGraph = mergeBoxSideSubgraphs([leftSubgraph, rightSubgraph])

Merging example

convertToFlatBpcGraph(graph)

Flatten a BPC graph into nodes and undirected edges. This is performed prior to constructing adjacency matrices. This changes the representation of the graph from a 2 layer hierarchy to a flat list of nodes and edges.

Flattening example

convertFromFlatBpcGraph(flatBpcGraph)

Rebuild a BPC graph from the flat representation

layoutSchematic(graph, options)

Automatically partition, match and lay out a floating BPC graph using a corpus of fixed graph layouts.

import { layoutSchematicGraph } from "bpc-graph"

const { fixedGraph } = layoutSchematicGraph(floatingGraph, { corpus })

layout schematic example

layoutSchematicWithInputVariants(variants, options)

Try multiple input variants of a floating graph and pick the one that best matches the corpus.

import { layoutSchematicGraphVariants } from "bpc-graph"

const { fixedGraph, selectedVariantName } = layoutSchematicGraphVariants(
  variants,
  { corpus },
)

layout schematic variants example