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

rect-utils

v1.0.0

Published

Utility functions for rectangle and point manipulation

Readme

rect-utils

CI Publish to NPM npm version License: MIT

TypeScript utility library for rectangle and point manipulation.

Installation

npm install rect-utils

Features

  • RectOps: Creation, validation, and conversion operations
  • RectCompare: Comparison, intersection, and containment checks
  • RectTransform: Inflate, deflate, offset, and alignment transformations
  • RectGeometry: Center and corner point calculations
  • RectBorder: Side extraction, cutting, and expansion operations
  • RectSplit: Horizontal and vertical splitting into cells

Usage

import {
  Rectangle,
  Point,
  ContentAlignment,
  RectOps,
  RectCompare,
  RectTransform,
  RectGeometry,
  RectBorder,
  RectSplit
} from 'rect-utils';

// Create rectangles
const rect: Rectangle = { x: 10, y: 20, width: 100, height: 50 };
const rect2 = RectOps.create(0, 0, 100, 100);
const rect3 = RectOps.fromPoints({ x: 50, y: 50 }, { x: 150, y: 100 });

// Calculate center
const center = RectGeometry.center(rect);
console.log(center); // { x: 60, y: 45 }

// Check intersection
const rect4 = { x: 50, y: 30, width: 80, height: 60 };
if (RectCompare.intersects(rect, rect4)) {
  console.log('Rectangles intersect');
}

// Check containment
const smallRect = { x: 20, y: 25, width: 30, height: 20 };
if (RectCompare.contains(rect, smallRect)) {
  console.log('rect contains smallRect');
}

// Align rectangle
const baseRect = { x: 0, y: 0, width: 200, height: 200 };
const aligned = RectTransform.align(
  { x: 0, y: 0, width: 50, height: 50 },
  baseRect,
  ContentAlignment.MiddleCenter
);
console.log(aligned); // { x: 75, y: 75, width: 50, height: 50 }

// Split into cells
const cells = RectSplit.horizontal(rect, 3);
console.log(cells.length); // 3

// Transform operations
const inflated = RectTransform.inflate(rect, 5, 5, 5, 5);
const deflated = RectTransform.deflate(rect, 10, 10, 10, 10);
const moved = RectTransform.offset(rect, 20, 30);

// Border operations
const topPart = RectBorder.getTopSide(rect, 10);
const withoutTop = RectBorder.cutFromTop(rect, 10);
const expanded = RectBorder.expandToBottom(rect, 20);

API Reference

RectOps - Basic Operations

  • ceiling(size) - Round up size dimensions
  • validate(rect) - Normalize rectangle with positive dimensions
  • create(x1, y1, x2, y2) - Create from coordinates
  • fromPoints(pt1, pt2) - Create from two points
  • inflateToInteger(rect) - Expand to integer bounds
  • snap(rect, dpi, snapDpi) - Snap to pixel grid

RectCompare - Comparison Operations

  • equals(rect1, rect2, epsilon?) - Compare with tolerance
  • isEmpty(rect, epsilon?) - Check if completely empty
  • isEmptySize(rect, epsilon?) - Check if size is zero
  • intersects(rect1, rect2, epsilon?) - Check intersection
  • contains(rect1, rect2, epsilon?) - Check containment
  • containsPoint(rect, point, digits?) - Check point containment
  • containsX(rect, x) - Check X coordinate
  • containsY(rect, y) - Check Y coordinate
  • intersect(rect1, rect2) - Compute intersection
  • unionNonEmpty(rect1, rect2) - Union ignoring empty
  • isEqualArray(arr1, arr2) - Compare rectangle arrays

RectTransform - Transformations

  • inflate(rect, left, top, right, bottom) - Expand rectangle
  • inflateWithMargins(rect, margins) - Expand with margins
  • deflate(rect, left, top, right, bottom) - Shrink rectangle
  • deflateWithMargins(rect, margins) - Shrink with margins
  • offset(rect, x, y) - Move rectangle
  • offsetByPoint(rect, point) - Move by point
  • align(rect, baseRect, alignment) - Align within base
  • calcCropping(bounds, rect) - Calculate cropping padding

RectGeometry - Geometry Operations

  • center(rect) - Get center point
  • topLeft(rect) - Get top-left corner
  • topRight(rect) - Get top-right corner
  • bottomLeft(rect) - Get bottom-left corner
  • bottomRight(rect) - Get bottom-right corner

RectBorder - Border Operations

  • getTopSide(rect, size) - Extract top border
  • getBottomSide(rect, size) - Extract bottom border
  • getLeftSide(rect, size) - Extract left border
  • getRightSide(rect, size) - Extract right border
  • setLeft(rect, value) - Set left boundary
  • setRight(rect, value) - Set right boundary
  • cutFromTop/Bottom/Left/Right(rect, size) - Remove from side
  • expandToTop/Bottom/Left/Right(rect, value) - Expand to side

RectSplit - Splitting Operations

  • horizontal(bounds, cellCount, isRightToLeft?) - Split horizontally
  • vertical(bounds, cellCount) - Split vertically

Types

interface Point {
  x: number;
  y: number;
}

interface Rectangle {
  x: number;
  y: number;
  width: number;
  height: number;
}

interface Size {
  width: number;
  height: number;
}

interface Margins {
  left: number;
  top: number;
  right: number;
  bottom: number;
}

enum ContentAlignment {
  TopLeft,
  TopCenter,
  TopRight,
  MiddleLeft,
  MiddleCenter,
  MiddleRight,
  BottomLeft,
  BottomCenter,
  BottomRight
}

Examples

Creating a 3x3 Grid

const container = { x: 0, y: 0, width: 300, height: 300 };
const rows = RectSplit.vertical(container, 3);
const grid = rows.map(row => RectSplit.horizontal(row, 3));

grid.forEach((row, i) => {
  console.log(`Row ${i}:`, row);
});

Panel with Header and Content

const panel = { x: 10, y: 10, width: 400, height: 300 };
const headerHeight = 40;
const padding = 10;

const header = RectBorder.getTopSide(panel, headerHeight);
const contentArea = RectBorder.cutFromTop(panel, headerHeight);
const content = RectTransform.deflate(contentArea, padding, padding, padding, padding);

console.log('Header:', header);
console.log('Content:', content);

Project Structure

The library is organized into focused modules:

src/
├── types.ts           # Type definitions
├── ops.ts             # RectOps - creation & validation
├── compare.ts         # RectCompare - comparison operations
├── transform.ts       # RectTransform - transformations
├── geometry.ts        # RectGeometry - geometric calculations
├── border.ts          # RectBorder - border operations
├── split.ts           # RectSplit - splitting operations
└── index.ts           # Main entry point

Each namespace is self-contained and can be tree-shaken by modern bundlers.

For detailed architecture information, see docs/ARCHITECTURE.md.

Documentation

Author

sky3d

License

MIT License - see LICENSE file for details

Contributing

Contributions are welcome! Please read CONTRIBUTING.md for details.

Changelog

See CHANGELOG.md for release history.