Utilities
Utility helpers, date helpers, and TypeScript utilities used across the monorepo
Utilities Package
The @workspace/utils package provides small, reusable helper functions and TypeScript utilities used across all applications and packages in the monorepo. It's focused on string/number helpers, date utilities, and lightweight helpers that keep application code concise.
Overview
- Package:
@workspace/utils - Location:
packages/utils - Purpose: Shared utility functions and TypeScript types for common tasks across apps
- Date Handling: date-fns for formatting and calculations
Architecture
The utilities package:
- Provides Small Helpers - String, number, and date helpers used widely across apps
- Date Handling - Re-exports from date-fns for formatting and calculations
- Enables Code Reuse - Modular, composable helper functions
Project Structure
packages/utils/
├── src/
│ ├── helpers/ # Utility functions
│ │ ├── string.ts # String manipulation (capitalize, truncate, isEmpty)
│ │ ├── number.ts # Number operations (clamp, round)
│ │ ├── date.ts # Date helpers (re-exports from date-fns)
│ │ └── index.ts # Helper exports
│ └── index.ts # Package entry point
├── package.json # Dependencies and scripts
└── tsconfig.json # TypeScript configurationUsage
Importing Utilities
Import specific utilities by category:
// Date helpers (re-exported from date-fns)
import { formatDate, formatDistanceToNow } from "@workspace/utils/helpers";
// Helper functions
import { capitalize, isEmpty } from "@workspace/utils/helpers";Helper Functions
The package includes helper functions for string manipulation and number operations (capitalize, truncate, isEmpty, clamp, round). These are lightweight utilities intended for common app tasks.
Date Helpers
All date helpers are re-exported from date-fns:
import {
formatDate,
formatDistanceToNow,
formatDuration,
intervalToDuration,
} from "@workspace/utils/helpers";
// Format a date
const date = new Date("2024-03-23");
formatDate(date); // "Mar 23, 2024" (default format)
// Get relative time
const pastDate = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
formatDistanceToNow(pastDate); // "2 days ago"
// Format a duration
formatDuration({ hours: 2, minutes: 30 }); // "2h 30m"
// Calculate interval between dates
intervalToDuration({ start: date1, end: date2 });Leverage Tree-Shaking
Import from subpaths when possible:
// ✓ Good - Only import helpers you need
import { capitalize } from "@workspace/utils/helpers";
// ✗ Bad - Imports entire utils package
import * as utils from "@workspace/utils";Create Custom Helpers
Extend package with domain-specific helpers:
import { capitalize } from "@workspace/utils/helpers";
// App-specific helper
export function formatUserName(first: string, last: string) {
return `${capitalize(first)} ${capitalize(last)}`;
}Troubleshooting
Type Errors
If TypeScript can't find types, ensure @workspace/utils is installed:
pnpm install
pnpm check-typesMissing Utilities
Check the source for available utilities:
ls packages/utils/src/helpers/Key Scripts
| Command | Purpose |
|---|---|
pnpm dev | Watch mode for development |
pnpm build | Build package for production |
pnpm lint | Check code quality |
Related Documentation
- Authentication Package - Integrates helper utilities
- API Application - Uses helpers and validators
- Web Application - Uses helpers and types
- date-fns Documentation - Date handling library
For additional utilities or to request new helpers, check the source code in
packages/utils/src/ or open an issue on
GitHub.