This document describes the internal structure and organization of the rect-utils library.
rect-utils/
├── src/ # Source code
│ ├── types.ts # TypeScript type definitions
│ ├── internal/ # Internal utilities (not exported)
│ │ └── helpers.ts # Helper functions (fromLTRB, getLeft, etc.)
│ ├── ops.ts # RectOps namespace - creation & validation
│ ├── compare.ts # RectCompare namespace - comparison operations
│ ├── transform.ts # RectTransform namespace - transformations
│ ├── geometry.ts # RectGeometry namespace - geometric calculations
│ ├── border.ts # RectBorder namespace - border operations
│ ├── split.ts # RectSplit namespace - splitting operations
│ └── index.ts # Main entry point (exports all namespaces)
│
├── tests/ # Test files (Node.js Test Runner)
│ ├── rect-ops.test.ts
│ ├── rect-compare.test.ts
│ ├── rect-transform.test.ts
│ ├── rect-geometry.test.ts
│ ├── rect-border.test.ts
│ └── rect-split.test.ts
│
├── docs/ # Documentation
│ ├── API.md # Complete API reference
│ ├── EXAMPLES.md # Practical usage examples
│ └── ARCHITECTURE.md # This file
│
├── scripts/ # Build and utility scripts
│ └── test.sh # Test runner script
│
├── .github/ # GitHub configuration
│ ├── workflows/ # CI/CD workflows
│ │ ├── ci.yml # Continuous integration
│ │ ├── publish.yml # NPM publishing
│ │ └── codeql.yml # Security analysis
│ └── ISSUE_TEMPLATE/ # Issue templates
│
├── package.json # NPM package configuration
├── tsconfig.json # TypeScript configuration
├── .eslintrc.json # ESLint configuration
├── .npmrc # NPM configuration
├── README.md # Main documentation
├── LICENSE # MIT License
├── CHANGELOG.md # Version history
├── CONTRIBUTING.md # Contribution guidelines
└── example.ts # Usage examples
The library is organized into focused namespaces, each in its own file:
Defines all TypeScript interfaces and enums:
Point- 2D point with x, y coordinatesRectangle- Rectangle with position and dimensionsSize- Width and heightMargins/Padding- Spacing on all sidesContentAlignment- Alignment options enum
Internal utility functions (not exported publicly):
fromLTRB()- Create rectangle from left, top, right, bottomgetLeft(),getTop(),getRight(),getBottom()- Extract boundscompareDoubles()- Floating point comparison with epsilonDEFAULT_EPSILON- Default tolerance for comparisons
Each namespace is self-contained in its own file:
Basic rectangle operations:
- Creation (
create,fromPoints) - Validation (
validate) - Conversion (
ceiling,inflateToInteger,snap)
Comparison and containment:
- Equality checks (
equals,isEmpty,isEmptySize) - Intersection (
intersects,intersect) - Containment (
contains,containsPoint,containsX,containsY) - Set operations (
unionNonEmpty,isEqualArray)
Transformation operations:
- Expansion/contraction (
inflate,deflate) - Movement (
offset,offsetByPoint) - Alignment (
align) - Cropping calculation (
calcCropping)
Geometric calculations:
- Center point (
center) - Corner points (
topLeft,topRight,bottomLeft,bottomRight)
Border and side operations:
- Side extraction (
getTopSide,getBottomSide,getLeftSide,getRightSide) - Boundary modification (
setLeft,setRight) - Cutting (
cutFromTop,cutFromBottom,cutFromLeft,cutFromRight) - Expansion (
expandToTop,expandToBottom,expandToLeft,expandToRight)
Rectangle division:
- Horizontal splitting (
horizontal) - Vertical splitting (
vertical)
All functions are pure - they don't modify input parameters and always return new objects.
// ✅ Good - returns new rectangle
const moved = RectTransform.offset(rect, 10, 20);
// ❌ Bad - would modify original (not used in this library)
rect.offset(10, 20);Functions are grouped into logical namespaces similar to lodash:
import { RectOps, RectCompare } from 'rect-utils';
const rect = RectOps.create(0, 0, 100, 100);
const isEmpty = RectCompare.isEmpty(rect);Full TypeScript support with strict typing:
interface Rectangle {
x: number;
y: number;
width: number;
height: number;
}Modular structure allows bundlers to eliminate unused code:
// Only imports RectOps module
import { RectOps } from 'rect-utils';No runtime dependencies - only dev dependencies for building and testing.
Uses Node.js built-in Test Runner (Node 18+) with TypeScript support via tsx:
- No external test framework dependencies
- Native Node.js testing capabilities
- Fast execution
Each namespace has its own test file:
tests/rect-ops.test.tstests/rect-compare.test.tstests/rect-transform.test.tstests/rect-geometry.test.tstests/rect-border.test.tstests/rect-split.test.ts
npm test # Run all tests
npm run test:single # Run specific test filenpm run buildCompiles TypeScript to JavaScript with:
- ES2020 target
- CommonJS modules
- Type declarations (.d.ts files)
- Output to
dist/directory
npm run lint # Check for issues
npm run lint:fix # Auto-fix issuesUses ESLint with TypeScript support.
Runs on every push and pull request:
- Install dependencies
- Run linter
- Build project
- Run tests
- Test on Node.js 16, 18, 20
Automatically publishes to NPM on release:
- Run CI checks
- Build project
- Publish to NPM registry
Weekly CodeQL security analysis.
// Import everything from main entry
import { RectOps, RectCompare, Rectangle } from 'rect-utils';
// Or import specific namespaces
import { RectOps } from 'rect-utils';// Import from specific modules
import { RectOps } from './src/ops';
import { RectCompare } from './src/compare';Choose the correct module based on functionality:
- Creation/validation →
ops.ts - Comparison →
compare.ts - Transformation →
transform.ts - Geometry →
geometry.ts - Borders →
border.ts - Splitting →
split.ts
Create tests in corresponding test file:
describe('NewFeature', () => {
it('should do something', () => {
// Test implementation
});
});- Add to API.md
- Add example to EXAMPLES.md if applicable
- Update README.md if it's a major feature
Ensure the namespace is exported from src/index.ts.
All operations return new objects, which:
- ✅ Prevents bugs from shared state
- ✅ Enables functional composition
⚠️ Creates garbage for GC (minimal impact for typical use)
Floating-point comparisons use epsilon tolerance:
const DEFAULT_EPSILON = 1e-10;This prevents issues with floating-point precision.
Zero runtime dependencies means:
- Smaller bundle size
- Faster installation
- No security vulnerabilities from dependencies
- Better tree-shaking
See CHANGELOG.md for detailed version history.
See CONTRIBUTING.md for contribution guidelines.