Fancy date ranges for Moment.js with comprehensive range operations and iteration capabilities
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Query relationships between ranges and dates including containment, overlap, adjacency, and intersection operations.
Test if a range contains a date, moment, or another range.
/**
* Check if this range contains a date, moment, or another range
* @param other - Date, moment, or range to test
* @param options - Options for boundary inclusion/exclusion
* @returns true if other is contained within this range
*/
contains(other: Date | Moment | DateRange, options?: ContainsOptions): boolean;
interface ContainsOptions {
excludeStart?: boolean; // Exclude start boundary from containment check
excludeEnd?: boolean; // Exclude end boundary from containment check
exclusive?: boolean; // DEPRECATED: Use excludeStart and excludeEnd
}Usage Examples:
const range = moment.range('2024-01-01', '2024-01-31');
// Test date containment
const testDate = moment('2024-01-15');
console.log(range.contains(testDate)); // true
// Test boundary dates
const startDate = moment('2024-01-01');
console.log(range.contains(startDate)); // true
console.log(range.contains(startDate, { excludeStart: true })); // false
// Test range containment
const subRange = moment.range('2024-01-10', '2024-01-20');
console.log(range.contains(subRange)); // true
// Test with boundary exclusion
const boundaryRange = moment.range('2024-01-01', '2024-01-15');
console.log(range.contains(boundaryRange, { excludeStart: true })); // falseTest if two ranges overlap or are adjacent.
/**
* Check if this range overlaps with another range
* @param other - Range to test for overlap
* @param options - Options for adjacency consideration
* @returns true if ranges overlap or are adjacent (when specified)
*/
overlaps(other: DateRange, options?: OverlapOptions): boolean;
interface OverlapOptions {
adjacent?: boolean; // Consider adjacent ranges as overlapping
}Usage Examples:
const range1 = moment.range('2024-01-01', '2024-01-15');
const range2 = moment.range('2024-01-10', '2024-01-25');
// Test overlap
console.log(range1.overlaps(range2)); // true
// Test non-overlapping
const range3 = moment.range('2024-01-20', '2024-01-31');
console.log(range1.overlaps(range3)); // false
// Test adjacent ranges
const range4 = moment.range('2024-01-16', '2024-01-31');
console.log(range1.overlaps(range4)); // false
console.log(range1.overlaps(range4, { adjacent: true })); // trueTest if two ranges are adjacent (touching but not overlapping).
/**
* Check if this range is adjacent to another range
* Adjacent means ranges touch at exactly one point but don't overlap
* @param other - Range to test for adjacency
* @returns true if ranges are adjacent
*/
adjacent(other: DateRange): boolean;Usage Examples:
const range1 = moment.range('2024-01-01', '2024-01-15');
const range2 = moment.range('2024-01-16', '2024-01-31');
console.log(range1.adjacent(range2)); // true
const range3 = moment.range('2024-01-14', '2024-01-20');
console.log(range1.adjacent(range3)); // false (overlapping)
const range4 = moment.range('2024-01-20', '2024-01-31');
console.log(range1.adjacent(range4)); // false (gap between ranges)Get the intersection of two ranges.
/**
* Get the intersection of this range with another range
* @param other - Range to intersect with
* @returns DateRange representing intersection, or null if no intersection
*/
intersect(other: DateRange): DateRange | null;Usage Examples:
const range1 = moment.range('2024-01-01', '2024-01-15');
const range2 = moment.range('2024-01-10', '2024-01-25');
// Get intersection
const intersection = range1.intersect(range2);
console.log(intersection.toString()); // "2024-01-10T00:00:00Z/2024-01-15T23:59:59Z"
// No intersection case
const range3 = moment.range('2024-01-20', '2024-01-31');
const noIntersection = range1.intersect(range3);
console.log(noIntersection); // null
// Zero-length range intersection
const point1 = moment.range('2024-01-10', '2024-01-10');
const point2 = moment.range('2024-01-10', '2024-01-10');
console.log(point1.intersect(point2)); // null (zero-length intersections return null)Test if two ranges are exactly equal.
/**
* Check if this range is exactly equal to another range
* @param other - Range to compare with
* @returns true if start and end moments are identical
*/
isEqual(other: DateRange): boolean;
/**
* Alias for isEqual
* @param other - Range to compare with
* @returns true if ranges are equal
*/
isSame(other: DateRange): boolean;Usage Examples:
const range1 = moment.range('2024-01-01', '2024-01-31');
const range2 = moment.range('2024-01-01', '2024-01-31');
const range3 = moment.range('2024-01-01', '2024-01-30');
console.log(range1.isEqual(range2)); // true
console.log(range1.isSame(range2)); // true (alias)
console.log(range1.isEqual(range3)); // falseTest if a moment is within a range (added to Moment instances).
/**
* Check if this moment is within the given range
* @param range - DateRange to test against
* @returns true if this moment is contained in the range
*/
moment.fn.within(range: DateRange): boolean;Usage Examples:
const range = moment.range('2024-01-01', '2024-01-31');
const testMoment = moment('2024-01-15');
console.log(testMoment.within(range)); // true
const outsideMoment = moment('2024-02-15');
console.log(outsideMoment.within(range)); // falseTest if an object is a DateRange instance.
/**
* Check if an object is a DateRange instance
* @param range - Object to test
* @returns true if object is a DateRange
*/
function moment.isRange(range: any): boolean;Usage Examples:
const range = moment.range('2024-01-01', '2024-01-31');
const notRange = { start: moment(), end: moment() };
console.log(moment.isRange(range)); // true
console.log(moment.isRange(notRange)); // false
console.log(moment.isRange(null)); // falseinterface ContainsOptions {
excludeStart?: boolean;
excludeEnd?: boolean;
exclusive?: boolean; // DEPRECATED in favor of excludeStart/excludeEnd
}
interface OverlapOptions {
adjacent?: boolean;
}