All files / core/src/utilities FrameRange.ts

4.76% Statements 1/21
0% Branches 0/16
0% Functions 0/6
5% Lines 1/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65                  428x                                                                                                              
export type FramesRange = [number, number] | number;
 
/**
 * This class handles the annotation frame range values for multiframes.
 * Mostly used for the Video viewport, it allows references to
 * a range of frame values.
 */
export default class FrameRange {
  protected static frameRangeExtractor =
    /(\/frames\/|[&?]frameNumber=)([^/&?]*)/i;
 
  /**
   * This method will extract a single frame number or range of frame numbers
   * from a multiframe image id containing a frame range.
   */
  protected static imageIdToFrames(imageId: string): FramesRange {
    const match = imageId.match(this.frameRangeExtractor);
    if (!match || !match[2]) {
      return null;
    }
    const range = match[2].split('-').map((it) => Number(it));
    if (range.length === 1) {
      return range[0];
    }
    return range as FramesRange;
  }
 
  public static imageIdToFrameEnd(imageId: string): number {
    const range = this.imageIdToFrames(imageId);
    return Array.isArray(range) ? range[1] : range;
  }
 
  public static imageIdToFrameStart(imageId: string): number {
    const range = this.imageIdToFrames(imageId);
    return Array.isArray(range) ? range[0] : range;
  }
 
  /**
   * @returns A string range or single value representation of a range array
   *    or single instance image.
   */
  public static framesToString(range) {
    if (Array.isArray(range)) {
      return `${range[0]}-${range[1]}`;
    }
    return String(range);
  }
 
  /** Applies the range string to the given image id as a frame range. */
  protected static framesToImageId(
    imageId: string,
    range: FramesRange | string
  ): string {
    const match = imageId.match(this.frameRangeExtractor);
    if (!match || !match[2]) {
      return null;
    }
    const newRangeString = this.framesToString(range);
    return imageId.replace(
      this.frameRangeExtractor,
      `${match[1]}${newRangeString}`
    );
  }
}