es-stl - v3.0.2
    Preparing search index...
    • Finds the index of a sorted range that contains searchValue.

      Type Parameters

      • T extends object

        Object type that describes each searchable range.

      Parameters

      • array: T[]

        Sorted array of range-like objects.

      • searchValue: number

        Numeric value to locate within a range.

      • callback: SearchRangeCallback<T>

        Writes an element's inclusive range bounds.

      Returns number

      The index of the containing range, or -1 when no range matches.

      The callback writes the searchable range into the provided two-item range array as [start, end]. Bounds are treated as inclusive and ranges must be ordered from low to high by their bounds.

      const dataList = [
      { start: 0, end: 5 },
      { start: 6, end: 18 },
      { start: 19, end: 30 },
      ];

      const index = binarySearchByRange(dataList, 10, (range, data) => {
      range[0] = data.start;
      range[1] = data.end;
      });
      // index === 1
      const pages = [
      { from: 1, to: 10 },
      { from: 20, to: 30 },
      ];

      binarySearchByRange(pages, 15, (range, page) => {
      range[0] = page.from;
      range[1] = page.to;
      });
      // => -1