A comprehensive TypeScript library for working with time ranges and date intervals.
npm install time-range-utilsA comprehensive TypeScript library for working with time ranges and date intervals.
- ✅ Merge overlapping ranges - Combine overlapping time periods
- ✅ Subtract ranges - Remove time periods from ranges
- ✅ Split ranges - Divide ranges into equal chunks
- ✅ Check overlaps - Detect if ranges overlap
- ✅ Point-in-range testing - Check if dates fall within ranges
- ✅ Format ranges - Human-readable string representations
- ✅ TypeScript support - Full type safety and IntelliSense
- ✅ Comprehensive validation - Detailed error messages for invalid inputs
- ✅ Zero dependencies - Lightweight and fast
``bash`
npm install time-range-utilsor
yarn add time-range-utilsor
pnpm add time-range-utils
`typescript
import TimeRangeUtils from "time-range-utils";
const ranges = [
{
start: new Date("2025-01-01T10:00:00Z"),
end: new Date("2025-01-01T12:00:00Z"),
},
{
start: new Date("2025-01-01T11:00:00Z"),
end: new Date("2025-01-01T13:00:00Z"),
},
];
// Merge overlapping ranges
const merged = TimeRangeUtils.mergeOverlap(ranges);
// Result: [{ start: 2025-01-01T10:00:00Z, end: 2025-01-01T13:00:00Z }]
// Split a range into chunks
const range = {
start: new Date("2025-01-01T10:00:00Z"),
end: new Date("2025-01-01T12:00:00Z"),
};
const chunks = TimeRangeUtils.splitRange(range, 4);
// Result: 4 equal 30-minute periods
// Check if date is within range
const isInside = TimeRangeUtils.isInside(
new Date("2025-01-01T11:00:00Z"),
range
);
// Result: true
`
Merges overlapping or adjacent time ranges.
Subtracts rangeB from rangeA, returning remaining time periods.
Splits a time range into equal chunks.
Checks if two ranges overlap (inclusive boundaries).
Tests if a date falls within a range (inclusive boundaries).
Converts a range to a human-readable string.
The library provides detailed error types for better debugging:
`typescript
import { InvalidDateRangeError, InvalidParameterError } from "time-range-utils";
try {
TimeRangeUtils.splitRange(range, -1);
} catch (error) {
if (error instanceof InvalidParameterError) {
console.log(Invalid parameter: ${error.parameterName});Value: ${error.parameterValue}
console.log();`
}
}
Full TypeScript definitions included:
`typescript`
type DateRange = {
start: Date;
end: Date;
};
1. Fork the repository
2. Create a feature branch: git checkout -b feature/amazing-featurepnpm test
3. Make your changes and add tests
4. Run tests: pnpm run lint
5. Run linting: git commit -m 'Add amazing feature'
6. Commit your changes: git push origin feature/amazing-feature
7. Push to the branch:
8. Open a Pull Request
`bashInstall dependencies
pnpm install
This project is licensed under the MIT License - see the LICENSE file for details.
See CHANGELOG.md for a detailed history of changes.
- 📖 Documentation
- 🐛 Issue Tracker
- 💬 Discussions