/// Cases: /// [---- 1 ----] /// [-A-] [--C--] [-B-] /// [------------D------------] /// [----E----] [----F----] function rangeOverlap(min1: number, max1: number, min2: number, max2: number): number { if (min1 > max1 || min2 > max2) { throw new Error("Invalid range"); } if (max2 <= min1 || min2 >= max1) /* A + B */ { return 0; } if (min1 <= min2 && max1 >= max2) /* C */ { return max2 - min2; } if (min1 >= min2 && max1 <= max2) /* D */ { return max1 - min1; } if (min1 >= min2 && max1 >= max2 && max2 >= min1) /* E */ { return max2 - min1; } if (min1 <= min2 && max1 >= min2 && max2 >= max1) /* F */ { return max1 - min2; } throw new Error("Invalid case"); } function rangeOverlapType(min1: number, max1: number, min2: number, max2: number): string { if (min1 > max1 || min2 > max2) { throw new Error("Invalid range"); } if (max2 <= min1) /* A */ { return "outside-before"; } if (min2 >= max1) /* B */ { return "outside-after"; } if (min1 <= min2 && max1 >= max2) /* C */ { return "contains"; } if (min1 >= min2 && max1 <= max2) /* D */ { return "contained"; } if (min1 >= min2 && max1 >= max2 && max2 >= min1) /* E */ { return "overlap-begin"; } if (min1 <= min2 && max1 >= min2 && max2 >= max1) /* F */ { return "overlap-end"; } throw new Error("Invalid case"); }
221300cookie-checkTypescript Range overlap