MCPcopy Index your code
hub / github.com/TanStack/db / findInsertPositionInArray

Function findInsertPositionInArray

packages/db/src/utils/array-utils.ts:8–28  ·  view source on GitHub ↗
(
  sortedArray: Array<T>,
  value: T,
  compareFn: (a: T, b: T) => number,
)

Source from the content-addressed store, hash-verified

6 * @returns The index where the value should be inserted to maintain order
7 */
8export function findInsertPositionInArray<T>(
9 sortedArray: Array<T>,
10 value: T,
11 compareFn: (a: T, b: T) => number,
12): number {
13 let left = 0
14 let right = sortedArray.length
15
16 while (left < right) {
17 const mid = Math.floor((left + right) / 2)
18 const comparison = compareFn(sortedArray[mid]!, value)
19
20 if (comparison < 0) {
21 left = mid + 1
22 } else {
23 right = mid
24 }
25 }
26
27 return left
28}
29
30/**
31 * Finds the correct insert position for a value in a sorted tuple array using binary search

Callers 5

deleteInSortedArrayFunction · 0.85
addMethod · 0.85
rangeQueryMethod · 0.85
takeMethod · 0.85
takeReversedMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected